ahmad-agilekode commited on
Commit
ede60f4
·
verified ·
1 Parent(s): 8630e19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -16
app.py CHANGED
@@ -1,12 +1,13 @@
1
  import os
2
  import uuid
 
3
  from datetime import datetime
4
  from dotenv import load_dotenv
5
  load_dotenv()
6
 
7
  from supabase import create_client
8
  from langchain_core.runnables import RunnableConfig
9
- from typing import TypedDict, List, Optional, Annotated
10
  from langchain_core.messages import BaseMessage, AIMessage, HumanMessage, SystemMessage
11
  from langgraph.graph.message import add_messages
12
  from langgraph.graph import StateGraph, END
@@ -15,6 +16,7 @@ from langchain.chat_models import init_chat_model
15
  from langchain_pinecone import Pinecone as LangchainPinecone
16
  from langchain_core.chat_history import BaseChatMessageHistory
17
  from langchain_core.messages import messages_from_dict, messages_to_dict
 
18
 
19
  # --- Supabase Setup ---
20
  SUPABASE_URL = os.getenv("SUPABASE_URL")
@@ -27,10 +29,25 @@ class CustomState(TypedDict):
27
  user_id: str
28
  chat_history: Optional[List[List[str]]]
29
 
30
- # --- AI Model ---
 
 
 
 
 
 
 
31
  if not os.environ.get("OPENAI_API_KEY"):
32
  os.environ["OPENAI_API_KEY"] = input("Enter API key for OpenAI: ")
33
- llm = init_chat_model("gpt-4.1", model_provider="openai", temperature=0.3)
 
 
 
 
 
 
 
 
34
 
35
  # --- Vector Store ---
36
  pinecone_api_key = os.getenv("PINECONE_API_KEY", "").strip()
@@ -92,32 +109,28 @@ def log_interaction_cloud(user_id: str, question: str, answer: str):
92
 
93
  # --- Retrieval ---
94
  def retrieve(query: str):
95
- retrieved_docs = vector_store.similarity_search(query, k=6)
96
  serialized = "\n\n".join(
97
  (f"[Source]({doc.metadata.get('source', '')}):\n{doc.page_content}")
98
  for doc in retrieved_docs
99
  )
100
  return serialized, retrieved_docs
101
 
102
- # --- System Message ---
103
  SYSTEM_MESSAGE_TEXT = (
104
  "You are Einbürgerungs-Helfer, a highly skilled, friendly, and helpful administrative assistant for naturalization matters in Germany."
105
  " You only support the following languages: German, English, Turkish, and Arabic."
106
  " You MUST always respond in the language of the user's last message."
107
  " If a user sends a message in an unsupported language, politely inform them with the following message:"
108
  " 'Bitte stellen Sie Ihre Frage auf Deutsch, Englisch, Türkisch oder Arabisch.'"
109
-
110
  " Your sole purpose is to assist individuals who are eligible for German naturalization but have not yet applied."
111
  " You only answer questions directly related to the current naturalization process in Germany."
112
  " You do not assist with completing applications or submitting them."
113
  " Your answers must be based solely on the provided context. Do NOT generate answers from your own knowledge."
114
  " Your responses must always be accurate, concise, and easy to understand."
115
- " Include direct relevant source link in your answer at the end , where necessary."
116
  " Never hallucinate, never go beyond the topic of naturalization, and never share information that is false or misleading."
117
-
118
  " If you do not have enough knowledge to answer a question based on the provided context, respond with:"
119
  " 'I do not have enough knowledge to answer your question accurately. Please email your question to SBD-Umfrage@polsoz.fu-berlin.de, and we will respond as soon as possible.'"
120
-
121
  " If a user uses toxic, offensive, or inappropriate language, kindly ask them to rephrase their question"
122
  " and explain that such behavior is not acceptable."
123
  )
