rvdbra commited on
Commit
7d4eb2b
·
verified ·
1 Parent(s): ae08c59

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +147 -173
app.py CHANGED
@@ -1,231 +1,205 @@
 
 
1
  import getpass
2
  import os
 
 
3
 
4
- if not os.environ.get("OPENAI_API_KEY"):
5
- os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter API key for OpenAI: ")
 
6
 
7
  from langchain.chat_models import init_chat_model
8
-
9
- llm = init_chat_model("gpt-4.1", model_provider="openai", temperature=0.0)
10
-
11
- if not os.environ.get("OPENAI_API_KEY"):
12
- os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter API key for OpenAI: ")
13
-
14
  from langchain_openai import OpenAIEmbeddings
15
-
16
- embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
17
-
18
  from langchain_pinecone import PineconeVectorStore
19
  from pinecone import Pinecone
 
 
 
 
 
20
 
21
- pinecone_api_key = os.environ.get("PINECONE_API_KEY")
 
22
 
23
- if not pinecone_api_key:
24
- raise ValueError("Missing Pinecone API key in environment variables")
 
 
25
 
26
- pc = Pinecone(api_key=pinecone_api_key)
27
- index = pc.Index("natstudy")
 
28
 
 
 
 
29
  vector_store = PineconeVectorStore(embedding=embeddings, index=index)
30
 
31
- os.environ["LANGSMITH_TRACING"] = "true"
32
- if not os.environ.get("LANGSMITH_API_KEY"):
33
- os.environ["LANGSMITH_API_KEY"] = getpass.getpass()
34
 
35
- from langgraph.graph import MessagesState, StateGraph
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- graph_builder = StateGraph(MessagesState)
38
-
39
- from langchain_core.tools import tool
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  @tool(response_format="content_and_artifact")
43
  def retrieve(query: str):
44
- """Retrieve information related to a query."""
45
  retrieved_docs = vector_store.similarity_search(query, k=5)
46
  serialized = "\n\n".join(
47
- (f"Source: {doc.metadata}\n" f"Content: {doc.page_content}")
48
- for doc in retrieved_docs
49
  )
50
  return serialized, retrieved_docs
51
 
52
- from langchain_core.messages import SystemMessage
53
- from langgraph.prebuilt import ToolNode
54
-
55
- # Create unique ID per user
56
- import uuid
57
  def generate_user_id():
58
- return "user_" + uuid.uuid4().hex[:6]
59
-
60
- # System message
61
- SYSTEM_MESSAGE_TEXT = (
62
- "You are Einbürgerungs-Helfer, a highly-skilled, friendly, and helpful administrative assistant in Germany. "
63
- "You help people who are eligible for naturalization but haven't applied yet. "
64
- "Guide users to resources; do not fill in applications for them. "
65
- "You must provide accurate, concise answers in simple language. "
66
- "Only speak German, English, Turkish, or Arabic (use Einfaches Deutsch for German). "
67
- "Never hallucinate, never be discriminatory or toxic, and always protect users' privacy. "
68
- "Always assume the context is Germany. If the user's query requires"
69
- "information on citizenship, call the retrieve tool to fetch relevant documents before answering."
70
- )
71
 
72
- # Step 1: Generate an AIMessage that may include a tool-call to be sent.
73
  def query_or_respond(state: MessagesState):
74
- """Generate tool call for retrieval or respond."""
75
- # Prepare the system message
76
  system_message = SystemMessage(SYSTEM_MESSAGE_TEXT)
77
-
78
- # Filter existing messages (remove older system messages)
79
- filtered_messages = [
80
- msg for msg in state["messages"]
81
- if msg.type != "system"
82
- ]
83
-
84
- # Inject system message at the beginning
85
- prompt = [system_message] + filtered_messages
86
-
87
-
88
- # Bind tool and invoke
89
  llm_with_tools = llm.bind_tools([retrieve])
90
  response = llm_with_tools.invoke(prompt)
91
-
92
- # Logging
93
- user_id = state.get("user_id", "unknown_user")
94
- user_question = [m.content for m in state["messages"] if m.type == "human"][-1]
95
- answer_text = response.content
96
-
97
- print("[Debug] Attempting to log interaction...")
98
 
