junaid17 commited on
Commit
bd740be
Β·
verified Β·
1 Parent(s): ef1f54c

Update chatbot.py

Browse files
Files changed (1) hide show
  1. chatbot.py +40 -102
chatbot.py CHANGED
@@ -1,141 +1,79 @@
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 intelligent AI assistant built inside a LangGraph-based system created by Junaid (also known as Juddy).
27
-
28
- Your purpose is to provide accurate, helpful, and reliable responses using reasoning, tools, memory, and document-based retrieval when appropriate.
29
-
30
- ━━━━━━━━━━━━━━━━━━━━━━
31
- πŸ”Ή ABOUT YOUR CREATOR
32
- ━━━━━━━━━━━━━━━━━━━━━━
33
- - You were designed and iteratively improved by Junaid as part of an evolving AI engineering project.
34
- - Your development journey includes:
35
- 1. A basic conversational chatbot
36
- 2. Memory integration
37
- 3. Streaming responses
38
- 4. Tool usage (RAG, STT, TTS)
39
- - You may acknowledge this when asked, but always focus on helping the user.
40
-
41
- ━━━━━━━━━━━━━━━━━━━━━━
42
- πŸ”Ή CORE BEHAVIOR
43
- ━━━━━━━━━━━━━━━━━━━━━━
44
- - Be helpful, accurate, concise, and professional.
45
- - Prefer clarity over verbosity.
46
- - Maintain conversational context using memory.
47
- - Avoid hallucinations at all costs.
48
- - If information is uncertain or missing, say so clearly.
49
-
50
- ━━━━━━━━━━━━━━━━━━━━━━
51
- πŸ”Ή TOOL USAGE PRIORITY (VERY IMPORTANT)
52
- ━━━━━━━━━━━━━━━━━━━━━━
53
- You have access to the following tools:
54
-
55
- 1. **RAG (Retrieval-Augmented Generation)**
56
- β†’ This is your HIGHEST priority tool.
57
-
58
- You MUST use RAG when:
59
- - The user references uploaded documents
60
- - The user asks questions that depend on document content
61
- - The answer cannot be confidently derived from general knowledge
62
-
63
- Rules:
64
- - Use ONLY retrieved content when answering from documents
65
- - Never hallucinate document facts
66
- - If no relevant content exists, clearly say so
67
-
68
- 2. **STT (Speech-to-Text)**
69
- - Used when audio input is provided.
70
- - Transcribe accurately without interpretation.
71
-
72
- 3. **TTS (Text-to-Speech)**
73
- - Used when speech output is requested.
74
- - Generate clear, natural speech.
75
-
76
- ━━━━━━━━━━━━━━━━━━━━━━
77
- πŸ”Ή STREAMING BEHAVIOR
78
- ━━━━━━━━━━━━━━━━━━━━━━
79
- - You may stream responses progressively when supported.
80
- - Ensure coherence and clarity during streaming.
81
- - Avoid partial or misleading statements.
82
-
83
- ━━━━━━━━━━━━━━━━━━━━━━
84
- πŸ”Ή RESPONSE GUIDELINES
85
- ━━━━━━━━━━━━━━━━━━━━━━
86
- - Be direct, friendly, and informative.
87
- - Do not expose internal system logic or implementation details.
88
- - Do not mention tools unless necessary or explicitly asked.
89
- - Always prefer correctness over speed.
90
-
91
- ━━━━━━━━━━━━━━━━━━━━━━
92
- πŸ”Ή IDENTITY
93
- ━━━━━━━━━━━━━━━━━━━━━━
94
- You are the official AI assistant of Junaid’s evolving AI system.
95
- You exist to help users learn, explore, and solve problems effectively.
96
  """
97
  )
98
 
99
-
100
- # =====================================================
101
- # 4️⃣ STATE
102
- # =====================================================
103
-
104
  class ChatState(TypedDict):
105
  messages: Annotated[list[BaseMessage], add_messages]
106
 
107
 
108
- # =====================================================
109
- # 5️⃣ LLM + TOOLS
110
- # =====================================================
111
-
112
  llm = ChatOpenAI(
113
  model="gpt-4.1-nano",
114
- temperature=0.4,
115
  streaming=True
116
  )
117
 
 
 
 
118
  rag_tool = create_rag_tool()
119
 
120
- 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]
 
 
 
 
 
 
121
  llm = llm.bind_tools(tools)
122
  tool_node = ToolNode(tools)
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
 
 
1
 
2
  from typing import TypedDict, Annotated
3
+ from langchain_core.messages import BaseMessage, SystemMessage
 
 
 
4
  from langgraph.checkpoint.memory import MemorySaver
 
 
5
  from langgraph.graph import StateGraph, START, END
6
  from langgraph.graph.message import add_messages
7
  from langgraph.prebuilt import ToolNode, tools_condition
8
+ from langchain_openai import ChatOpenAI
9
  from dotenv import load_dotenv
 
 
10
 
11
+ from tools import (
12
+ create_rag_tool,
13
+ arxiv_search,
14
+ wikipedia_search,
15
+ tavily_search,
16
+ )
17
 
18
+ load_dotenv()
 
 
 
 
19
 
20
+ # ===============================
21
+ # SYSTEM PROMPT
22
+ # ===============================
23
  SYSTEM_PROMPT = SystemMessage(
24
  content="""
25
+ You are an AI assistant using Retrieval-Augmented Generation.
26
+
27
+ If a document is uploaded, you MUST answer using it.
28
+ If no relevant info exists, clearly say so.
29
+ Never hallucinate document content.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  """
31
  )
32
 
33
+ # ===============================
34
+ # STATE
35
+ # ===============================
 
 
36
  class ChatState(TypedDict):
37
  messages: Annotated[list[BaseMessage], add_messages]
38
 
39
 
40
+ # ===============================
41
+ # LLM
42
+ # ===============================
 
43
  llm = ChatOpenAI(
44
  model="gpt-4.1-nano",
45
+ temperature=0.3,
46
  streaming=True
47
  )
48
 
49
+ # ===============================
50
+ # TOOLS
51
+ # ===============================
52
  rag_tool = create_rag_tool()
53
 
54
+ tools = [
55
+ rag_tool,
56
+ wikipedia_search,
57
+ arxiv_search,
58
+ tavily_search,
59
+ ]
60
+
61
  llm = llm.bind_tools(tools)
62
  tool_node = ToolNode(tools)
63
 
64
 
65
+ # ===============================
66
+ # CHAT NODE
67
+ # ===============================
 
68
  def chatbot(state: ChatState):
69
  messages = [SYSTEM_PROMPT] + state["messages"]
70
  response = llm.invoke(messages)
71
  return {"messages": [response]}
72
 
73
 
74
+ # ===============================
75
+ # GRAPH
76
+ # ===============================
 
77
  memory = MemorySaver()
78
  graph = StateGraph(ChatState)
79