Spaces:
Paused
Paused
Delete chat_handler_debug.py
Browse files- chat_handler_debug.py +0 -135
chat_handler_debug.py
DELETED
@@ -1,135 +0,0 @@
|
|
1 |
-
from fastapi import Request
|
2 |
-
from fastapi.responses import JSONResponse
|
3 |
-
import traceback, random
|
4 |
-
from intent import extract_parameters, validate_variable_formats, detect_intent
|
5 |
-
from intent_api import execute_intent
|
6 |
-
from llm_model import generate_response, get_model, get_tokenizer, Message
|
7 |
-
from log import log
|
8 |
-
|
9 |
-
DEBUG = True
|
10 |
-
|
11 |
-
async def handle_chat(msg: Message, request: Request, app, s_config):
|
12 |
-
user_input = msg.user_input.strip()
|
13 |
-
session_id = request.headers.get("X-Session-ID", "demo-session")
|
14 |
-
|
15 |
-
if not hasattr(app.state, "session_store"):
|
16 |
-
app.state.session_store = {}
|
17 |
-
|
18 |
-
session_store = getattr(app.state, "session_store", {})
|
19 |
-
session = session_store.get(session_id, {
|
20 |
-
"session_id": session_id,
|
21 |
-
"variables": {},
|
22 |
-
"auth_tokens": {},
|
23 |
-
"last_intent": None,
|
24 |
-
"awaiting_variable": None
|
25 |
-
})
|
26 |
-
|
27 |
-
try:
|
28 |
-
if get_model() is None or get_tokenizer() is None:
|
29 |
-
return {"error": "Model yüklenmedi."}
|
30 |
-
|
31 |
-
detected_intent, intent_conf = await detect_intent(user_input)
|
32 |
-
if DEBUG:
|
33 |
-
log(f"🎯 Intent tespiti: {detected_intent}, Confidence: {intent_conf:.2f}")
|
34 |
-
|
35 |
-
current_intent = session.get("last_intent")
|
36 |
-
awaiting_variable = session.get("awaiting_variable")
|
37 |
-
|
38 |
-
if DEBUG:
|
39 |
-
log(f"📦 Session: {session}")
|
40 |
-
log(f"🧩 Awaiting: {awaiting_variable}, Last Intent: {current_intent}")
|
41 |
-
|
42 |
-
if (
|
43 |
-
awaiting_variable and
|
44 |
-
detected_intent and
|
45 |
-
detected_intent != current_intent and
|
46 |
-
intent_conf > s_config.INTENT_CONFIDENCE_THRESHOLD
|
47 |
-
):
|
48 |
-
log("🧹 Konu değişikliği algılandı → context sıfırlanıyor")
|
49 |
-
session["awaiting_variable"] = None
|
50 |
-
session["variables"] = {}
|
51 |
-
session["last_intent"] = detected_intent
|
52 |
-
current_intent = detected_intent
|
53 |
-
|
54 |
-
intent_is_valid = (
|
55 |
-
detected_intent and
|
56 |
-
intent_conf > s_config.INTENT_CONFIDENCE_THRESHOLD and
|
57 |
-
detected_intent in s_config.INTENT_DEFINITIONS
|
58 |
-
)
|
59 |
-
if DEBUG:
|
60 |
-
log(f"✅ Intent geçerli mi?: {intent_is_valid}")
|
61 |
-
|
62 |
-
if intent_is_valid:
|
63 |
-
session["last_intent"] = detected_intent
|
64 |
-
definition = s_config.INTENT_DEFINITIONS[detected_intent]
|
65 |
-
pattern_list = definition.get("variables", [])
|
66 |
-
data_formats = s_config.DATA_FORMATS
|
67 |
-
variable_format_map = definition.get("variable_formats", {})
|
68 |
-
|
69 |
-
if awaiting_variable:
|
70 |
-
extracted = extract_parameters(pattern_list, user_input)
|
71 |
-
for p in extracted:
|
72 |
-
if p["key"] == awaiting_variable:
|
73 |
-
session["variables"][awaiting_variable] = p["value"]
|
74 |
-
session["awaiting_variable"] = None
|
75 |
-
log(f"✅ Awaiting parametre tamamlandı: {awaiting_variable} = {p['value']}")
|
76 |
-
break
|
77 |
-
|
78 |
-
extracted = extract_parameters(pattern_list, user_input)
|
79 |
-
variables = {p["key"]: p["value"] for p in extracted}
|
80 |
-
session.setdefault("variables", {}).update(variables)
|
81 |
-
|
82 |
-
if DEBUG:
|
83 |
-
log(f"🧪 Tespit edilen parametreler: {variables}")
|
84 |
-
|
85 |
-
is_valid, validation_errors = validate_variable_formats(session["variables"], variable_format_map, data_formats)
|
86 |
-
if DEBUG:
|
87 |
-
log(f"📛 Validasyon hataları: {validation_errors}")
|
88 |
-
|
89 |
-
if not is_valid:
|
90 |
-
session["awaiting_variable"] = list(validation_errors.keys())[0]
|
91 |
-
session_store[session_id] = session
|
92 |
-
app.state.session_store = session_store
|
93 |
-
return {"response": list(validation_errors.values())[0]}
|
94 |
-
|
95 |
-
expected_vars = list(variable_format_map.keys())
|
96 |
-
missing_vars = [v for v in expected_vars if v not in session["variables"]]
|
97 |
-
if DEBUG:
|
98 |
-
log(f"📌 Beklenen parametreler: {expected_vars}, Eksik: {missing_vars}")
|
99 |
-
|
100 |
-
if missing_vars:
|
101 |
-
session["awaiting_variable"] = missing_vars[0]
|
102 |
-
session_store[session_id] = session
|
103 |
-
app.state.session_store = session_store
|
104 |
-
return {"response": f"Lütfen {missing_vars[0]} bilgisini belirtir misiniz?"}
|
105 |
-
|
106 |
-
log("🚀 execute_intent() çağrılıyor...")
|
107 |
-
result = execute_intent(
|
108 |
-
detected_intent,
|
109 |
-
user_input,
|
110 |
-
session,
|
111 |
-
s_config.INTENT_DEFINITIONS,
|
112 |
-
s_config.DATA_FORMATS
|
113 |
-
)
|
114 |
-
if "reply" in result:
|
115 |
-
session_store[session_id] = result["session"]
|
116 |
-
app.state.session_store = session_store
|
117 |
-
return {"reply": result["reply"]}
|
118 |
-
elif "errors" in result:
|
119 |
-
session_store[session_id] = result["session"]
|
120 |
-
app.state.session_store = session_store
|
121 |
-
return {"response": list(result["errors"].values())[0]}
|
122 |
-
else:
|
123 |
-
return {"response": random.choice(s_config.FALLBACK_ANSWERS)}
|
124 |
-
|
125 |
-
log("🤖 execute_intent çağrılmadı → LLM fallback devrede")
|
126 |
-
session["awaiting_variable"] = None
|
127 |
-
session["variables"] = {}
|
128 |
-
response, response_conf = await generate_response(user_input, s_config)
|
129 |
-
if response_conf is not None and response_conf < s_config.LLM_CONFIDENCE_THRESHOLD:
|
130 |
-
return {"response": random.choice(s_config.FALLBACK_ANSWERS)}
|
131 |
-
return {"response": response}
|
132 |
-
|
133 |
-
except Exception as e:
|
134 |
-
traceback.print_exc()
|
135 |
-
return JSONResponse(content={"error": str(e)}, status_code=500)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|