Spaces:
Building
Building
Create chat_handler.py
Browse files- chat_handler.py +93 -0
chat_handler.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Flare – Chat Handler
|
3 |
+
~~~~~~~~~~~~~~~~~~~~
|
4 |
+
Intent → Parametre → API → Humanize akışı.
|
5 |
+
"""
|
6 |
+
|
7 |
+
import json
|
8 |
+
import re
|
9 |
+
from datetime import datetime
|
10 |
+
from typing import Dict, List, Optional
|
11 |
+
|
12 |
+
from fastapi import APIRouter, HTTPException
|
13 |
+
|
14 |
+
from config_provider import (ConfigProvider, IntentConfig,
|
15 |
+
ParameterConfig, VersionConfig)
|
16 |
+
from llm_connector import ask_llm
|
17 |
+
from prompt_builder import (build_intent_prompt, build_param_prompt,
|
18 |
+
build_api_humanize_prompt)
|
19 |
+
from api_executor import call_api
|
20 |
+
from session import SessionStore
|
21 |
+
from log_utils import log
|
22 |
+
|
23 |
+
router = APIRouter()
|
24 |
+
cfg = ConfigProvider.get()
|
25 |
+
|
26 |
+
# ==================== Helpers ===============================================
|
27 |
+
def _detect_intent(version: VersionConfig, user_input: str) -> Optional[IntentConfig]:
|
28 |
+
for intent in version.intents:
|
29 |
+
prompt = build_intent_prompt(version.general_prompt, intent)
|
30 |
+
llm_resp = ask_llm(prompt, user_input, mode="classification")
|
31 |
+
if intent.name in llm_resp.lower():
|
32 |
+
return intent
|
33 |
+
return None
|
34 |
+
|
35 |
+
|
36 |
+
def _extract_params(intent: IntentConfig, version: VersionConfig,
|
37 |
+
user_input: str,
|
38 |
+
current_vars: Dict[str, str]) -> Dict[str, str]:
|
39 |
+
missing = [p for p in intent.parameters
|
40 |
+
if p.name not in current_vars]
|
41 |
+
if not missing:
|
42 |
+
return current_vars
|
43 |
+
|
44 |
+
prompt_base = build_intent_prompt(version.general_prompt, intent)
|
45 |
+
prompt = build_param_prompt(prompt_base, missing)
|
46 |
+
llm_json = ask_llm(prompt, user_input, mode="json")
|
47 |
+
current_vars.update(llm_json)
|
48 |
+
return current_vars
|
49 |
+
|
50 |
+
|
51 |
+
# ==================== Endpoint ==============================================
|
52 |
+
@router.post("/chat")
|
53 |
+
def chat(session_id: str, user_input: str):
|
54 |
+
session = SessionStore.get(session_id)
|
55 |
+
project = cfg.projects[0] # simplistic: first project
|
56 |
+
version = next(v for v in project.versions if v.published)
|
57 |
+
|
58 |
+
# 1) Intent Detection
|
59 |
+
intent = _detect_intent(version, user_input)
|
60 |
+
if not intent:
|
61 |
+
reply = ask_llm(version.general_prompt, user_input)
|
62 |
+
session.add_turn("assistant", reply)
|
63 |
+
return {"reply": reply}
|
64 |
+
|
65 |
+
# 2) Param Extraction
|
66 |
+
vars_ = session.variables
|
67 |
+
vars_ = _extract_params(intent, version, user_input, vars_)
|
68 |
+
|
69 |
+
# 3) Eksik parametre kontrolü
|
70 |
+
missing = [p for p in intent.parameters if p.required and p.name not in vars_]
|
71 |
+
if missing:
|
72 |
+
ask_prompts = "\n".join(p.invalid_prompt or f"{p.name} gerekti" for p in missing)
|
73 |
+
reply = ask_llm(version.general_prompt, ask_prompts)
|
74 |
+
return {"reply": reply}
|
75 |
+
|
76 |
+
# 4) API
|
77 |
+
api_cfg = cfg.apis[intent.action]
|
78 |
+
try:
|
79 |
+
resp = call_api(api_cfg, vars_)
|
80 |
+
except Exception as e:
|
81 |
+
log(f"❌ API error: {e}")
|
82 |
+
reply = intent.fallback_error_prompt or "İşlem sırasında hata oluştu."
|
83 |
+
return {"reply": reply}
|
84 |
+
|
85 |
+
# 5) Humanize
|
86 |
+
human_prompt = build_api_humanize_prompt(
|
87 |
+
version.general_prompt,
|
88 |
+
api_cfg.response_prompt,
|
89 |
+
json.dumps(resp.json(), ensure_ascii=False, indent=2)
|
90 |
+
)
|
91 |
+
reply = ask_llm(human_prompt, "")
|
92 |
+
session.add_turn("assistant", reply)
|
93 |
+
return {"reply": reply, "session": session.to_dict()}
|