Spaces:
Building
Building
Update chat_handler.py
Browse files- chat_handler.py +29 -28
chat_handler.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
"""
|
2 |
-
Flare β Chat Handler (
|
3 |
-
|
|
|
|
|
4 |
"""
|
5 |
|
6 |
import re, json, uuid, sys, httpx, commentjson
|
@@ -12,10 +14,8 @@ from commentjson import JSONLibraryException
|
|
12 |
|
13 |
from prompt_builder import build_intent_prompt, build_parameter_prompt, log
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
# --------------------------------------------------------------------------- #
|
18 |
-
def load_config(path: str = "service_config.jsonc") -> dict:
|
19 |
try:
|
20 |
with open(path, encoding="utf-8") as f:
|
21 |
cfg = commentjson.load(f)
|
@@ -31,9 +31,7 @@ APIS = {a["name"]: a for a in CFG["apis"]}
|
|
31 |
SPARK_URL = CFG["config"]["spark_endpoint"].rstrip("/") + "/generate"
|
32 |
ALLOWED_INTENTS = {"flight-booking", "flight-info", "booking-cancel"}
|
33 |
|
34 |
-
#
|
35 |
-
# SESSION
|
36 |
-
# --------------------------------------------------------------------------- #
|
37 |
class Session:
|
38 |
def __init__(self, project_name: str):
|
39 |
self.id = str(uuid.uuid4())
|
@@ -45,9 +43,7 @@ class Session:
|
|
45 |
|
46 |
SESSIONS: Dict[str, Session] = {}
|
47 |
|
48 |
-
#
|
49 |
-
# SPARK CLIENT
|
50 |
-
# --------------------------------------------------------------------------- #
|
51 |
async def spark_generate(session: Session,
|
52 |
system_prompt: str,
|
53 |
user_input: str) -> str:
|
@@ -61,11 +57,11 @@ async def spark_generate(session: Session,
|
|
61 |
r = await c.post(SPARK_URL, json=payload)
|
62 |
r.raise_for_status()
|
63 |
data = r.json()
|
64 |
-
return (data.get("assistant") or
|
|
|
|
|
65 |
|
66 |
-
#
|
67 |
-
# FASTAPI ROUTER
|
68 |
-
# --------------------------------------------------------------------------- #
|
69 |
router = APIRouter()
|
70 |
|
71 |
@router.get("/")
|
@@ -80,9 +76,7 @@ class ChatResponse(BaseModel):
|
|
80 |
session_id: str
|
81 |
answer: str
|
82 |
|
83 |
-
#
|
84 |
-
# ENDPOINTS
|
85 |
-
# --------------------------------------------------------------------------- #
|
86 |
@router.post("/start_session", response_model=ChatResponse)
|
87 |
async def start_session(req: StartSessionRequest):
|
88 |
if req.project_name not in PROJECTS:
|
@@ -100,35 +94,42 @@ async def chat(body: ChatBody, x_session_id: str = Header(...)):
|
|
100 |
user_msg = body.user_input.strip()
|
101 |
s.history.append({"role": "user", "content": user_msg})
|
102 |
|
103 |
-
#
|
104 |
if s.awaiting:
|
105 |
answer = await _followup(s, user_msg)
|
106 |
s.history.append({"role": "assistant", "content": answer})
|
107 |
return ChatResponse(session_id=s.id, answer=answer)
|
108 |
|
109 |
-
#
|
110 |
-
gen_prompt
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
-
# small-talk?
|
114 |
if not intent_raw.startswith("#DETECTED_INTENT:"):
|
115 |
-
|
116 |
-
|
|
|
117 |
|
118 |
intent_name = intent_raw.split(":", 1)[1].strip()
|
119 |
|
120 |
-
# short-message guard
|
121 |
if len(user_msg.split()) < 3:
|
122 |
clean = intent_raw.split("#DETECTED_INTENT")[0].split("\nassistant")[0].strip()
|
123 |
s.history.append({"role": "assistant", "content": clean})
|
124 |
return ChatResponse(session_id=s.id, answer=clean)
|
125 |
|
126 |
-
# allowed
|
127 |
if intent_name not in ALLOWED_INTENTS:
|
128 |
clean = intent_raw.split("#DETECTED_INTENT")[0].split("\nassistant")[0].strip()
|
129 |
s.history.append({"role": "assistant", "content": clean})
|
130 |
return ChatResponse(session_id=s.id, answer=clean)
|
131 |
|
|
|
132 |
intent_cfg = _find_intent(s.project, intent_name)
|
133 |
if not intent_cfg:
|
134 |
err = "ΓzgΓΌnΓΌm, anlayamadΔ±m."
|
|
|
1 |
"""
|
2 |
+
Flare β Chat Handler (small-talk clean fix)
|
3 |
+
===========================================
|
4 |
+
β’ EΔer model Γ§Δ±ktΔ±sΔ± #DETECTED_INTENT ile BAΕLAMIYORSA
|
5 |
+
cevabΔ± '#DETECTED_INTENT' veya '\nassistant' gΓΆrΓΌldΓΌΔΓΌ yere kadar kes.
|
6 |
"""
|
7 |
|
8 |
import re, json, uuid, sys, httpx, commentjson
|
|
|
14 |
|
15 |
from prompt_builder import build_intent_prompt, build_parameter_prompt, log
|
16 |
|
17 |
+
# ββ Config yΓΌkleme (deΔiΕmedi) βββββββββββββββββββββββββββββββββββββββββββββββ
|
18 |
+
def load_config(path="service_config.jsonc") -> dict:
|
|
|
|
|
19 |
try:
|
20 |
with open(path, encoding="utf-8") as f:
|
21 |
cfg = commentjson.load(f)
|
|
|
31 |
SPARK_URL = CFG["config"]["spark_endpoint"].rstrip("/") + "/generate"
|
32 |
ALLOWED_INTENTS = {"flight-booking", "flight-info", "booking-cancel"}
|
33 |
|
34 |
+
# ββ Session objesi (deΔiΕmedi) βββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
|
35 |
class Session:
|
36 |
def __init__(self, project_name: str):
|
37 |
self.id = str(uuid.uuid4())
|
|
|
43 |
|
44 |
SESSIONS: Dict[str, Session] = {}
|
45 |
|
46 |
+
# ββ Spark client (deΔiΕmedi) βββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
|
47 |
async def spark_generate(session: Session,
|
48 |
system_prompt: str,
|
49 |
user_input: str) -> str:
|
|
|
57 |
r = await c.post(SPARK_URL, json=payload)
|
58 |
r.raise_for_status()
|
59 |
data = r.json()
|
60 |
+
return (data.get("assistant") or
|
61 |
+
data.get("model_answer") or
|
62 |
+
data.get("text", "")).strip()
|
63 |
|
64 |
+
# ββ FastAPI router setup (deΔiΕmedi) βββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
|
65 |
router = APIRouter()
|
66 |
|
67 |
@router.get("/")
|
|
|
76 |
session_id: str
|
77 |
answer: str
|
78 |
|
79 |
+
# ββ Endpoints ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
|
80 |
@router.post("/start_session", response_model=ChatResponse)
|
81 |
async def start_session(req: StartSessionRequest):
|
82 |
if req.project_name not in PROJECTS:
|
|
|
94 |
user_msg = body.user_input.strip()
|
95 |
s.history.append({"role": "user", "content": user_msg})
|
96 |
|
97 |
+
# follow-up? ----------------------------------------------------------------
|
98 |
if s.awaiting:
|
99 |
answer = await _followup(s, user_msg)
|
100 |
s.history.append({"role": "assistant", "content": answer})
|
101 |
return ChatResponse(session_id=s.id, answer=answer)
|
102 |
|
103 |
+
# intent detect -------------------------------------------------------------
|
104 |
+
gen_prompt = s.project["versions"][0]["general_prompt"]
|
105 |
+
intents_cfg = s.project["versions"][0]["intents"]
|
106 |
+
intent_raw = await spark_generate(
|
107 |
+
s,
|
108 |
+
build_intent_prompt(gen_prompt, s.history, user_msg, intents_cfg),
|
109 |
+
user_msg
|
110 |
+
)
|
111 |
|
112 |
+
# small-talk? ---------------------------------------------------------------
|
113 |
if not intent_raw.startswith("#DETECTED_INTENT:"):
|
114 |
+
clean = intent_raw.split("#DETECTED_INTENT")[0].split("\nassistant")[0].strip()
|
115 |
+
s.history.append({"role": "assistant", "content": clean})
|
116 |
+
return ChatResponse(session_id=s.id, answer=clean)
|
117 |
|
118 |
intent_name = intent_raw.split(":", 1)[1].strip()
|
119 |
|
120 |
+
# short-message guard -------------------------------------------------------
|
121 |
if len(user_msg.split()) < 3:
|
122 |
clean = intent_raw.split("#DETECTED_INTENT")[0].split("\nassistant")[0].strip()
|
123 |
s.history.append({"role": "assistant", "content": clean})
|
124 |
return ChatResponse(session_id=s.id, answer=clean)
|
125 |
|
126 |
+
# allowed intent? -----------------------------------------------------------
|
127 |
if intent_name not in ALLOWED_INTENTS:
|
128 |
clean = intent_raw.split("#DETECTED_INTENT")[0].split("\nassistant")[0].strip()
|
129 |
s.history.append({"role": "assistant", "content": clean})
|
130 |
return ChatResponse(session_id=s.id, answer=clean)
|
131 |
|
132 |
+
# devam: parametre akΔ±ΕΔ± (deΔiΕmedi) ----------------------------------------
|
133 |
intent_cfg = _find_intent(s.project, intent_name)
|
134 |
if not intent_cfg:
|
135 |
err = "ΓzgΓΌnΓΌm, anlayamadΔ±m."
|