99
- # 🐛 DEBUG LOGGING (Add here!)
100
  user_id = state.get("user_id", "unknown_user")
101
  user_question = [m.content for m in state["messages"] if m.type == "human"][-1]
102
  answer_text = response.content
 
103
 
104
- print(f"[Debug] user_id: {user_id}")
105
- print(f"[Debug] question: {user_question}")
106
- print(f"[Debug] answer: {answer_text}")
107
-
108
- log_interaction_cloud(user_id, user_question, answer_text)
109
-
110
- return {"messages": [response]}
111
-
112
-
113
-
114
- # Step 2: Execute the retrieval.
115
- tools = ToolNode([retrieve])
116
 
117
- # Step 3: Generate a response using the retrieved content.
118
  def generate(state: MessagesState):
119
- """Generate answer."""
120
- # Get generated ToolMessages
121
- recent_tool_messages = []
122
- for message in reversed(state["messages"]):
123
- if message.type == "tool":
124
- recent_tool_messages.append(message)
125
- else:
126
- break
127
- tool_messages = recent_tool_messages[::-1]
128
-
129
- # Format system message with docs into prompt
130
- docs_content = "\n\n".join(doc.content for doc in tool_messages)
131
- system_message_content = SYSTEM_MESSAGE_TEXT + "\n\n" + docs_content
132
-
133
- # Filter out old system messages, only keep human and AI (non-tool)
134
- conversation_messages = [
135
- msg for msg in state["messages"]
136
- if msg.type in ("human", "ai") and not getattr(msg, "tool_calls", False)
137
- ]
138
-
139
-
140
- conversation_messages = [
141
- message
142
- for message in state["messages"]
143
- if message.type in ("human", "system")
144
- or (message.type == "ai" and not message.tool_calls)
145
- ]
146
- prompt = [SystemMessage(system_message_content)] + conversation_messages
147
-
148
-
149
- # Run
150
  response = llm.invoke(prompt)
151
- return {"messages": [response]}
152
 
153
- from langgraph.graph import END
154
- from langgraph.prebuilt import ToolNode, tools_condition
 
 
 
155
 
 
 
156
  graph_builder.add_node(query_or_respond)
157
- graph_builder.add_node(tools)
158
  graph_builder.add_node(generate)
159
 
160
  graph_builder.set_entry_point("query_or_respond")
161
- graph_builder.add_conditional_edges(
162
- "query_or_respond",
163
- tools_condition,
164
- {END: END, "tools": "tools"},
165
- )
166
  graph_builder.add_edge("tools", "generate")
167
  graph_builder.add_edge("generate", END)
168
-
169
  graph = graph_builder.compile()
170
 
171
- input_message = "How can i get citizenship in Germany if i have been arrested?"
172
-
173
- for step in graph.stream(
174
- {"messages": [{"role": "user", "user_id": generate_user_id(),"content": input_message}]},
175
- stream_mode="values",
176
- ):
177
- step["messages"][-1].pretty_print()
178
- for step in graph.stream(
179
- {"messages": [{"role": "user", "user_id": generate_user_id(),"content": input_message}]},
180
- stream_mode="values",
181
- ):
182
- step["messages"][-1].pretty_print()
183
- from supabase import create_client, Client
184
- SUPABASE_URL = os.environ.get("SUPABASE_URL") or input("Supabase URL: ")
185
- SUPABASE_KEY = os.environ.get("SUPABASE_KEY") or input("Supabase Service Role Key: ")
186
-
187
- supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
188
-
189
- from datetime import datetime
190
 
191
- def log_interaction_cloud(user_id: str, question: str, answer: str):
192
- response = supabase.table("logs").insert({
193
- "user_id": user_id,
194
- "question": question,
195
- "answer": answer,
196
- "timestamp": datetime.utcnow().isoformat()
197
- }).execute()
198
-
199
- if response.status_code != 201:
200
- print(f"[Log Error] Could not store log: {response.data}")
201
- else:
202
- print(f"[Log] Interaction saved for user {user_id}")
203
-
204
- def chat_with_bot(user_input, user_id="anonymous"):
205
- # Prepare state for your chatbot graph
206
  state = {
207
  "messages": [{"role": "user", "content": user_input}],
208
  "user_id": user_id,
209
  }
