Spaces:
Paused
Paused
Update chat_handler.py
Browse files- chat_handler.py +29 -22
chat_handler.py
CHANGED
@@ -11,10 +11,11 @@ async def handle_chat(msg: Message, request: Request, app, service_config, sessi
|
|
11 |
try:
|
12 |
user_input = msg.user_input.strip()
|
13 |
log(f"💬 Kullanıcı input'u: '{user_input}'")
|
14 |
-
|
15 |
project_name = session.project_name
|
16 |
project_config = service_config.get_project_llm_config(project_name)
|
17 |
project_intents = service_config.get_project_intents(project_name)
|
|
|
18 |
|
19 |
if llm_model.model is None or llm_model.tokenizer is None:
|
20 |
return {"error": f"{project_name} için model yüklenmedi."}
|
@@ -25,21 +26,36 @@ async def handle_chat(msg: Message, request: Request, app, service_config, sessi
|
|
25 |
current_intent = session.last_intent
|
26 |
awaiting_variable = session.awaiting_variable
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
|
|
40 |
intent_is_valid = (
|
41 |
detected_intent and
|
42 |
-
intent_conf >
|
43 |
any(i["name"] == detected_intent for i in project_intents)
|
44 |
)
|
45 |
log(f"✅ Intent geçerli mi?: {intent_is_valid}")
|
@@ -51,15 +67,6 @@ async def handle_chat(msg: Message, request: Request, app, service_config, sessi
|
|
51 |
variable_format_map = intent_def.get("variable_formats", {})
|
52 |
data_formats = service_config.data_formats
|
53 |
|
54 |
-
if awaiting_variable:
|
55 |
-
extracted = extract_parameters(pattern_list, user_input)
|
56 |
-
for p in extracted:
|
57 |
-
if p["key"] == awaiting_variable:
|
58 |
-
session.variables[awaiting_variable] = p["value"]
|
59 |
-
session.awaiting_variable = None
|
60 |
-
log(f"✅ Awaiting parametre tamamlandı: {awaiting_variable} = {p['value']}")
|
61 |
-
break
|
62 |
-
|
63 |
extracted = extract_parameters(pattern_list, user_input)
|
64 |
variables = {p["key"]: p["value"] for p in extracted}
|
65 |
session.variables.update(variables)
|
|
|
11 |
try:
|
12 |
user_input = msg.user_input.strip()
|
13 |
log(f"💬 Kullanıcı input'u: '{user_input}'")
|
14 |
+
|
15 |
project_name = session.project_name
|
16 |
project_config = service_config.get_project_llm_config(project_name)
|
17 |
project_intents = service_config.get_project_intents(project_name)
|
18 |
+
intent_conf_threshold = project_config["intent_confidence_treshold"]
|
19 |
|
20 |
if llm_model.model is None or llm_model.tokenizer is None:
|
21 |
return {"error": f"{project_name} için model yüklenmedi."}
|
|
|
26 |
current_intent = session.last_intent
|
27 |
awaiting_variable = session.awaiting_variable
|
28 |
|
29 |
+
# Eğer parametre bekleniyorsa:
|
30 |
+
if awaiting_variable:
|
31 |
+
# Intent değişti mi?
|
32 |
+
if (
|
33 |
+
detected_intent and
|
34 |
+
detected_intent != current_intent and
|
35 |
+
intent_conf > intent_conf_threshold
|
36 |
+
):
|
37 |
+
log("🧹 Konu değişikliği algılandı → context sıfırlanıyor")
|
38 |
+
session.awaiting_variable = None
|
39 |
+
session.variables = {}
|
40 |
+
session.last_intent = detected_intent
|
41 |
+
current_intent = detected_intent
|
42 |
+
|
43 |
+
# Intent yoksa veya çok düşük confidence → parametre extract et
|
44 |
+
elif not detected_intent or intent_conf < intent_conf_threshold:
|
45 |
+
extracted = extract_parameters([], user_input)
|
46 |
+
if extracted:
|
47 |
+
for p in extracted:
|
48 |
+
session.variables[p["key"]] = p["value"]
|
49 |
+
log(f"✅ Parametre dolduruldu: {p['key']} = {p['value']}")
|
50 |
+
session.awaiting_variable = None
|
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
|
59 |
any(i["name"] == detected_intent for i in project_intents)
|
60 |
)
|
61 |
log(f"✅ Intent geçerli mi?: {intent_is_valid}")
|
|
|
67 |
variable_format_map = intent_def.get("variable_formats", {})
|
68 |
data_formats = service_config.data_formats
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
extracted = extract_parameters(pattern_list, user_input)
|
71 |
variables = {p["key"]: p["value"] for p in extracted}
|
72 |
session.variables.update(variables)
|