martinbrahm commited on
Commit
212e616
·
verified ·
1 Parent(s): 701465f

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +22 -18
main.py CHANGED
@@ -13,7 +13,7 @@ COLLECTION_RULES = "availability_rules"
13
  COLLECTION_INBOX = "inbox"
14
  KNOWLEDGE_CACHE = []
15
 
16
- # --- FIREBASE VERBINDUNG ---
17
  db = None
18
  try:
19
  key = os.environ.get("FIREBASE_KEY")
@@ -50,8 +50,7 @@ def get_stem(word):
50
  return w
51
 
52
  # ==========================================
53
- # NEU: TOOL FÜR VERFÜGBARKEIT
54
- # Endpunkt: /check_availability
55
  # ==========================================
56
  @app.post("/check_availability")
57
  async def check_availability(request: Request):
@@ -59,27 +58,27 @@ async def check_availability(request: Request):
59
 
60
  today = datetime.now().strftime("%Y-%m-%d")
61
  status = "available"
62
- message = "Normaler Betrieb. Nimm Anrufe entgegen und vereinbare Termine."
 
63
 
64
  try:
65
  if db:
66
  rules = db.collection(COLLECTION_RULES).where("active", "==", True).stream()
67
  for r in rules:
68
  rd = r.to_dict()
69
- # Datum Check
70
  if rd.get('start_date') <= today <= rd.get('end_date'):
71
  print(f"🛑 REGEL GEFUNDEN: {rd.get('name')}")
72
  status = "unavailable"
73
- # Wir bauen die Instruktion direkt in die Message
74
- message = f"ACHTUNG SONDERREGEL: {rd.get('instruction_text')}"
75
  break
76
  except Exception as e:
77
  print(f"❌ ERROR CHECK: {e}")
78
- return {"status": "error", "message": "Fehler beim Datenbank-Check. Gehe von Normalbetrieb aus."}
79
 
80
  print(f"👉 RESULTAT: {status} | {message}")
81
 