210
- responses = []
211
  for step in graph.stream(state, stream_mode="values"):
212
- responses = step["messages"]
213
-
214
- answer = responses[-1].content if responses else "Sorry, no answer."
215
- return answer
216
-
217
- import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
 
219
- iface = gr.Interface(
220
- fn=chat_with_bot,
221
- inputs=[
222
- gr.Textbox(lines=2, placeholder="Type your question here..."),
223
- gr.Textbox(value="anonymous", label="User ID (optional)")
224
- ],
225
- outputs="text",
226
- title="Einbürgerungs-Helfer Chatbot",
227
- description="Ask about German naturalization. Enter your question and optionally a user ID."
228
- )
229
 
230
- iface.launch(share=True)
231
 
 
1
+ # app.py
2
+
3
  import getpass
4
  import os
5
+ import uuid
6
+ from datetime import datetime
7
 
8
+ import gradio as gr
9
+ from dotenv import load_dotenv
10
+ from supabase import create_client, Client
11
 
12
  from langchain.chat_models import init_chat_model
 
 
 
 
 
 
13
  from langchain_openai import OpenAIEmbeddings
 
 
 
14
  from langchain_pinecone import PineconeVectorStore
15
  from pinecone import Pinecone
16
+ from langchain.memory import ConversationBufferWindowMemory
17
+ from langchain_core.tools import tool
18
+ from langchain_core.messages import SystemMessage
19
+ from langgraph.graph import END, MessagesState, StateGraph
20
+ from langgraph.prebuilt import ToolNode, tools_condition
21
 
22
+ # Load environment variables
23
+ load_dotenv()
24
 
25
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
26
+ SUPABASE_URL = os.getenv("SUPABASE_URL")
27
+ SUPABASE_KEY = os.getenv("SUPABASE_KEY")
28
+ PINECONE_API_KEY = os.getenv("PINECONE_API_KEY")
29
 
30
+ # Models
31
+ llm = init_chat_model("gpt-4.1-2025-04-14", model_provider="openai", temperature=0.0)
32
+ embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
33
 
34
+ # Vector store
35
+ pc = Pinecone(api_key=PINECONE_API_KEY)
36
+ index = pc.Index("natstudy")
37
  vector_store = PineconeVectorStore(embedding=embeddings, index=index)
38
 
39
+ # Supabase logging
40
+ supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
 
41
 
42
+ def log_interaction_cloud(user_id: str, question: str, answer: str):
43
+ try:
44
+ payload = {
45
+ "user_id": user_id,
46
+ "question": question,
47
+ "answer": answer,
48
+ "timestamp": datetime.utcnow().isoformat(),
49
+ }
50
+ res = supabase.table("logs").insert(payload).execute()
51
+ if hasattr(res, "status_code") and res.status_code >= 400:
52
+ print(f"[Supabase Error #{res.status_code}]:", res.data)
53
+ else:
54
+ print("[Supabase Success]:", res.data)
55
+ except Exception as e:
56
+ print("[Supabase Exception]", e)
57
 
58
+ # Conversation memory
59
+ memory = ConversationBufferWindowMemory(memory_key="chat_history", return_messages=True, k=5)
 
60
 
