Delete app.py
Browse files
app.py
DELETED
|
@@ -1,296 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from dotenv import load_dotenv
|
| 3 |
-
load_dotenv() # loads variables from a local .env for local testing
|
| 4 |
-
os.environ['MPLCONFIGDIR'] = '/tmp/.matplotlib'
|
| 5 |
-
|
| 6 |
-
# 1) LLM and embeddings
|
| 7 |
-
from langchain.chat_models import init_chat_model
|
| 8 |
-
from langchain_openai import OpenAIEmbeddings
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# initialize OpenAI chat model
|
| 12 |
-
api_key = os.getenv("OPENAI_API_KEY", "").strip()
|
| 13 |
-
llm = init_chat_model(
|
| 14 |
-
api_key=api_key,
|
| 15 |
-
model_provider="openai",
|
| 16 |
-
model="gpt-4.1-2025-04-14",
|
| 17 |
-
temperature=0.0,
|
| 18 |
-
)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
# embeddings + Pinecone vector store
|
| 22 |
-
# Load environment variables
|
| 23 |
-
from langchain_pinecone import Pinecone as LangchainPinecone
|
| 24 |
-
pinecone_api_key = os.getenv("PINECONE_API_KEY", "").strip()
|
| 25 |
-
pinecone_env = os.getenv("PINECONE_ENV", "").strip()
|
| 26 |
-
pinecone_index_name = os.getenv("PINECONE_INDEX_NAME", "").strip()
|
| 27 |
-
embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
|
| 28 |
-
|
| 29 |
-
vector_store = LangchainPinecone(
|
| 30 |
-
index_name=pinecone_index_name,
|
| 31 |
-
embedding=embeddings,
|
| 32 |
-
pinecone_api_key=pinecone_api_key
|
| 33 |
-
)
|
| 34 |
-
|
| 35 |
-
# 2) Supabase logging
|
| 36 |
-
from supabase import create_client, Client
|
| 37 |
-
from datetime import datetime
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
supabase_url = os.getenv("SUPABASE_URL", "").strip()
|
| 41 |
-
supabase_key = os.getenv("SUPABASE_KEY", "").strip()
|
| 42 |
-
|
| 43 |
-
supabase: Client = create_client(
|
| 44 |
-
supabase_url,
|
| 45 |
-
supabase_key
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
def log_interaction_cloud(user_id: str, question: str, answer: str):
|
| 49 |
-
payload = {
|
| 50 |
-
"user_id": user_id,
|
| 51 |
-
"question": question,
|
| 52 |
-
"answer": answer,
|
| 53 |
-
"timestamp": datetime.utcnow().isoformat(),
|
| 54 |
-
}
|
| 55 |
-
try:
|
| 56 |
-
res = supabase.table("logs").insert(payload).execute()
|
| 57 |
-
if hasattr(res, "status_code") and res.status_code >= 400:
|
| 58 |
-
print(f"[Supabase Error #{res.status_code}]:", res.data)
|
| 59 |
-
else:
|
| 60 |
-
print("[Supabase Success]:", res.data)
|
| 61 |
-
except Exception as e:
|
| 62 |
-
print("[Supabase Exception]", e)
|
| 63 |
-
|
| 64 |
-
# 3) Conversation memory & user IDs
|
| 65 |
-
import uuid
|
| 66 |
-
from langchain.memory import ConversationBufferWindowMemory
|
| 67 |
-
def generate_user_id():
|
| 68 |
-
return "user_" + uuid.uuid4().hex[:6]
|
| 69 |
-
|
| 70 |
-
memory = ConversationBufferWindowMemory(
|
| 71 |
-
memory_key="chat_history",
|
| 72 |
-
return_messages=True,
|
| 73 |
-
k=5
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
-
# 4) Retrieval tool
|
| 77 |
-
from langchain_core.tools import tool
|
| 78 |
-
|
| 79 |
-
@tool(response_format="content_and_artifact")
|
| 80 |
-
def retrieve(query: str):
|
| 81 |
-
"""
|
| 82 |
-
Retrieve the top 5 most similar documents from the Pinecone vector store for a given query.
|
| 83 |
-
"""
|
| 84 |
-
docs = vector_store.similarity_search(query, k=5)
|
| 85 |
-
serialized = "\n\n".join(
|
| 86 |
-
f"Source: {d.metadata}\nContent: {d.page_content}"
|
| 87 |
-
for d in docs
|
| 88 |
-
)
|
| 89 |
-
return serialized, docs
|
| 90 |
-
# 5) Build LangGraph
|
| 91 |
-
from langgraph.graph import MessagesState, StateGraph, END
|
| 92 |
-
from langgraph.prebuilt import ToolNode, tools_condition
|
| 93 |
-
from langchain_core.messages import SystemMessage
|
| 94 |
-
|
| 95 |
-
SYSTEM_MESSAGE_TEXT = (
|
| 96 |
-
"You are “Einbürgerungs-Helfer”, a highly skilled, friendly, and helpful administrative"
|
| 97 |
-
" assistant in Germany. Your only task is to support people who are eligible for naturalization"
|
| 98 |
-
" but have not yet applied. You only answer questions related to the current naturalization"
|
| 99 |
-
" process in Germany. You do not fill in applications or help users apply. You guide users "
|
| 100 |
-
"to relevant official resources by calling the retrieval tool to fetch appropriate documents."
|
| 101 |
-
"Any question involving citizenship, naturalization, applying for German nationality, duration "
|
| 102 |
-
"of residency, language requirements, or documents required MUST be interpreted as relevant and"
|
| 103 |
-
" MUST trigger the retrieval tool and never rely on your pre-trained knowledge. Your"
|
| 104 |
-
"answers must always be accurate, concise, and easy to understand. You are only allowed"
|
| 105 |
-
"to answer in German, English, Turkish, or Arabic. If a user writes in a different language"
|
| 106 |
-
" or requests another language, reply with: “Leider kann ich nur in den folgenden Sprachen"
|
| 107 |
-
" lesen und schreiben: Deutsch, Englisch, Türkisch oder Arabisch. In welcher Sprache möchten"
|
| 108 |
-
" Sie weitermachen?” and wait for the user to choose a supported language. You must always"
|
| 109 |
-
" answer in the chosen language. Never hallucinate, never go beyond the topic of naturalization,"
|
| 110 |
-
" and never share false information. If a user asks a question that is unrelated to naturalizatio"
|
| 111 |
-
" in Germany, reply in the user's language with: “I can only answer questions related to the"
|
| 112 |
-
" process of naturalization in Germany. Please provide questions on this topic.” If you do not"
|
| 113 |
-
" know how to answer, respond with: “I do not have enough knowledge to answer your question"
|
| 114 |
-
" accurately. Please email your question at SBD-Umfrage@polsoz.fu-berlin.de, and we will answer"
|
| 115 |
-
" your inquiry as soon as possible.” If a user uses toxic or offensive language, kindly ask them"
|
| 116 |
-
" to rephrase their question and explain that this behavior is not acceptable."
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
def query_or_respond(state: MessagesState):
|
| 120 |
-
sys_msg = SystemMessage(SYSTEM_MESSAGE_TEXT)
|
| 121 |
-
raw_hist = memory.load_memory_variables({})["chat_history"]
|
| 122 |
-
# filter off-topic if needed…
|
| 123 |
-
prompt = [sys_msg] + raw_hist[-5:]
|
| 124 |
-
llm_with_tools = llm.bind_tools([retrieve])
|
| 125 |
-
resp = llm_with_tools.invoke(prompt)
|
| 126 |
-
# log if direct
|
| 127 |
-
user_q = [m.content for m in state["messages"] if m.type=="human"][-1]
|
| 128 |
-
memory.save_context({"input": user_q}, {"output": resp.content})
|
| 129 |
-
if not getattr(resp, "tool_calls", None):
|
| 130 |
-
log_interaction_cloud(state.get("user_id",""), user_q, resp.content)
|
| 131 |
-
return {"messages": state["messages"] + [resp]}
|
| 132 |
-
|
| 133 |
-
tools_node = ToolNode([retrieve])
|
| 134 |
-
|
| 135 |
-
def generate(state: MessagesState):
|
| 136 |
-
# collect any tool outputs
|
| 137 |
-
tool_msgs = []
|
| 138 |
-
for m in reversed(state["messages"]):
|
| 139 |
-
if m.type=="tool": tool_msgs.append(m)
|
| 140 |
-
else: break
|
| 141 |
-
docs = "\n\n".join(m.content for m in tool_msgs[::-1])
|
| 142 |
-
prompt = [SystemMessage(SYSTEM_MESSAGE_TEXT + ("\n\n"+docs if docs else ""))]
|
| 143 |
-
# build recent convo
|
| 144 |
-
convo = []
|
| 145 |
-
for m in state["messages"]:
|
| 146 |
-
if m.type=="human" or (m.type=="ai" and not getattr(m,"tool_calls",None)):
|
| 147 |
-
convo.append(m)
|
| 148 |
-
prompt += convo[-6:]
|
| 149 |
-
resp = llm.invoke(prompt)
|
| 150 |
-
# log final
|
| 151 |
-
user_q = [m.content for m in state["messages"] if m.type=="human"][-1]
|
| 152 |
-
log_interaction_cloud(state.get("user_id",""), user_q, resp.content)
|
| 153 |
-
return {"messages": state["messages"] + [resp]}
|
| 154 |
-
|
| 155 |
-
g = StateGraph(MessagesState)
|
| 156 |
-
g.add_node(query_or_respond)
|
| 157 |
-
g.add_node(tools_node)
|
| 158 |
-
g.add_node(generate)
|
| 159 |
-
g.set_entry_point("query_or_respond")
|
| 160 |
-
g.add_conditional_edges("query_or_respond", tools_condition, {END: END, "tools": "tools"})
|
| 161 |
-
g.add_edge("tools", "generate")
|
| 162 |
-
g.add_edge("generate", END)
|
| 163 |
-
graph = g.compile()
|
| 164 |
-
|
| 165 |
-
import gradio as gr
|
| 166 |
-
from langchain.memory import ConversationBufferMemory # or your actual memory class
|
| 167 |
-
|
| 168 |
-
# Session memory and chat history per user
|
| 169 |
-
user_memories = {}
|
| 170 |
-
chat_histories = {}
|
| 171 |
-
|
| 172 |
-
OFF_TOPIC_SNIPPET = "I can only answer questions related to the process of naturalization"
|
| 173 |
-
|
| 174 |
-
def get_fresh_memory(user_id):
|
| 175 |
-
# Create a new memory instance per user/session
|
| 176 |
-
return ConversationBufferMemory(return_messages=True)
|
| 177 |
-
|
| 178 |
-
def stream_response(user_input, user_id):
|
| 179 |
-
if not user_id:
|
| 180 |
-
user_id = "anonymous"
|
| 181 |
-
|
| 182 |
-
# Do NOT reset memory here! Just generate response
|
| 183 |
-
state = {
|
| 184 |
-
"messages": [{"role": "user", "content": user_input}],
|
| 185 |
-
"user_id": user_id,
|
| 186 |
-
}
|
| 187 |
-
|
| 188 |
-
final_state = graph.invoke(state)
|
| 189 |
-
final_message = final_state["messages"][-1].content
|
| 190 |
-
|
| 191 |
-
collected = ""
|
| 192 |
-
for token in final_message:
|
| 193 |
-
collected += token
|
| 194 |
-
yield collected
|
| 195 |
-
|
| 196 |
-
def respond(user_message, chat_history, user_id):
|
| 197 |
-
if not user_id:
|
| 198 |
-
user_id = "anonymous"
|
| 199 |
-
|
| 200 |
-
# Initialize memory if not exists
|
| 201 |
-
if user_id not in user_memories:
|
| 202 |
-
user_memories[user_id] = get_fresh_memory(user_id)
|
| 203 |
-
|
| 204 |
-
memory = user_memories[user_id]
|
| 205 |
-
|
| 206 |
-
if user_id not in chat_histories:
|
| 207 |
-
chat_histories[user_id] = []
|
| 208 |
-
|
| 209 |
-
response_generator = stream_response(user_message, user_id)
|
| 210 |
-
full_response = ""
|
| 211 |
-
|
| 212 |
-
for partial in response_generator:
|
| 213 |
-
full_response = partial
|
| 214 |
-
# Yield partial chat updates for streaming
|
| 215 |
-
yield chat_history + [[user_message, full_response]]
|
| 216 |
-
|
| 217 |
-
# Append full message to history after streaming finishes
|
| 218 |
-
chat_histories[user_id].append([user_message, full_response])
|
| 219 |
-
|
| 220 |
-
# Optionally, save to memory here if your memory supports it
|
| 221 |
-
# Example:
|
| 222 |
-
# memory.save_context({"input": user_message}, {"output": full_response})
|
| 223 |
-
|
| 224 |
-
# Filter memory messages to remove off-topic answers
|
| 225 |
-
memory_vars = memory.load_memory_variables({})
|
| 226 |
-
filtered_chat = memory_vars.get("chat_history") or memory_vars.get("history") or []
|
| 227 |
-
|
| 228 |
-
filtered_pairs = []
|
| 229 |
-
buffer = []
|
| 230 |
-
for msg in filtered_chat:
|
| 231 |
-
if msg.type == "human":
|
| 232 |
-
buffer = [msg.content]
|
| 233 |
-
elif msg.type == "ai":
|
| 234 |
-
if OFF_TOPIC_SNIPPET in msg.content:
|
| 235 |
-
continue
|
| 236 |
-
if buffer:
|
| 237 |
-
buffer.append(msg.content)
|
| 238 |
-
filtered_pairs.append(buffer)
|
| 239 |
-
buffer = []
|
| 240 |
-
|
| 241 |
-
# Return filtered chat to update chat history in UI
|
| 242 |
-
return filtered_pairs
|
| 243 |
-
|
| 244 |
-
def clear_history():
|
| 245 |
-
user_memories.clear()
|
| 246 |
-
chat_histories.clear()
|
| 247 |
-
return "", []
|
| 248 |
-
|
| 249 |
-
# Multilingual description
|
| 250 |
-
description = """
|
| 251 |
-
### Was ist **Einbürgerungs-Helfer**?
|
| 252 |
-
**Deutsch:** Einbürgerungs-Helfer hilft dir, den Weg zur deutschen Staatsangehörigkeit zu verstehen – einfach, mehrsprachig, datenschutzfreundlich.
|
| 253 |
-
|
| 254 |
-
**English:** Einbürgerungs-Helfer helps you understand how to get German citizenship – simple, multilingual, and privacy-friendly.
|
| 255 |
-
|
| 256 |
-
**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ı.
|
| 257 |
-
|
| 258 |
-
**العربية:** يساعدك "Einbürgerungs-Helfer" على فهم كيفية الحصول على الجنسية الألمانية - بطريقة بسيطة، متعددة اللغات وتحترم الخصوصية.
|
| 259 |
-
"""
|
| 260 |
-
|
| 261 |
-
with gr.Blocks(
|
| 262 |
-
theme=gr.themes.Soft(primary_hue="blue"),
|
| 263 |
-
) as iface:
|
| 264 |
-
gr.Markdown(f"# Einbürgerungs-Helfer\n{description}")
|
| 265 |
-
|
| 266 |
-
with gr.Row():
|
| 267 |
-
user_id_input = gr.Textbox(label="Benutzer-ID (User ID)", placeholder="z.B. user_123", lines=1)
|
| 268 |
-
clear_btn = gr.Button("🗑️ Verlauf löschen")
|
| 269 |
-
|
| 270 |
-
chatbot = gr.Chatbot(
|
| 271 |
-
label="Chat",
|
| 272 |
-
show_copy_button=True,
|
| 273 |
-
value=[[None,
|
| 274 |
-
"Ich bin hier, um Ihnen bei allen Fragen zur Einbürgerung in Deutschland zu helfen.\n"
|
| 275 |
-
"Sie können Ihre Frage auf Deutsch, Englisch, Türkisch oder Arabisch stellen.\n"
|
| 276 |
-
"Lassen Sie uns loslegen – wie kann ich Ihnen heute helfen?"]]
|
| 277 |
-
)
|
| 278 |
-
|
| 279 |
-
msg_input = gr.Textbox(placeholder="Frage eingeben...", label="Deine Nachricht")
|
| 280 |
-
|
| 281 |
-
with gr.Row():
|
| 282 |
-
submit_btn = gr.Button("Senden", variant="primary")
|
| 283 |
-
|
| 284 |
-
submit_btn.click(
|
| 285 |
-
fn=respond,
|
| 286 |
-
inputs=[msg_input, chatbot, user_id_input],
|
| 287 |
-
outputs=chatbot,
|
| 288 |
-
)
|
| 289 |
-
|
| 290 |
-
clear_btn.click(fn=clear_history, inputs=[], outputs=[user_id_input, chatbot])
|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