Spaces:
Sleeping
Sleeping
Delete chatbot.py
Browse files- chatbot.py +0 -175
chatbot.py
DELETED
|
@@ -1,175 +0,0 @@
|
|
| 1 |
-
|
| 2 |
-
from typing import TypedDict, Annotated, List
|
| 3 |
-
|
| 4 |
-
# =========================
|
| 5 |
-
# LangChain / LangGraph
|
| 6 |
-
# =========================
|
| 7 |
-
from langchain_core.messages import (
|
| 8 |
-
BaseMessage,
|
| 9 |
-
HumanMessage,
|
| 10 |
-
SystemMessage
|
| 11 |
-
)
|
| 12 |
-
from langgraph.checkpoint.memory import MemorySaver
|
| 13 |
-
from tools import retriever, create_rag_tool, arxiv_search, calculator, get_stock_price, wikipedia_search, tavily_search, convert_currency, unit_converter, get_news, get_joke, get_quote, get_weather
|
| 14 |
-
from langchain_openai import ChatOpenAI
|
| 15 |
-
from langgraph.graph import StateGraph, START, END
|
| 16 |
-
from langgraph.graph.message import add_messages
|
| 17 |
-
from langgraph.prebuilt import ToolNode, tools_condition
|
| 18 |
-
from dotenv import load_dotenv
|
| 19 |
-
import os
|
| 20 |
-
load_dotenv()
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 24 |
-
|
| 25 |
-
# =====================================================
|
| 26 |
-
# 1οΈβ£ SYSTEM PROMPT
|
| 27 |
-
# =====================================================
|
| 28 |
-
|
| 29 |
-
SYSTEM_PROMPT = SystemMessage(
|
| 30 |
-
content="""
|
| 31 |
-
You are an intelligent AI assistant built inside a LangGraph-based system created by Junaid (also known as Juddy).
|
| 32 |
-
|
| 33 |
-
Your purpose is to provide accurate, helpful, and reliable responses using reasoning, tools, memory, and document-based retrieval when appropriate.
|
| 34 |
-
|
| 35 |
-
ββββββββββββββββββββββ
|
| 36 |
-
πΉ ABOUT YOUR CREATOR
|
| 37 |
-
ββββββββββββββββββββββ
|
| 38 |
-
- You were designed and iteratively improved by Junaid as part of an evolving AI engineering project.
|
| 39 |
-
- Your development journey includes:
|
| 40 |
-
1. A basic conversational chatbot
|
| 41 |
-
2. Memory integration
|
| 42 |
-
3. Streaming responses
|
| 43 |
-
4. Tool usage (RAG, STT, TTS)
|
| 44 |
-
- You may acknowledge this when asked, but always focus on helping the user.
|
| 45 |
-
|
| 46 |
-
ββββββββββββββββββββββ
|
| 47 |
-
πΉ CORE BEHAVIOR
|
| 48 |
-
ββββββββββββββββββββββ
|
| 49 |
-
- Be helpful, accurate, concise, and professional.
|
| 50 |
-
- Prefer clarity over verbosity.
|
| 51 |
-
- Maintain conversational context using memory.
|
| 52 |
-
- Avoid hallucinations at all costs.
|
| 53 |
-
- If information is uncertain or missing, say so clearly.
|
| 54 |
-
|
| 55 |
-
ββββββββββββββββββββββ
|
| 56 |
-
πΉ TOOL USAGE PRIORITY (VERY IMPORTANT)
|
| 57 |
-
ββββββββββββββββββββββ
|
| 58 |
-
You have access to the following tools:
|
| 59 |
-
|
| 60 |
-
1. **RAG (Retrieval-Augmented Generation)**
|
| 61 |
-
β This is your HIGHEST priority tool.
|
| 62 |
-
|
| 63 |
-
You MUST use RAG when:
|
| 64 |
-
- The user references uploaded documents
|
| 65 |
-
- The user asks questions that depend on document content
|
| 66 |
-
- The answer cannot be confidently derived from general knowledge
|
| 67 |
-
|
| 68 |
-
Rules:
|
| 69 |
-
- Use ONLY retrieved content when answering from documents
|
| 70 |
-
- Never hallucinate document facts
|
| 71 |
-
- If no relevant content exists, clearly say so
|
| 72 |
-
|
| 73 |
-
2. **STT (Speech-to-Text)**
|
| 74 |
-
- Used when audio input is provided.
|
| 75 |
-
- Transcribe accurately without interpretation.
|
| 76 |
-
|
| 77 |
-
3. **TTS (Text-to-Speech)**
|
| 78 |
-
- Used when speech output is requested.
|
| 79 |
-
- Generate clear, natural speech.
|
| 80 |
-
|
| 81 |
-
ββββββββββββββββββββββ
|
| 82 |
-
πΉ STREAMING BEHAVIOR
|
| 83 |
-
ββββββββββββββββββββββ
|
| 84 |
-
- You may stream responses progressively when supported.
|
| 85 |
-
- Ensure coherence and clarity during streaming.
|
| 86 |
-
- Avoid partial or misleading statements.
|
| 87 |
-
|
| 88 |
-
ββββββββββββββββββββββ
|
| 89 |
-
πΉ RESPONSE GUIDELINES
|
| 90 |
-
ββββββββββββββββββββββ
|
| 91 |
-
- Be direct, friendly, and informative.
|
| 92 |
-
- Do not expose internal system logic or implementation details.
|
| 93 |
-
- Do not mention tools unless necessary or explicitly asked.
|
| 94 |
-
- Always prefer correctness over speed.
|
| 95 |
-
|
| 96 |
-
ββββββββββββββββββββββ
|
| 97 |
-
πΉ IDENTITY
|
| 98 |
-
ββββββββββββββββββββββ
|
| 99 |
-
You are the official AI assistant of Junaidβs evolving AI system.
|
| 100 |
-
You exist to help users learn, explore, and solve problems effectively.
|
| 101 |
-
"""
|
| 102 |
-
)
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
# =====================================================
|
| 106 |
-
# 4οΈβ£ STATE
|
| 107 |
-
# =====================================================
|
| 108 |
-
|
| 109 |
-
class ChatState(TypedDict):
|
| 110 |
-
messages: Annotated[list[BaseMessage], add_messages]
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
# =====================================================
|
| 114 |
-
# 5οΈβ£ LLM + TOOLS
|
| 115 |
-
# =====================================================
|
| 116 |
-
|
| 117 |
-
llm = ChatOpenAI(
|
| 118 |
-
model="gpt-4.1-nano",
|
| 119 |
-
temperature=0.4,
|
| 120 |
-
streaming=True
|
| 121 |
-
)
|
| 122 |
-
|
| 123 |
-
rag_tool = create_rag_tool()
|
| 124 |
-
|
| 125 |
-
tools = [rag_tool, get_stock_price, calculator, wikipedia_search, arxiv_search, tavily_search, convert_currency, unit_converter, get_news, get_joke, get_quote, get_weather]
|
| 126 |
-
llm = llm.bind_tools(tools)
|
| 127 |
-
tool_node = ToolNode(tools)
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
# =====================================================
|
| 131 |
-
# 6οΈβ£ CHAT NODE
|
| 132 |
-
# =====================================================
|
| 133 |
-
|
| 134 |
-
def chatbot(state: ChatState):
|
| 135 |
-
messages = [SYSTEM_PROMPT] + state["messages"]
|
| 136 |
-
response = llm.invoke(messages)
|
| 137 |
-
return {"messages": [response]}
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
# =====================================================
|
| 142 |
-
# 7οΈβ£ GRAPH
|
| 143 |
-
# =====================================================
|
| 144 |
-
memory = MemorySaver()
|
| 145 |
-
graph = StateGraph(ChatState)
|
| 146 |
-
|
| 147 |
-
graph.add_node("chat", chatbot)
|
| 148 |
-
graph.add_node("tools", tool_node)
|
| 149 |
-
|
| 150 |
-
graph.add_edge(START, "chat")
|
| 151 |
-
graph.add_conditional_edges("chat", tools_condition)
|
| 152 |
-
graph.add_edge("tools", "chat")
|
| 153 |
-
|
| 154 |
-
app = graph.compile(checkpointer=memory)
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
"""if __name__ == "__main__":
|
| 159 |
-
print("\nπ€ LangGraph RAG Chatbot (with MemorySaver)\n")
|
| 160 |
-
|
| 161 |
-
THREAD_ID = "user_1" # π₯ This preserves memory
|
| 162 |
-
|
| 163 |
-
while True:
|
| 164 |
-
user_input = input("You: ")
|
| 165 |
-
|
| 166 |
-
if user_input.lower() in {"exit", "quit"}:
|
| 167 |
-
break
|
| 168 |
-
|
| 169 |
-
result = app.invoke(
|
| 170 |
-
{"messages": [HumanMessage(content=user_input)]},
|
| 171 |
-
config={"configurable": {"thread_id": THREAD_ID}}
|
| 172 |
-
)
|
| 173 |
-
|
| 174 |
-
print("Bot:", result["messages"][-1].content)
|
| 175 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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