61
+ SYSTEM_MESSAGE_TEXT = (
62
+ "You are “Einbürgerungs-Helfer”, a highly skilled, friendly, and helpful administrative"
63
+ " assistant in Germany. Your only task is to support people who are eligible for naturalization"
64
+ " but have not yet applied. You only answer questions related to the current naturalization"
65
+ " process in Germany. You do not fill in applications or help users apply. You guide users "
66
+ "to relevant official resources by calling the retrieval tool to fetch appropriate documents."
67
+ "Any question involving citizenship, naturalization, applying for German nationality, duration "
68
+ "of residency, language requirements, or documents required MUST be interpreted as relevant and"
69
+ " MUST trigger the retrieval tool and never rely on your pre-trained knowledge. Your"
70
+ "answers must always be accurate, concise, and easy to understand. You are only allowed"
71
+ "to answer in German, English, Turkish, or Arabic. If a user writes in a different language"
72
+ " or requests another language, reply with: “Leider kann ich nur in den folgenden Sprachen"
73
+ " lesen und schreiben: Deutsch, Englisch, Türkisch oder Arabisch. In welcher Sprache möchten"
74
+ " Sie weitermachen?” and wait for the user to choose a supported language. You must always"
75
+ " answer in the chosen language. Never hallucinate, never go beyond the topic of naturalization,"
76
+ " and never share false information. If a user asks a question that is unrelated to naturalizatio"
77
+ " in Germany, reply in the user's language with: “I can only answer questions related to the"
78
+ " process of naturalization in Germany. Please provide questions on this topic.” If you do not"
79
+ " know how to answer, respond with: “I do not have enough knowledge to answer your question"
80
+ " accurately. Please email your question at SBD-Umfrage@polsoz.fu-berlin.de, and we will answer"
81
+ " your inquiry as soon as possible.” If a user uses toxic or offensive language, kindly ask them"
82
+ " to rephrase their question and explain that this behavior is not acceptable."
83
+ )
84
 
85
  @tool(response_format="content_and_artifact")
86
  def retrieve(query: str):
 
87
  retrieved_docs = vector_store.similarity_search(query, k=5)
88
  serialized = "\n\n".join(
89
+ (f"Source: {doc.metadata}\n" f"Content: {doc.page_content}") for doc in retrieved_docs
 
90
  )
91
  return serialized, retrieved_docs
92
 
 
 
 
 
 
93
  def generate_user_id():
94
+ return "user_" + uuid.uuid4().hex[:6]
 
 
 
 
 
 
 
 
 
 
 
 
95
 
 
96
  def query_or_respond(state: MessagesState):
 
 
97
  system_message = SystemMessage(SYSTEM_MESSAGE_TEXT)
98
+ raw_history = memory.load_memory_variables({})["chat_history"]
99
+ OFF_TOPIC_RESPONSE_SNIPPET = "I can only answer questions related to the process of naturalization in Germany"
100
+ filtered_history = [msg for msg in raw_history if not (msg.type == "ai" and OFF_TOPIC_RESPONSE_SNIPPET in msg.content)]
101
+ history = filtered_history[-5:]
102
+ prompt = [system_message] + history
 
 
 
 
 
 
 
103
  llm_with_tools = llm.bind_tools([retrieve])
104
  response = llm_with_tools.invoke(prompt)
 
 
 
 
 
 
 
105
 
 
106
  user_id = state.get("user_id", "unknown_user")
107
  user_question = [m.content for m in state["messages"] if m.type == "human"][-1]
108
  answer_text = response.content
109
+ memory.save_context({"input": user_question}, {"output": answer_text})
110
 
111
+ if not getattr(response, "tool_calls", None):
112
+ log_interaction_cloud(user_id, user_question, answer_text)
113
+ return {"messages": state["messages"] + [response]}
 
 
 
 
 
 
 
 
 
114
 
 
115
  def generate(state: MessagesState):
116
+ tool_messages = [msg for msg in reversed(state["messages"]) if msg.type == "tool"]
117
+ docs_content = "\n\n".join(msg.content for msg in reversed(tool_messages))
118
+ system_prompt = SYSTEM_MESSAGE_TEXT + ("\n\n" + docs_content if docs_content else "")
119
+ conversation_messages = [msg for msg in state["messages"] if msg.type == "human" or (msg.type == "ai" and not getattr(msg, "tool_calls", None))]
120
+ recent_context = conversation_messages[-6:]
121
+ prompt = [SystemMessage(system_prompt)] + recent_context
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  response = llm.invoke(prompt)
 
123
 
124
+ user_id = state.get("user_id", "unknown_user")
125
+ question = [m.content for m in state["messages"] if m.type == "human"][-1]
126
+ answer = response.content
127
+ log_interaction_cloud(user_id, question, answer)
128
+ return {"messages": state["messages"] + [response]}
129
 