@@ -125,27 +138,102 @@ SYSTEM_MESSAGE_TEXT = (
125
  def generate_user_id():
126
  return "user_" + uuid.uuid4().hex[:6]
127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  def query_or_respond(state: CustomState) -> CustomState:
129
  user_id = state.get("user_id") or generate_user_id()
130
  human_messages = [m for m in state["messages"] if m.type == "human"]
131
  latest_user_msg = human_messages[-1]
132
 
133
  memory = store.get_memory(user_id)
134
- history = memory.messages[-5:]
 
 
 
 
 
 
 
 
 
 
135
 
136
- retrieved_text, _ = retrieve(latest_user_msg.content)
137
- system_msg = SystemMessage(SYSTEM_MESSAGE_TEXT)
138
 
139
- # 2. Retrieved context as assistant message
140
- system_msg = SystemMessage(SYSTEM_MESSAGE_TEXT + "\n\nStrictly use the following documents to answer the question. If helpful, include direct source links in your answer: " + retrieved_text)
141
- prompt = [system_msg] + history + [latest_user_msg]
 
 
 
 
 
 
 
142
  response = llm.invoke(prompt)
143
 
144
  print("The retrieved text is ", retrieved_text)
145
 
146
  memory.add_user_message(latest_user_msg.content)
147
  memory.add_ai_message(response.content)
148
-
149
  log_interaction_cloud(user_id, latest_user_msg.content, response.content)
150
 
151
  return {**state, "messages": [response], "user_id": user_id}
 
1
  import os
2
  import uuid
3
+ import json
4
  from datetime import datetime
5
  from dotenv import load_dotenv
6
  load_dotenv()
7
 
8
  from supabase import create_client
9
  from langchain_core.runnables import RunnableConfig
10
+ from typing import TypedDict, List, Optional, Annotated, Literal
11
  from langchain_core.messages import BaseMessage, AIMessage, HumanMessage, SystemMessage
12
  from langgraph.graph.message import add_messages
13
  from langgraph.graph import StateGraph, END
 
16
  from langchain_pinecone import Pinecone as LangchainPinecone
17
  from langchain_core.chat_history import BaseChatMessageHistory
18
  from langchain_core.messages import messages_from_dict, messages_to_dict
19
+ from pydantic import BaseModel, Field
20
 
21
  # --- Supabase Setup ---
22
  SUPABASE_URL = os.getenv("SUPABASE_URL")
 
29
  user_id: str
30
  chat_history: Optional[List[List[str]]]
31
 
32
+ # --- Router Structured Output Schema (SINGLE MODEL) ---
33
+ class RouterDecision(BaseModel):
34
+ action: Literal["reply", "retrieve"]
35
+ language: Literal["de", "en", "tr", "ar"]
36
+ text: Optional[str] = None
37
+ normalized_query: Optional[str] = None
38
+
39
+ # --- AI Models ---
40
  if not os.environ.get("OPENAI_API_KEY"):
41
  os.environ["OPENAI_API_KEY"] = input("Enter API key for OpenAI: ")
42
+
43
+ llm = init_chat_model("gpt-4.1", model_provider="openai", temperature=0.5)
44
+
45
+ # router with structured output using the single model
46
+ _router_llm_base = init_chat_model("gpt-4.1", model_provider="openai", temperature=0.5)
47
+ router_llm = _router_llm_base.with_structured_output(
48
+ schema=RouterDecision,
49
+ method="json_schema",
50
+ )
51
 
52
  # --- Vector Store ---
53
  pinecone_api_key = os.getenv("PINECONE_API_KEY", "").strip()
 
109
 
110
  # --- Retrieval ---
111
  def retrieve(query: str):
112
+ retrieved_docs = vector_store.similarity_search(query, k=4)
113
  serialized = "\n\n".join(
114
  (f"[Source]({doc.metadata.get('source', '')}):\n{doc.page_content}")
115
  for doc in retrieved_docs
116
  )
117
  return serialized, retrieved_docs
118
 
 
119
  SYSTEM_MESSAGE_TEXT = (
120
  "You are Einbürgerungs-Helfer, a highly skilled, friendly, and helpful administrative assistant for naturalization matters in Germany."
121
  " You only support the following languages: German, English, Turkish, and Arabic."
122
  " You MUST always respond in the language of the user's last message."
123
  " If a user sends a message in an unsupported language, politely inform them with the following message:"
124
  " 'Bitte stellen Sie Ihre Frage auf Deutsch, Englisch, Türkisch oder Arabisch.'"
 
125
  " Your sole purpose is to assist individuals who are eligible for German naturalization but have not yet applied."
126
  " You only answer questions directly related to the current naturalization process in Germany."
127
  " You do not assist with completing applications or submitting them."
128
  " Your answers must be based solely on the provided context. Do NOT generate answers from your own knowledge."
129
  " Your responses must always be accurate, concise, and easy to understand."
130
+ " Include direct relevant link in your answer at the end, where necessary."
131
  " Never hallucinate, never go beyond the topic of naturalization, and never share information that is false or misleading."
 
132
  " If you do not have enough knowledge to answer a question based on the provided context, respond with:"
133
  " 'I do not have enough knowledge to answer your question accurately. Please email your question to SBD-Umfrage@polsoz.fu-berlin.de, and we will respond as soon as possible.'"
 
134
  " If a user uses toxic, offensive, or inappropriate language, kindly ask them to rephrase their question"
135
  " and explain that such behavior is not acceptable."
136
  )
 
138
  def generate_user_id():
139
  return "user_" + uuid.uuid4().hex[:6]
140
 
141
+ ROUTER_SCHEMA = """
142
+ Return ONLY one of the following JSON objects (no prose):
143
+
144
+ 1) For generic chitchat/query OR for unsupported language OR toxic/offensive content:
145
+ {
146
+ "action": "reply",
147
+ "language": "de|en|tr|ar",
148
+ "text": "final reply to this provided generic chitchat/query (concise and friendly) OR toxic/offensive message"
149
+ }
150
+
151
+ 2) For questions that require knowledge about German naturalization and thus retrieval:
152
+ {
153
+ "action": "retrieve",
154
+ "language": "de|en|tr|ar",
155
+ "normalized_query": "concise query to use for retrieval"
156
+ }
157
+ """
158
+
159
+ ROUTER_SYS = (
160
+ "You are a router for a German naturalization assistant.\n"
161
+ "- Supported languages: German (de), English (en), Turkish (tr), Arabic (ar). Detect the user's language and set it.\n"
162
+ "- If the message is generic chitchat/query, produce a friendly, concise direct reply.\n"
163
+ "- If the message is toxic/offensive, produce a polite request to rephrase.\n"
164
+ "- If the language is unsupported, politely ask the user to use one of the supported languages.\n"
165
+ "- ONLY if the user asks something about German naturalization that needs factual answering, return action 'retrieve'.\n"
166
+ "- Never attempt to answer naturalization content yourself — only the final RAG step can do that.\n"
167
+ + ROUTER_SCHEMA
168
+ )
169
+
170
+ # --- STRICT Router (Structured Output + Retry + Runtime guards) ---
171
+ def route_or_reply(user_text: str, prior_msgs: List[BaseMessage]) -> dict:
172
+ history_slice = prior_msgs[-4:] if prior_msgs else []
173
+ msgs = [SystemMessage(ROUTER_SYS)] + history_slice + [HumanMessage(user_text)]
174
+
175
+ last_err = None
176
+ for attempt in range(2):
177
+ try:
178
+ obj = router_llm.invoke(msgs)
179
+ data = obj.model_dump()
180
+
181
+ action = data.get("action")
182
+ if action == "reply":
183
+ if not data.get("text"):
184
+ raise ValueError("RouterDecision 'reply' requires 'text'.")
185
+ data.pop("normalized_query", None)
186
+ elif action == "retrieve":
187
+ if not data.get("normalized_query"):
188
+ raise ValueError("RouterDecision 'retrieve' requires 'normalized_query'.")
189
+ data.pop("text", None)
190
+ else:
191
+ raise ValueError(f"Unknown action: {action}")
192
+
193
+ return data
194
+ except Exception as e:
195
+ last_err = e
196
+ print(f"[Router Parse/Validation Error Attempt {attempt+1}]:", repr(e))
197
+
198
+ return {"action": "reply", "language": "en", "text": "Sorry, something went wrong. Please try again."}
199
+
200
+ # --- Main Node ---
201
  def query_or_respond(state: CustomState) -> CustomState:
202
  user_id = state.get("user_id") or generate_user_id()
203
  human_messages = [m for m in state["messages"] if m.type == "human"]
204
  latest_user_msg = human_messages[-1]
205
 
206
  memory = store.get_memory(user_id)
207
+ history = memory.messages
208
+
209
+ decision = route_or_reply(latest_user_msg.content, history)
210
+ print("[Router]", decision)
211
+
212
+ if decision.get("action") == "reply":
213
+ response = AIMessage(content=decision.get("text", ""))
214
+ memory.add_user_message(latest_user_msg.content)
215
+ memory.add_ai_message(response.content)
216
+ #log_interaction_cloud(user_id, latest_user_msg.content, response.content)
217
+ return {**state, "messages": [response], "user_id": user_id}
218
 
219
+ normalized_query = decision.get("normalized_query") or latest_user_msg.content
 
220
 
221
+ retrieved_text, _ = retrieve(normalized_query)
222
+
223
+ history_tail = history[-5:]
224
+ system_msg = SystemMessage(
225
+ SYSTEM_MESSAGE_TEXT
226
+ + "\n\nStrictly use the following documents to answer the question. "
227
+ "If helpful, include direct source links in your answer: "
228
+ + retrieved_text
229
+ )
230
+ prompt = [system_msg] + history_tail + [latest_user_msg]
231
  response = llm.invoke(prompt)
232
 
233
  print("The retrieved text is ", retrieved_text)
234
 
235
  memory.add_user_message(latest_user_msg.content)
236
  memory.add_ai_message(response.content)
 
237
  log_interaction_cloud(user_id, latest_user_msg.content, response.content)
238
 
239
  return {**state, "messages": [response], "user_id": user_id}