Update app.py
Browse files
app.py
CHANGED
|
@@ -1,94 +1,176 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
import sys
|
| 4 |
from dotenv import load_dotenv
|
|
|
|
| 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 |
-
api_key = os.environ.get("OPENAI_API_KEY")
|
| 41 |
-
if not api_key:
|
| 42 |
-
return "❌ No OPENAI_API_KEY found"
|
| 43 |
-
|
| 44 |
try:
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
)
|
| 51 |
-
return "✅ OpenAI ChatGPT initialized successfully"
|
| 52 |
except Exception as e:
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
**OpenAI Connection Test:**
|
| 67 |
-
{openai_test}
|
| 68 |
-
|
| 69 |
-
**Environment Check:**
|
| 70 |
-
- OPENAI_API_KEY: {'✅ SET' if os.environ.get('OPENAI_API_KEY') else '❌ MISSING'}
|
| 71 |
-
- PINECONE_API_KEY: {'✅ SET' if os.environ.get('PINECONE_API_KEY') else '❌ MISSING'}
|
| 72 |
-
- SUPABASE_URL: {'✅ SET' if os.environ.get('SUPABASE_URL') else '❌ MISSING'}
|
| 73 |
-
"""
|
| 74 |
-
|
| 75 |
-
# Test a simple OpenAI call if everything looks good
|
| 76 |
-
if os.environ.get("OPENAI_API_KEY") and "✅" in openai_test:
|
| 77 |
-
try:
|
| 78 |
-
from langchain_openai import ChatOpenAI
|
| 79 |
-
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.0)
|
| 80 |
-
test_response = llm.invoke("Say 'Hello from LangChain!'")
|
| 81 |
-
response += f"\n**LangChain Test Response:**\n{test_response.content}"
|
| 82 |
-
except Exception as e:
|
| 83 |
-
response += f"\n**LangChain Test Failed:** {e}"
|
| 84 |
-
|
| 85 |
-
return response
|
| 86 |
-
|
| 87 |
-
demo = gr.ChatInterface(
|
| 88 |
-
fn=chat_function,
|
| 89 |
-
title="🔧 Debug Chat - Step 4 (LangChain Core)",
|
| 90 |
-
description="Testing LangChain core components"
|
| 91 |
)
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
+
load_dotenv() # loads variables from a local .env for local testing
|
| 4 |
|
| 5 |
+
# 1) LLM and embeddings
|
| 6 |
+
from langchain.chat_models import init_chat_model
|
| 7 |
+
from langchain_openai import OpenAIEmbeddings
|
| 8 |
+
from pinecone import Pinecone
|
| 9 |
+
from langchain_pinecone import PineconeVectorStore
|
| 10 |
|
| 11 |
+
# initialize OpenAI chat model
|
| 12 |
+
llm = init_chat_model(
|
| 13 |
+
os.getenv("OPENAI_API_KEY"),
|
| 14 |
+
model_provider="openai",
|
| 15 |
+
model="gpt-4.1-2025-04-14",
|
| 16 |
+
temperature=0.0,
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# embeddings + Pinecone vector store
|
| 20 |
+
embeddings = OpenAIEmbeddings(model="text-embedding-3-large")
|
| 21 |
+
pinecone_api_key = os.getenv("PINECONE_API_KEY")
|
| 22 |
+
pinecone_index_name = os.getenv("PINECONE_INDEX_NAME")
|
| 23 |
+
pc = Pinecone(api_key=pinecone_api_key)
|
| 24 |
+
index = pc.Index(pinecone_index_name)
|
| 25 |
+
vector_store = PineconeVectorStore(embedding=embeddings, index=index)
|
| 26 |
+
|
| 27 |
+
# 2) Supabase logging
|
| 28 |
+
from supabase import create_client, Client
|
| 29 |
+
from datetime import datetime
|
| 30 |
+
|
| 31 |
+
supabase: Client = create_client(
|
| 32 |
+
os.getenv("SUPABASE_URL"),
|
| 33 |
+
os.getenv("SUPABASE_KEY")
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
def log_interaction_cloud(user_id: str, question: str, answer: str):
|
| 37 |
+
payload = {
|
| 38 |
+
"user_id": user_id,
|
| 39 |
+
"question": question,
|
| 40 |
+
"answer": answer,
|
| 41 |
+
"timestamp": datetime.utcnow().isoformat(),
|
| 42 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
try:
|
| 44 |
+
res = supabase.table("logs").insert(payload).execute()
|
| 45 |
+
if hasattr(res, "status_code") and res.status_code >= 400:
|
| 46 |
+
print(f"[Supabase Error #{res.status_code}]:", res.data)
|
| 47 |
+
else:
|
| 48 |
+
print("[Supabase Success]:", res.data)
|
|
|
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
+
print("[Supabase Exception]", e)
|
| 51 |
+
|
| 52 |
+
# 3) Conversation memory & user IDs
|
| 53 |
+
import uuid
|
| 54 |
+
from langchain.memory import ConversationBufferWindowMemory
|
| 55 |
+
def generate_user_id():
|
| 56 |
+
return "user_" + uuid.uuid4().hex[:6]
|
| 57 |
+
|
| 58 |
+
memory = ConversationBufferWindowMemory(
|
| 59 |
+
memory_key="chat_history",
|
| 60 |
+
return_messages=True,
|
| 61 |
+
k=5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
)
|
| 63 |
|
| 64 |
+
# 4) Retrieval tool
|
| 65 |
+
from langchain_core.tools import tool
|
| 66 |
+
|
| 67 |
+
@tool(response_format="content_and_artifact")
|
| 68 |
+
def retrieve(query: str):
|
| 69 |
+
docs = vector_store.similarity_search(query, k=5)
|
| 70 |
+
serialized = "\n\n".join(
|
| 71 |
+
f"Source: {d.metadata}\nContent: {d.page_content}"
|
| 72 |
+
for d in docs
|
| 73 |
+
)
|
| 74 |
+
return serialized, docs
|
| 75 |
+
|
| 76 |
+
# 5) Build LangGraph
|
| 77 |
+
from langgraph.graph import MessagesState, StateGraph, END
|
| 78 |
+
from langgraph.prebuilt import ToolNode, tools_condition
|
| 79 |
+
from langchain_core.messages import SystemMessage
|
| 80 |
+
|
| 81 |
+
SYSTEM_MESSAGE_TEXT = (
|
| 82 |
+
"You are “Einbürgerungs-Helfer” … [your long system prompt here] …"
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
def query_or_respond(state: MessagesState):
|
| 86 |
+
sys_msg = SystemMessage(SYSTEM_MESSAGE_TEXT)
|
| 87 |
+
raw_hist = memory.load_memory_variables({})["chat_history"]
|
| 88 |
+
# filter off-topic if needed…
|
| 89 |
+
prompt = [sys_msg] + raw_hist[-5:]
|
| 90 |
+
llm_with_tools = llm.bind_tools([retrieve])
|
| 91 |
+
resp = llm_with_tools.invoke(prompt)
|
| 92 |
+
# log if direct
|
| 93 |
+
user_q = [m.content for m in state["messages"] if m.type=="human"][-1]
|
| 94 |
+
memory.save_context({"input": user_q}, {"output": resp.content})
|
| 95 |
+
if not getattr(resp, "tool_calls", None):
|
| 96 |
+
log_interaction_cloud(state.get("user_id",""), user_q, resp.content)
|
| 97 |
+
return {"messages": state["messages"] + [resp]}
|
| 98 |
+
|
| 99 |
+
tools_node = ToolNode([retrieve])
|
| 100 |
+
|
| 101 |
+
def generate(state: MessagesState):
|
| 102 |
+
# collect any tool outputs
|
| 103 |
+
tool_msgs = []
|
| 104 |
+
for m in reversed(state["messages"]):
|
| 105 |
+
if m.type=="tool": tool_msgs.append(m)
|
| 106 |
+
else: break
|
| 107 |
+
docs = "\n\n".join(m.content for m in tool_msgs[::-1])
|
| 108 |
+
prompt = [SystemMessage(SYSTEM_MESSAGE_TEXT + ("\n\n"+docs if docs else ""))]
|
| 109 |
+
# build recent convo
|
| 110 |
+
convo = []
|
| 111 |
+
for m in state["messages"]:
|
| 112 |
+
if m.type=="human" or (m.type=="ai" and not getattr(m,"tool_calls",None)):
|
| 113 |
+
convo.append(m)
|
| 114 |
+
prompt += convo[-6:]
|
| 115 |
+
resp = llm.invoke(prompt)
|
| 116 |
+
# log final
|
| 117 |
+
user_q = [m.content for m in state["messages"] if m.type=="human"][-1]
|
| 118 |
+
log_interaction_cloud(state.get("user_id",""), user_q, resp.content)
|
| 119 |
+
return {"messages": state["messages"] + [resp]}
|
| 120 |
+
|
| 121 |
+
g = StateGraph(MessagesState)
|
| 122 |
+
g.add_node(query_or_respond)
|
| 123 |
+
g.add_node(tools_node)
|
| 124 |
+
g.add_node(generate)
|
| 125 |
+
g.set_entry_point("query_or_respond")
|
| 126 |
+
g.add_conditional_edges("query_or_respond", tools_condition, {END: END, "tools": "tools"})
|
| 127 |
+
g.add_edge("tools", "generate")
|
| 128 |
+
g.add_edge("generate", END)
|
| 129 |
+
graph = g.compile()
|
| 130 |
+
|
| 131 |
+
# 6) Gradio UI
|
| 132 |
+
import gradio as gr
|
| 133 |
+
|
| 134 |
+
chat_histories = {}
|
| 135 |
+
|
| 136 |
+
def stream_response(user_input, user_id):
|
| 137 |
+
if not user_id: user_id = "anonymous"
|
| 138 |
+
state = {"messages":[{"role":"user","user_id":user_id,"content":user_input}], "user_id":user_id}
|
| 139 |
+
collected = ""
|
| 140 |
+
for step in graph.stream(state, stream_mode="values"):
|
| 141 |
+
msg = step["messages"][-1].content
|
| 142 |
+
for t in msg:
|
| 143 |
+
collected += t
|
| 144 |
+
yield collected
|
| 145 |
+
|
| 146 |
+
def respond(user_msg, chat_history, user_id):
|
| 147 |
+
if user_id not in chat_histories: chat_histories[user_id] = []
|
| 148 |
+
gen = stream_response(user_msg, user_id)
|
| 149 |
+
full = ""
|
| 150 |
+
for partial in gen:
|
| 151 |
+
full = partial
|
| 152 |
+
yield chat_history + [[user_msg, full]]
|
| 153 |
+
chat_histories[user_id].append([user_msg, full])
|
| 154 |
+
|
| 155 |
+
def clear_history():
|
| 156 |
+
return "", []
|
| 157 |
+
|
| 158 |
+
description = """### Einbürgerungs-Helfer
|
| 159 |
+
… your multilingual description …"""
|
| 160 |
+
|
| 161 |
+
with gr.Blocks() as iface:
|
| 162 |
+
gr.Markdown("# Einbürgerungs-Helfer\n" + description)
|
| 163 |
+
with gr.Row():
|
| 164 |
+
user_id_input = gr.Textbox(label="User ID", placeholder="z.B. user_123")
|
| 165 |
+
clear_btn = gr.Button("🗑️ Clear")
|
| 166 |
+
chatbot = gr.Chatbot(value=[[None,"Ich helfe…"]])
|
| 167 |
+
msg_in = gr.Textbox(label="Your question")
|
| 168 |
+
btn = gr.Button("Send")
|
| 169 |
+
btn.click(respond, [msg_in, chatbot, user_id_input], chatbot)
|
| 170 |
+
clear_btn.click(clear_history, [], [user_id_input, chatbot])
|
| 171 |
+
|
| 172 |
+
iface.launch(
|
| 173 |
+
server_name="0.0.0.0",
|
| 174 |
+
server_port=int(os.getenv("PORT", 7860)),
|
| 175 |
+
share=False
|
| 176 |
+
)
|