Spaces:
Sleeping
Sleeping
Update chatbot.py
Browse files- chatbot.py +124 -149
chatbot.py
CHANGED
|
@@ -1,150 +1,125 @@
|
|
| 1 |
-
|
| 2 |
-
from typing import TypedDict, Annotated
|
| 3 |
-
from langchain_core.messages import (
|
| 4 |
-
BaseMessage,
|
| 5 |
-
SystemMessage
|
| 6 |
-
)
|
| 7 |
-
from langgraph.checkpoint.memory import MemorySaver
|
| 8 |
-
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
|
| 9 |
-
from langchain_openai import ChatOpenAI
|
| 10 |
-
from langgraph.graph import StateGraph, START, END
|
| 11 |
-
from langgraph.graph.message import add_messages
|
| 12 |
-
from langgraph.prebuilt import ToolNode, tools_condition
|
| 13 |
-
from dotenv import load_dotenv
|
| 14 |
-
import os
|
| 15 |
-
load_dotenv()
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 19 |
-
|
| 20 |
-
# =====================================================
|
| 21 |
-
# 1οΈβ£ SYSTEM PROMPT
|
| 22 |
-
# =====================================================
|
| 23 |
-
|
| 24 |
-
SYSTEM_PROMPT = SystemMessage(
|
| 25 |
-
content="""
|
| 26 |
-
You are an
|
| 27 |
-
|
| 28 |
-
Your
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
-
|
| 45 |
-
-
|
| 46 |
-
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
)
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
# =====================================================
|
| 101 |
-
#
|
| 102 |
-
# =====================================================
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
messages
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
# =====================================================
|
| 126 |
-
# 6οΈβ£ CHAT NODE
|
| 127 |
-
# =====================================================
|
| 128 |
-
|
| 129 |
-
def chatbot(state: ChatState):
|
| 130 |
-
messages = [SYSTEM_PROMPT] + state["messages"]
|
| 131 |
-
response = llm.invoke(messages)
|
| 132 |
-
return {"messages": [response]}
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
# =====================================================
|
| 137 |
-
# 7οΈβ£ GRAPH
|
| 138 |
-
# =====================================================
|
| 139 |
-
memory = MemorySaver()
|
| 140 |
-
graph = StateGraph(ChatState)
|
| 141 |
-
|
| 142 |
-
graph.add_node("chat", chatbot)
|
| 143 |
-
graph.add_node("tools", tool_node)
|
| 144 |
-
|
| 145 |
-
graph.add_edge(START, "chat")
|
| 146 |
-
graph.add_conditional_edges("chat", tools_condition)
|
| 147 |
-
graph.add_edge("tools", "chat")
|
| 148 |
-
graph.add_edge("chat", END)
|
| 149 |
-
|
| 150 |
app = graph.compile(checkpointer=memory)
|
|
|
|
| 1 |
+
|
| 2 |
+
from typing import TypedDict, Annotated
|
| 3 |
+
from langchain_core.messages import (
|
| 4 |
+
BaseMessage,
|
| 5 |
+
SystemMessage
|
| 6 |
+
)
|
| 7 |
+
from langgraph.checkpoint.memory import MemorySaver
|
| 8 |
+
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
|
| 9 |
+
from langchain_openai import ChatOpenAI
|
| 10 |
+
from langgraph.graph import StateGraph, START, END
|
| 11 |
+
from langgraph.graph.message import add_messages
|
| 12 |
+
from langgraph.prebuilt import ToolNode, tools_condition
|
| 13 |
+
from dotenv import load_dotenv
|
| 14 |
+
import os
|
| 15 |
+
load_dotenv()
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 19 |
+
|
| 20 |
+
# =====================================================
|
| 21 |
+
# 1οΈβ£ SYSTEM PROMPT
|
| 22 |
+
# =====================================================
|
| 23 |
+
|
| 24 |
+
SYSTEM_PROMPT = SystemMessage(
|
| 25 |
+
content="""
|
| 26 |
+
You are an advanced AI assistant built by Junaid.
|
| 27 |
+
|
| 28 |
+
Your primary responsibility is to provide accurate, helpful, and well-structured answers using:
|
| 29 |
+
- reasoning
|
| 30 |
+
- memory
|
| 31 |
+
- tools (especially Retrieval-Augmented Generation)
|
| 32 |
+
|
| 33 |
+
ββββββββββββββββββββββ
|
| 34 |
+
πΉ DOCUMENT UNDERSTANDING RULES
|
| 35 |
+
ββββββββββββββββββββββ
|
| 36 |
+
When a document is retrieved using RAG:
|
| 37 |
+
|
| 38 |
+
1. DO NOT copy or list raw document text.
|
| 39 |
+
2. DO NOT dump bullet points directly from the document.
|
| 40 |
+
3. ALWAYS interpret, summarize, and rephrase the information in your own words.
|
| 41 |
+
4. Focus on explaining the main ideas clearly and concisely.
|
| 42 |
+
5. Group related points together logically.
|
| 43 |
+
6. If the user asks βwhat is this document aboutβ, provide:
|
| 44 |
+
- A short overview
|
| 45 |
+
- Key themes or sections
|
| 46 |
+
- The purpose of the document
|
| 47 |
+
|
| 48 |
+
Example good response:
|
| 49 |
+
βThis document describes a set of AI projects focused on machine learning, deployment, and automation. It highlights work onβ¦β
|
| 50 |
+
|
| 51 |
+
ββββββββββββββββββββββ
|
| 52 |
+
πΉ TOOL USAGE PRIORITY
|
| 53 |
+
ββββββββββββββββββββββ
|
| 54 |
+
- Use the RAG tool when document knowledge is required.
|
| 55 |
+
- Never hallucinate content not present in retrieved data.
|
| 56 |
+
- If information is missing, say so clearly.
|
| 57 |
+
|
| 58 |
+
ββββββββββββββββββββββ
|
| 59 |
+
πΉ RESPONSE STYLE
|
| 60 |
+
ββββββββββββββββββββββ
|
| 61 |
+
- Be natural, concise, and human-like.
|
| 62 |
+
- Avoid technical repetition unless necessary.
|
| 63 |
+
- Avoid listing unless it improves clarity.
|
| 64 |
+
|
| 65 |
+
ββββββββββββββββββββββ
|
| 66 |
+
πΉ IDENTITY
|
| 67 |
+
ββββββββββββββββββββββ
|
| 68 |
+
You are the official AI assistant of Junaidβs AI platform.
|
| 69 |
+
Your job is to make complex information easy to understand.
|
| 70 |
+
"""
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# =====================================================
|
| 76 |
+
# 4οΈβ£ STATE
|
| 77 |
+
# =====================================================
|
| 78 |
+
|
| 79 |
+
class ChatState(TypedDict):
|
| 80 |
+
messages: Annotated[list[BaseMessage], add_messages]
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
# =====================================================
|
| 84 |
+
# 5οΈβ£ LLM + TOOLS
|
| 85 |
+
# =====================================================
|
| 86 |
+
|
| 87 |
+
llm = ChatOpenAI(
|
| 88 |
+
model="gpt-4.1-nano",
|
| 89 |
+
temperature=0.4,
|
| 90 |
+
streaming=True
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
rag_tool = create_rag_tool()
|
| 94 |
+
|
| 95 |
+
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]
|
| 96 |
+
llm = llm.bind_tools(tools)
|
| 97 |
+
tool_node = ToolNode(tools)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
# =====================================================
|
| 101 |
+
# 6οΈβ£ CHAT NODE
|
| 102 |
+
# =====================================================
|
| 103 |
+
|
| 104 |
+
def chatbot(state: ChatState):
|
| 105 |
+
messages = [SYSTEM_PROMPT] + state["messages"]
|
| 106 |
+
response = llm.invoke(messages)
|
| 107 |
+
return {"messages": [response]}
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
# =====================================================
|
| 112 |
+
# 7οΈβ£ GRAPH
|
| 113 |
+
# =====================================================
|
| 114 |
+
memory = MemorySaver()
|
| 115 |
+
graph = StateGraph(ChatState)
|
| 116 |
+
|
| 117 |
+
graph.add_node("chat", chatbot)
|
| 118 |
+
graph.add_node("tools", tool_node)
|
| 119 |
+
|
| 120 |
+
graph.add_edge(START, "chat")
|
| 121 |
+
graph.add_conditional_edges("chat", tools_condition)
|
| 122 |
+
graph.add_edge("tools", "chat")
|
| 123 |
+
graph.add_edge("chat", END)
|
| 124 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
app = graph.compile(checkpointer=memory)
|