Spaces:
Paused
Paused
| from fastapi import FastAPI, Request | |
| import json | |
| # Wir laden SQL nur, wenn es da ist (um Abstuerze zu verhindern) | |
| try: | |
| from sqlalchemy import create_engine, text | |
| except ImportError: | |
| pass | |
| app = FastAPI() | |
| def home(): | |
| return {"status": "Bereit fuer Vapi!"} | |
| async def search_knowledge(request: Request): | |
| # 1. DATEN EMPFANGEN (Vapi-kompatibel) | |
| try: | |
| data = await request.json() | |
| print(f"📥 Vapi Log: {json.dumps(data)}") | |
| except: | |
| return {"result": "Fehler: Kein JSON."} | |
| query_text = "" | |
| # Wir fischen die Frage aus der verschachtelten Vapi-Struktur: | |
| if "query" in data and isinstance(data["query"], str): | |
| query_text = data["query"] | |
| elif "message" in data and "toolCalls" in data["message"]: | |
| try: | |
| # Wir holen uns die Argumente aus der Tool-Liste | |
| args = data["message"]["toolCalls"][0]["function"]["arguments"] | |
| # Manchmal ist es ein String, manchmal ein Dictionary | |
| if isinstance(args, str): | |
| query_text = json.loads(args).get("query", "") | |
| else: | |
| query_text = args.get("query", "") | |
| except: | |
| pass | |
| elif "toolCall" in data: | |
| try: | |
| args = data["toolCall"]["function"]["arguments"] | |
| if isinstance(args, str): | |
| query_text = json.loads(args).get("query", "") | |
| else: | |
| query_text = args.get("query", "") | |
| except: | |
| pass | |
| print(f"🔎 Erkannte Frage: '{query_text}'") | |
| # 2. ANTWORT LOGIK (Hardcoded für deinen Test) | |
| query_lower = query_text.lower() | |
| if "umsätze" in query_lower or "händler" in query_lower: | |
| antwort = "Umsätze sprudeln für Händler, wenn sie genau an dem Ort präsent sind, wo auch ihre Kunden sind." | |
| elif "geo" in query_lower: | |
| antwort = "Geofencing ist eine Technologie, die Aktionen auslöst, wenn ein Gerät eine Zone betritt." | |
| else: | |
| antwort = "Dazu habe ich leider keine Informationen in der Datenbank." | |
| # 3. RÜCKGABE | |
| return { | |
| "result": antwort | |
| } |