130
+ # LangGraph setup
131
+ graph_builder = StateGraph(MessagesState)
132
  graph_builder.add_node(query_or_respond)
133
+ graph_builder.add_node(ToolNode([retrieve]))
134
  graph_builder.add_node(generate)
135
 
136
  graph_builder.set_entry_point("query_or_respond")
137
+ graph_builder.add_conditional_edges("query_or_respond", tools_condition, {END: END, "tools": "tools"})
 
 
 
 
138
  graph_builder.add_edge("tools", "generate")
139
  graph_builder.add_edge("generate", END)
 
140
  graph = graph_builder.compile()
141
 
142
+ # Gradio Interface
143
+ chat_histories = {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
 
145
+ def stream_response(user_input, user_id):
146
+ if not user_id:
147
+ user_id = "anonymous"
 
 
 
 
 
 
 
 
 
 
 
 
148
  state = {
149
  "messages": [{"role": "user", "content": user_input}],
150
  "user_id": user_id,
151
  }
152
+ collected = ""
153
  for step in graph.stream(state, stream_mode="values"):
154
+ message = step["messages"][-1].content
155
+ for token in message:
156
+ collected += token
157
+ yield collected
158
+
159
+ def respond(user_message, chat_history, user_id):
160
+ if user_id not in chat_histories:
161
+ chat_histories[user_id] = []
162
+ response_generator = stream_response(user_message, user_id)
163
+ full_response = ""
164
+ for partial in response_generator:
165
+ full_response = partial
166
+ yield chat_history + [[user_message, full_response]]
167
+ chat_histories[user_id].append([user_message, full_response])
168
+
169
+ def clear_history():
170
+ return "", []
171
+
172
+ description = """
173
+ ### Was ist **Einbürgerungs-Helfer**?
174
+ **Deutsch:** Einbürgerungs-Helfer hilft dir, den Weg zur deutschen Staatsangehörigkeit zu verstehen – einfach, mehrsprachig, datenschutzfreundlich.
175
+ **English:** Einbürgerungs-Helfer helps you understand how to get German citizenship – simple, multilingual, and privacy-friendly.
176
+ **Türkçe:** Einbürgerungs-Helfer, Almanya vatandaşlığına giden yolu anlamanıza yardımcı olur – basit, çok dilli ve gizliliğe saygılı.
177
+ **العربية:** يساعدك "Einbürgerungs-Helfer" على فهم كيفية الحصول على الجنسية الألمانية - بطريقة بسيطة، متعددة اللغات وتحترم الخصوصية.
178
+ """
179
+
180
+ with gr.Blocks(
181
+ theme=gr.themes.Soft(primary_hue="blue"),
182
+ css="""* { font-family: 'Noto Sans', sans-serif !important; }"""
183
+ ) as iface:
184
+ gr.Markdown(f"# Einbürgerungs-Helfer\n{description}")
185
+ with gr.Row():
186
+ user_id_input = gr.Textbox(label="Benutzer-ID (User ID)", placeholder="z.B. user_123", lines=1)
187
+ clear_btn = gr.Button("🗑️ Verlauf löschen")
188
+
189
+ chatbot = gr.Chatbot(
190
+ label="Chat",
191
+ show_copy_button=True,
192
+ value=[[None,
193
+ "Ich bin hier, um Ihnen bei allen Fragen zur Einbürgerung in Deutschland zu helfen.\n"
194
+ "Sie können Ihre Frage auf Deutsch, Englisch, Türkisch oder Arabisch stellen.\n"
195
+ "Lassen Sie uns loslegen – wie kann ich Ihnen heute helfen?"]]
196
+ )
197
+ msg_input = gr.Textbox(placeholder="Frage eingeben...", label="Deine Nachricht")
198
+ with gr.Row():
199
+ submit_btn = gr.Button("Senden", variant="primary")
200
+ submit_btn.click(fn=respond, inputs=[msg_input, chatbot, user_id_input], outputs=chatbot)
201
+ clear_btn.click(fn=clear_history, inputs=[], outputs=[user_id_input, chatbot])
202
 
203
+ iface.launch()
 
 
 
 
 
 
 
 
 
204
 
 
205