82
- # Rückgabe genau wie vom LLM benötigt
83
  return {
84
  "result": json.dumps({
85
  "status": status,
@@ -88,8 +87,7 @@ async def check_availability(request: Request):
88
  }
89
 
90
  # ==========================================
91
- # TOOL: SUCHE / WISSEN
92
- # Endpunkt: /search
93
  # ==========================================
94
  @app.post("/search")
95
  async def search(request: Request):
@@ -97,7 +95,6 @@ async def search(request: Request):
97
  data = await request.json()
98
  query = ""
99
 
100
- # Parsing Logic
101
  if "search_query" in data: query = data["search_query"]
102
  elif "message" in data and "toolCalls" in data["message"]:
103
  args = data["message"]["toolCalls"][0]["function"]["arguments"]
@@ -107,13 +104,12 @@ async def search(request: Request):
107
  print(f"🔎 FRAGE: '{query}'")
108
  if not query: return {"result": "Akustik-Fehler."}
109
 
110
- # Stop-Wörter
111
  STOP_WORDS = ["capaneo", "udo", "schober", "firma", "gmbh", "hallo", "demo"]
112
  q_words = [get_stem(w) for w in query.lower().split() if len(w)>2]
113
  relevant_words = [w for w in q_words if w not in STOP_WORDS]
114
 
115
  if not relevant_words:
116
- return {"result": "Dazu habe ich keine spezifischen Informationen."}
117
 
118
  best_doc = None
119
  best_score = 0
@@ -137,13 +133,21 @@ async def search(request: Request):
137
  print(f"🏆 TREFFER ({best_score}): {best_doc.get('question')}")
138
  return {"result": best_doc.get("answer")}
139
  else:
140
- print(f"⚠️ KEIN TREFFER (Max: {best_score})")
141
- return {"result": "Dazu habe ich leider keine Informationen."}
142
 
143
  except Exception as e:
144
  print(f"❌ SEARCH ERROR: {e}")
145
- return {"result": "Fehler."}
 
 
 
 
 
 
 
 
 
146
 
147
  @app.get("/")
148
  def home():
149
- return {"status": "Online", "endpoints": ["/check_availability", "/search"]}
 
13
  COLLECTION_INBOX = "inbox"
14
  KNOWLEDGE_CACHE = []
15
 
16
+ # --- FIREBASE START ---
17
  db = None
18
  try:
19
  key = os.environ.get("FIREBASE_KEY")
 
50
  return w
51
 
52
  # ==========================================
53
+ # 1. TOOL: VERFÜGBARKEIT PRÜFEN
 
54
  # ==========================================
55
  @app.post("/check_availability")
56
  async def check_availability(request: Request):
 
58
 
59
  today = datetime.now().strftime("%Y-%m-%d")
60
  status = "available"
61
+ # Standard-Nachricht
62
+ message = "Normaler Betrieb."
63
 
64
  try:
65
  if db:
66
  rules = db.collection(COLLECTION_RULES).where("active", "==", True).stream()
67
  for r in rules:
68
  rd = r.to_dict()
 
69
  if rd.get('start_date') <= today <= rd.get('end_date'):
70
  print(f"🛑 REGEL GEFUNDEN: {rd.get('name')}")
71
  status = "unavailable"
72
+ # Hier bauen wir den Satz für den Agenten
73
+ message = rd.get('instruction_text')
74
  break
75
  except Exception as e:
76
  print(f"❌ ERROR CHECK: {e}")
77
+ return {"result": json.dumps({"status": "available", "instruction": "Fehler ignoriert, normal arbeiten."})}
78
 
79
  print(f"👉 RESULTAT: {status} | {message}")
80
 
81
+ # Rückgabe als JSON-String für das LLM
82
  return {
83
  "result": json.dumps({
84
  "status": status,
 
87
  }
88
 
89
  # ==========================================
90
+ # 2. TOOL: WISSENSSUCHE (Preise etc.)
 
91
  # ==========================================
92
  @app.post("/search")
93
  async def search(request: Request):
 
95
  data = await request.json()
96
  query = ""
97
 
 
98
  if "search_query" in data: query = data["search_query"]
99
  elif "message" in data and "toolCalls" in data["message"]:
100
  args = data["message"]["toolCalls"][0]["function"]["arguments"]
 
104
  print(f"🔎 FRAGE: '{query}'")
105
  if not query: return {"result": "Akustik-Fehler."}
106
 
 
107
  STOP_WORDS = ["capaneo", "udo", "schober", "firma", "gmbh", "hallo", "demo"]
108
  q_words = [get_stem(w) for w in query.lower().split() if len(w)>2]
109
  relevant_words = [w for w in q_words if w not in STOP_WORDS]
110
 
111
  if not relevant_words:
112
+ return {"result": "Dazu habe ich keine Informationen."}
113
 
114
  best_doc = None
115
  best_score = 0
 
133
  print(f"🏆 TREFFER ({best_score}): {best_doc.get('question')}")
134
  return {"result": best_doc.get("answer")}
135
  else:
136
+ return {"result": "Dazu habe ich keine Informationen."}
 
137
 
138
  except Exception as e:
139
  print(f"❌ SEARCH ERROR: {e}")
140
+ return {"result": "Systemfehler."}
141
+
142
+ # ==========================================
143
+ # 3. DUMMY ENDPUNKT (MÜLLSCHLUCKER)
144
+ # Fängt die 404 Fehler ab!
145
+ # ==========================================
146
+ @app.post("/vapi-incoming")
147
+ async def dummy_incoming(request: Request):
148
+ # Wir sagen Vapi einfach "Alles OK", damit er Ruhe gibt
149
+ return {"status": "ok"}
150
 
151
  @app.get("/")
152
  def home():
153
+ return {"status": "Online"}