File size: 14,705 Bytes
21c1ace 6b31ee3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | import os
import urllib.request
from dotenv import load_dotenv
import gradio as gr
import fasttext
# Download fastText model if not present
FASTTEXT_MODEL_PATH = "/tmp/lid.176.bin"
if not os.path.exists(FASTTEXT_MODEL_PATH):
print("Downloading fastText language identification model...")
urllib.request.urlretrieve(
"https://dl.fbaipublicfiles.com/fasttext/supervised-models/lid.176.bin",
FASTTEXT_MODEL_PATH
)
# Load environment variables
load_dotenv()
# Import your other dependencies
from langchain.memory import ConversationBufferWindowMemory
from langchain_openai import OpenAIEmbeddings
from langchain.chat_models import init_chat_model
from langchain_pinecone import Pinecone as LangchainPinecone
from supabase import create_client, Client
from datetime import datetime
from langgraph.graph import MessagesState, StateGraph, END
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
from langgraph.prebuilt import ToolNode, tools_condition
from langchain_core.tools import tool
# Load fastText model
fasttext_model = fasttext.load_model(FASTTEXT_MODEL_PATH)
SUPPORTED_LANGUAGES = {
"de": "German",
"en": "English",
"tr": "Turkish",
"ar": "Arabic"
}
def detect_supported_language(text):
try:
label, prob = fasttext_model.predict(text)
lang = label[0].replace("__label__", "")
if lang in SUPPORTED_LANGUAGES and prob[0] > 0.45:
return lang
return None
except Exception as e:
print("[fastText Exception]", e)
return None
# Set up OpenAI, Pinecone, Supabase, etc.
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
if not OPENAI_API_KEY:
raise RuntimeError("OPENAI_API_KEY must be set in your environment or .env file")
llm = init_chat_model("gpt-4.1-2025-04-14", model_provider="openai", temperature=0.0)
pinecone_api_key = os.getenv("PINECONE_API_KEY", "").strip()
pinecone_index_name = os.getenv("PINECONE_INDEX_NAME", "").strip()
embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
vector_store = LangchainPinecone(
index_name=pinecone_index_name,
embedding=embeddings,
pinecone_api_key=pinecone_api_key
)
SUPABASE_URL = os.environ.get("SUPABASE_URL")
SUPABASE_KEY = os.environ.get("SUPABASE_KEY")
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
def log_interaction_cloud(user_id: str, question: str, answer: str):
payload = {
"user_id": user_id,
"question": question,
"answer": answer,
"timestamp": datetime.utcnow().isoformat(),
}
try:
res = supabase.table("logs").insert(payload).execute()
except Exception as e:
print("[Supabase Exception]", e)
return
if hasattr(res, "status_code") and res.status_code >= 400:
print(f"[Supabase Error #{res.status_code}]:", res.data)
else:
print("[Supabase Success]:", res.data)
# LangGraph setup (your nodes, graph, etc.)
SYSTEM_MESSAGE_TEXT = (
"You are “Einbürgerungs-Helfer”, a highly skilled, friendly, and helpful administrative"
" assistant in Germany. Your only task is to support people who are eligible for naturalization"
" but have not yet applied. You only answer questions related to the current naturalization"
" process in Germany. You do not fill in applications or help users apply. You guide users "
"to relevant official resources by calling the retrieval tool to fetch appropriate documents."
"Any question involving citizenship, naturalization, applying for German nationality, duration "
"of residency, language requirements, or documents required MUST be interpreted as relevant and"
" MUST trigger the retrieval tool and never rely on your pre-trained knowledge. Your"
"answers must always be accurate, concise, and easy to understand. You must only answer in the user's"
"chosen language (German, English, Turkish, or Arabic). If a user writes in a different language"
" or requests another language, reply with: “Leider kann ich nur in den folgenden Sprachen"
" lesen und schreiben: Deutsch, Englisch, Türkisch oder Arabisch. In welcher Sprache möchten"
" Sie weitermachen?” and wait for the user to choose a supported language. You must always"
" answer in the chosen language. Never hallucinate, never go beyond the topic of naturalization,"
" and never share false information. If a user asks a question that is unrelated to naturalizatio"
" in Germany, reply in the user's language with: “I can only answer questions related to the"
" process of naturalization in Germany. Please provide questions on this topic.” If you do not"
" know how to answer, respond with: “I do not have enough knowledge to answer your question"
" accurately. Please email your question at SBD-Umfrage@polsoz.fu-berlin.de, and we will answer"
" your inquiry as soon as possible.” If a user uses toxic or offensive language, kindly ask them"
" to rephrase their question and explain that this behavior is not acceptable."
)
@tool(response_format="content_and_artifact")
def retrieve(query: str):
"""Retrieve information related to the last query."""
retrieved_docs = vector_store.similarity_search(query, k=5)
serialized = "\n\n".join(
(f"Source: {doc.metadata}\n" f"Content: {doc.page_content}")
for doc in retrieved_docs
)
return serialized, retrieved_docs
def query_or_respond(state: MessagesState):
latest_user_msg = [m for m in state["messages"] if m.type == "human"][-1]
language = state.get("language")
if not language:
detected_lang = detect_supported_language(latest_user_msg.content)
if not detected_lang:
return {
"messages": state["messages"] + [
AIMessage(content="Leider kann ich nur in den folgenden Sprachen lesen und schreiben: Deutsch, Englisch, Türkisch oder Arabisch. In welcher Sprache möchten Sie weitermachen?")
]}
else:
state["language"] = detected_lang
detected_lang = detect_supported_language(latest_user_msg.content)
if detected_lang and detected_lang != state.get("language"):
state["language"] = detected_lang
if not detected_lang:
return {
"messages": state["messages"] + [
AIMessage(content="Leider kann ich nur in den folgenden Sprachen lesen und schreiben: Deutsch, Englisch, Türkisch oder Arabisch. In welcher Sprache möchten Sie weitermachen?")
]
}
system_message = SystemMessage(SYSTEM_MESSAGE_TEXT + f"\n\nThe user is communicating in: {SUPPORTED_LANGUAGES[state['language']]}.")
raw_hist = []
if "chat_history" in state:
for pair in state["chat_history"]:
if len(pair) == 2:
raw_hist.append(HumanMessage(content=pair[0]))
raw_hist.append(AIMessage(content=pair[1]))
OFF_TOPIC_RESPONSE_SNIPPET = "I can only answer questions related to the process of naturalization in Germany"
filtered_history = [
msg for msg in raw_hist
if not (msg.type == "ai" and OFF_TOPIC_RESPONSE_SNIPPET in msg.content)
]
history = filtered_history[-5:] if filtered_history else []
latest_user_msg = [m for m in state["messages"] if m.type == "human"][-1]
prompt = [system_message] + history + [latest_user_msg]
llm_with_tools = llm.bind_tools([retrieve])
response = llm_with_tools.invoke(prompt)
if not getattr(response, "tool_calls", None):
user_id = state.get("user_id", "anonymous")
log_interaction_cloud(user_id, latest_user_msg.content, response.content)
return {"messages": state["messages"] + [response]}
tools_node = ToolNode([retrieve])
def generate(state: MessagesState):
language = state.get("language", "en")
system_prompt = SYSTEM_MESSAGE_TEXT + f"\n\nRespond in: {SUPPORTED_LANGUAGES[language]}.\n\n"
recent_tool_messages = []
for msg in reversed(state["messages"]):
if msg.type == "tool":
recent_tool_messages.append(msg)
else:
break
tool_messages = recent_tool_messages[::-1]
docs_content = "\n\n".join(msg.content for msg in tool_messages)
system_prompt = SYSTEM_MESSAGE_TEXT + ("\n\n" + docs_content if docs_content else "")
MAX_CONTEXT_MESSAGES = 5
conversation_messages = []
for msg in state["messages"]:
if msg.type == "human":
conversation_messages.append(msg)
elif msg.type == "ai":
# drop any assistant message that still has a pending tool_call
if not getattr(msg, "tool_calls", None):
conversation_messages.append(msg)
recent_context = conversation_messages[-MAX_CONTEXT_MESSAGES:]
# Step 4: Build final prompt
prompt = [SystemMessage(system_prompt)] + recent_context
# Step 5: Generate response
response = llm.invoke(prompt)
# Logging final answer
# pull out the same user_id you used in the beginning
user_id = state.get("user_id", "unknown_user")
# the last human message is the question
human_messages = [m.content for m in state["messages"] if m.type == "human"]
question = human_messages[-1] if human_messages else ""
answer = response.content
log_interaction_cloud(user_id, question, answer)
return {"messages": state["messages"] + [response]}
#Finally, we compile our application into a single graph object. In this case,
# we are just connecting the steps into a sequence. We also allow the first
# query_or_respond step to "short-circuit" and respond directly to the user if
# it does not generate a tool call. This allows our application to support
# conversational experiences-- e.g., responding to generic greetings that
# may not require a retrieval step.
g = StateGraph(MessagesState)
g.add_node(query_or_respond)
g.add_node(tools_node)
g.add_node(generate)
g.set_entry_point("query_or_respond")
g.add_conditional_edges("query_or_respond", tools_condition, {END: END, "tools": "tools"})
g.add_edge("tools", "generate")
g.add_edge("generate", END)
graph = g.compile()
import gradio as gr
OFF_TOPIC_SNIPPET = "I can only answer questions related to the process of naturalization"
from langchain.memory import ConversationBufferWindowMemory
def get_fresh_memory(user_id):
return ConversationBufferWindowMemory(
memory_key="chat_history",
return_messages=True,
k=5
)
def get_memory_from_state(chat_state):
memory = ConversationBufferWindowMemory(
memory_key="chat_history",
return_messages=True,
k=5
)
for pair in chat_state or []:
if len(pair) == 2:
user_msg, ai_msg = pair
memory.save_context({"input": user_msg}, {"output": ai_msg})
return memory
def stream_response(user_input, user_id):
if not user_id:
user_id = "anonymous"
# Do NOT reset memory here! Just generate response
state = {
"messages": [HumanMessage(content=user_message)],
"user_id": user_id,
}
final_state = graph.invoke(state)
final_message = final_state["messages"][-1].content
collected = ""
for token in final_message:
collected += token
yield collected
def respond(user_message, chat_history, user_id, chat_state):
lang_code = detect_supported_language(user_message)
if not lang_code:
chat_state = chat_state or []
chat_state.append([user_message, "Leider kann ich nur in den folgenden Sprachen lesen und schreiben: Deutsch, Englisch, Türkisch oder Arabisch. In welcher Sprache möchten Sie weitermachen?"])
return chat_state, chat_state, "" # clear textbox
if not user_id:
user_id = "anonymous"
# Rebuild memory from chat_state
memory = get_memory_from_state(chat_state)
# Generate response
state = {
"messages": [HumanMessage(content=user_message)],
"user_id": user_id,
"chat_history": chat_state,
}
final_state = graph.invoke(state)
full_response = final_state["messages"][-1].content
# Save to memory (for next time)
memory.save_context({"input": user_message}, {"output": full_response})
# Update chat_state
chat_state = chat_state or []
chat_state.append([user_message, full_response])
return chat_state, chat_state, "" # clear textbox
def clear_history():
return "", [], ""
description = """
### Was ist **Einbürgerungs-Helfer**?
**Deutsch:** Einbürgerungs-Helfer hilft dir, den Weg zur deutschen Staatsangehörigkeit zu verstehen – einfach, mehrsprachig, datenschutzfreundlich.
**English:** Einbürgerungs-Helfer helps you understand how to get German citizenship – simple, multilingual, and privacy-friendly.
**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ı.
<div dir="rtl" style="text-align: right;"><strong>العربية:</strong> يساعدك مساعد التجنيس على فهم الطريق إلى الجنسية الألمانية - بسيط ومتعدد اللغات وملائم لحماية البيانات.</div>
"""
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as iface:
gr.Markdown(f"# Einbürgerungs-Helfer\n{description}")
with gr.Row():
user_id_input = gr.Textbox(label="Benutzer-ID (User ID)", placeholder="z.B. user_123", lines=1)
clear_btn = gr.Button("🗑️ Verlauf löschen")
chat_state = gr.State([])
chatbot = gr.Chatbot(
label="Chat",
show_copy_button=True,
value=[[None,
"Ich bin hier, um Ihnen bei allen Fragen zur Einbürgerung in Deutschland zu helfen.\n"
"Sie können Ihre Frage auf Deutsch, Englisch, Türkisch oder Arabisch stellen.\n"
"Lassen Sie uns loslegen – wie kann ich Ihnen heute helfen?"]]
)
msg_input = gr.Textbox(
placeholder="Frage eingeben...",
label="Deine Nachricht",
lines=1,
value="" # start empty
)
with gr.Row():
submit_btn = gr.Button("Senden", variant="primary")
# Button click
submit_btn.click(
fn=respond,
inputs=[msg_input, chatbot, user_id_input, chat_state],
outputs=[chatbot, chat_state, msg_input],
)
# Enter key in textbox
msg_input.submit(
fn=respond,
inputs=[msg_input, chatbot, user_id_input, chat_state],
outputs=[chatbot, chat_state, msg_input],
)
clear_btn.click(fn=clear_history, inputs=[], outputs=[user_id_input, chatbot, msg_input])
iface.launch(share=True)
|