Spaces:
Paused
Paused
| from fastapi import FastAPI, Request | |
| import json | |
| app = FastAPI() | |
| # 1. Startseite (damit du im Browser siehst, dass es läuft) | |
| def home(): | |
| return {"status": "Die API läuft und wartet auf Vapi!"} | |
| # 2. Der Such-Endpunkt für Vapi | |
| async def search_knowledge(request: Request): | |
| # Wir holen uns die Daten, egal wie Vapi sie verpackt | |
| try: | |
| data = await request.json() | |
| print(f"📥 DEBUG: {json.dumps(data)}") # Zeigt dir im Log, was ankommt | |
| except: | |
| return {"result": "Fehler: Kein JSON empfangen."} | |
| query_text = "" | |
| # Vapi sendet Daten oft tief verschachtelt. Wir suchen die 'query' überall: | |
| # Check 1: Einfaches Format (dein Curl Test) | |
| if "query" in data and isinstance(data["query"], str): | |
| query_text = data["query"] | |
| # Check 2: Vapi Format A (message -> toolCalls) | |
| elif "message" in data and "toolCalls" in data["message"]: | |
| try: | |
| args = data["message"]["toolCalls"][0]["function"]["arguments"] | |
| # Manchmal ist args ein String, manchmal schon ein Objekt | |
| if isinstance(args, str): | |
| query_text = json.loads(args).get("query", "") | |
| else: | |
| query_text = args.get("query", "") | |
| except: | |
| pass | |
| # Check 3: Vapi Format B (direkt toolCall) | |
| 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 | |
| # --- ANTWORT --- | |
| if not query_text: | |
| # Fallback, falls wir nichts finden | |
| return {"result": "Ich habe die Frage technisch nicht verstanden. Bitte prüfen Sie die Parameter."} | |
| # Einfache Logik für den Test: | |
| if "geo" in query_text.lower(): | |
| return {"result": "Geofencing ist eine virtuelle Begrenzung für ein echtes geografisches Gebiet."} | |
| return {"result": f"Ich habe deine Frage '{query_text}' erhalten, habe aber keine Antwort in der Datenbank."} |