Spaces:
Paused
Paused
Upload main.py
Browse files
main.py
CHANGED
|
@@ -1,45 +1,42 @@
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
import json
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
app = FastAPI()
|
| 5 |
|
| 6 |
-
# 1. Startseite (damit du im Browser siehst, dass es läuft)
|
| 7 |
@app.get("/")
|
| 8 |
def home():
|
| 9 |
-
return {"status": "
|
| 10 |
|
| 11 |
-
# 2. Der Such-Endpunkt für Vapi
|
| 12 |
@app.post("/search")
|
| 13 |
async def search_knowledge(request: Request):
|
| 14 |
-
#
|
| 15 |
try:
|
| 16 |
data = await request.json()
|
| 17 |
-
print(f"📥
|
| 18 |
except:
|
| 19 |
-
return {"result": "Fehler: Kein JSON
|
| 20 |
|
| 21 |
query_text = ""
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
# Check 1: Einfaches Format (dein Curl Test)
|
| 26 |
if "query" in data and isinstance(data["query"], str):
|
| 27 |
query_text = data["query"]
|
| 28 |
-
|
| 29 |
-
# Check 2: Vapi Format A (message -> toolCalls)
|
| 30 |
elif "message" in data and "toolCalls" in data["message"]:
|
| 31 |
try:
|
| 32 |
args = data["message"]["toolCalls"][0]["function"]["arguments"]
|
| 33 |
-
# Manchmal ist args ein String, manchmal schon ein Objekt
|
| 34 |
if isinstance(args, str):
|
| 35 |
query_text = json.loads(args).get("query", "")
|
| 36 |
else:
|
| 37 |
query_text = args.get("query", "")
|
| 38 |
except:
|
| 39 |
pass
|
| 40 |
-
|
| 41 |
-
# Check 3: Vapi Format B (direkt toolCall)
|
| 42 |
-
elif "toolCall" in data:
|
| 43 |
try:
|
| 44 |
args = data["toolCall"]["function"]["arguments"]
|
| 45 |
if isinstance(args, str):
|
|
@@ -49,13 +46,25 @@ async def search_knowledge(request: Request):
|
|
| 49 |
except:
|
| 50 |
pass
|
| 51 |
|
| 52 |
-
|
|
|
|
| 53 |
if not query_text:
|
| 54 |
-
|
| 55 |
-
return {"result": "Ich habe die Frage technisch nicht verstanden. Bitte prüfen Sie die Parameter."}
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
|
|
|
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
import json
|
| 3 |
|
| 4 |
+
# Jetzt können wir das wieder laden, da es in requirements.txt steht!
|
| 5 |
+
try:
|
| 6 |
+
from sqlalchemy import create_engine, text
|
| 7 |
+
except ImportError:
|
| 8 |
+
pass # Fallback, falls Installation noch läuft
|
| 9 |
+
|
| 10 |
app = FastAPI()
|
| 11 |
|
|
|
|
| 12 |
@app.get("/")
|
| 13 |
def home():
|
| 14 |
+
return {"status": "Brain is online."}
|
| 15 |
|
|
|
|
| 16 |
@app.post("/search")
|
| 17 |
async def search_knowledge(request: Request):
|
| 18 |
+
# 1. DATEN EMPFANGEN (Der Vapi-Fix)
|
| 19 |
try:
|
| 20 |
data = await request.json()
|
| 21 |
+
print(f"📥 Vapi sendet: {json.dumps(data)}")
|
| 22 |
except:
|
| 23 |
+
return {"result": "Fehler: Kein JSON."}
|
| 24 |
|
| 25 |
query_text = ""
|
| 26 |
|
| 27 |
+
# Wir suchen die Frage überall im Paket:
|
|
|
|
|
|
|
| 28 |
if "query" in data and isinstance(data["query"], str):
|
| 29 |
query_text = data["query"]
|
|
|
|
|
|
|
| 30 |
elif "message" in data and "toolCalls" in data["message"]:
|
| 31 |
try:
|
| 32 |
args = data["message"]["toolCalls"][0]["function"]["arguments"]
|
|
|
|
| 33 |
if isinstance(args, str):
|
| 34 |
query_text = json.loads(args).get("query", "")
|
| 35 |
else:
|
| 36 |
query_text = args.get("query", "")
|
| 37 |
except:
|
| 38 |
pass
|
| 39 |
+
elif "toolCall" in data: # Manchmal schickt Vapi es so
|
|
|
|
|
|
|
| 40 |
try:
|
| 41 |
args = data["toolCall"]["function"]["arguments"]
|
| 42 |
if isinstance(args, str):
|
|
|
|
| 46 |
except:
|
| 47 |
pass
|
| 48 |
|
| 49 |
+
print(f"🔎 Frage erkannt: '{query_text}'")
|
| 50 |
+
|
| 51 |
if not query_text:
|
| 52 |
+
return {"result": "Ich habe die Frage nicht verstanden."}
|
|
|
|
| 53 |
|
| 54 |
+
# 2. LOGIK: Hier simulieren wir deine Datenbank (CAPANEO Wissen)
|
| 55 |
+
# Später kannst du hier deine echte 'sqlalchemy' Suche wieder reinkopieren.
|
| 56 |
+
|
| 57 |
+
query_lower = query_text.lower()
|
| 58 |
|
| 59 |
+
if "umsätze" in query_lower or "händler" in query_lower:
|
| 60 |
+
# Das ist die Antwort aus deinem Screenshot!
|
| 61 |
+
antwort = "Umsätze sprudeln für Händler, wenn sie genau an dem Ort präsent sind, wo auch ihre Kunden sind."
|
| 62 |
+
elif "geo" in query_lower:
|
| 63 |
+
antwort = "Geofencing ist eine Technologie, die Aktionen auslöst, wenn ein Gerät eine Zone betritt."
|
| 64 |
+
else:
|
| 65 |
+
antwort = "Dazu habe ich leider keine Informationen in der Datenbank."
|
| 66 |
+
|
| 67 |
+
# 3. RÜCKGABE (Kompakt für Vapi)
|
| 68 |
+
return {
|
| 69 |
+
"result": antwort
|
| 70 |
+
}
|