Spaces:
Running
Running
Update chat_handler.py
Browse files- chat_handler.py +57 -26
chat_handler.py
CHANGED
@@ -20,39 +20,70 @@ async def handle_chat(msg: Message, request: Request, app, service_config, sessi
|
|
20 |
if llm_model.model is None or llm_model.tokenizer is None:
|
21 |
return {"error": f"{project_name} için model yüklenmedi."}
|
22 |
|
23 |
-
detected_intent, intent_conf = await detect_intent(user_input, project_name)
|
24 |
-
log(f"🎯 Intent tespit edildi: {detected_intent}, Confidence: {intent_conf:.2f}")
|
25 |
-
|
26 |
current_intent = session.last_intent
|
27 |
awaiting_variable = session.awaiting_variable
|
28 |
|
29 |
-
#
|
30 |
-
if awaiting_variable:
|
31 |
-
|
32 |
-
if
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
38 |
session.awaiting_variable = None
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
else:
|
52 |
-
log("⚠️ Parametre bulunamadı → fallback veya yeni intent bekleniyor")
|
53 |
return {"response": random.choice(project_config["fallback_answers"])}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
-
# Normal intent akışı
|
56 |
intent_is_valid = (
|
57 |
detected_intent and
|
58 |
intent_conf > intent_conf_threshold and
|
|
|
20 |
if llm_model.model is None or llm_model.tokenizer is None:
|
21 |
return {"error": f"{project_name} için model yüklenmedi."}
|
22 |
|
|
|
|
|
|
|
23 |
current_intent = session.last_intent
|
24 |
awaiting_variable = session.awaiting_variable
|
25 |
|
26 |
+
# 🧩 1. PARAMETRE TAMAMLAMA MODU
|
27 |
+
if awaiting_variable and current_intent:
|
28 |
+
log(f"🔄 Parametre tamamlama modu: intent={current_intent}, beklenen={awaiting_variable}")
|
29 |
+
intent_def = next(i for i in project_intents if i["name"] == current_intent)
|
30 |
+
pattern_list = intent_def.get("variables", [])
|
31 |
+
variable_format_map = intent_def.get("variable_formats", {})
|
32 |
+
data_formats = service_config.data_formats
|
33 |
+
|
34 |
+
extracted = extract_parameters(pattern_list, user_input)
|
35 |
+
if extracted:
|
36 |
+
for p in extracted:
|
37 |
+
session.variables[p["key"]] = p["value"]
|
38 |
+
log(f"✅ Parametre dolduruldu: {p['key']} = {p['value']}")
|
39 |
session.awaiting_variable = None
|
40 |
+
|
41 |
+
# Doğrulama + execute_intent’e geçiş
|
42 |
+
is_valid, validation_errors = validate_variable_formats(session.variables, variable_format_map, data_formats)
|
43 |
+
log(f"📛 Validasyon hataları: {validation_errors}")
|
44 |
+
|
45 |
+
expected_vars = list(variable_format_map.keys())
|
46 |
+
missing_vars = [v for v in expected_vars if v not in session.variables]
|
47 |
+
log(f"📌 Beklenen parametreler: {expected_vars}, Eksik: {missing_vars}")
|
48 |
+
|
49 |
+
if not is_valid:
|
50 |
+
session.awaiting_variable = list(validation_errors.keys())[0]
|
51 |
+
return {"response": list(validation_errors.values())[0]}
|
52 |
+
|
53 |
+
if missing_vars:
|
54 |
+
if len(missing_vars) > 1:
|
55 |
+
ordered_list = ", ".join(missing_vars)
|
56 |
+
return {"response": f"Lütfen şu bilgileri sırayla belirtir misiniz: {ordered_list}."}
|
57 |
+
else:
|
58 |
+
session.awaiting_variable = missing_vars[0]
|
59 |
+
return {"response": f"Lütfen {missing_vars[0]} bilgisini belirtir misiniz?"}
|
60 |
+
|
61 |
+
log("🚀 execute_intent() çağrılıyor...")
|
62 |
+
result = execute_intent(
|
63 |
+
current_intent,
|
64 |
+
user_input,
|
65 |
+
session.__dict__,
|
66 |
+
{i["name"]: i for i in project_intents},
|
67 |
+
data_formats,
|
68 |
+
project_name,
|
69 |
+
service_config
|
70 |
+
)
|
71 |
+
if "reply" in result:
|
72 |
+
return {"reply": result["reply"]}
|
73 |
+
elif "errors" in result:
|
74 |
+
return {"response": list(result["errors"].values())[0]}
|
75 |
else:
|
|
|
76 |
return {"response": random.choice(project_config["fallback_answers"])}
|
77 |
+
else:
|
78 |
+
# ❌ Parametre yine çıkarılamadı → konu değişmiş olabilir
|
79 |
+
log("⚠️ Parametre bulunamadı → intent detect tetikleniyor")
|
80 |
+
session.awaiting_variable = None
|
81 |
+
session.variables = {}
|
82 |
+
|
83 |
+
# 🧩 2. NORMAL INTENT DEĞERLENDİRMESİ
|
84 |
+
detected_intent, intent_conf = await detect_intent(user_input, project_name)
|
85 |
+
log(f"🎯 Intent tespit edildi: {detected_intent}, Confidence: {intent_conf:.2f}")
|
86 |
|
|
|
87 |
intent_is_valid = (
|
88 |
detected_intent and
|
89 |
intent_conf > intent_conf_threshold and
|