Spaces:
Paused
Paused
Update chat_handler.py
Browse files- chat_handler.py +61 -27
chat_handler.py
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
-
Flare โ Chat Handler (small-talk
|
| 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,8 +12,31 @@ from commentjson import JSONLibraryException
|
|
| 14 |
|
| 15 |
from prompt_builder import build_intent_prompt, build_parameter_prompt, log
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
try:
|
| 20 |
with open(path, encoding="utf-8") as f:
|
| 21 |
cfg = commentjson.load(f)
|
|
@@ -31,7 +52,10 @@ 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 |
class Session:
|
| 36 |
def __init__(self, project_name: str):
|
| 37 |
self.id = str(uuid.uuid4())
|
|
@@ -43,7 +67,10 @@ class Session:
|
|
| 43 |
|
| 44 |
SESSIONS: Dict[str, Session] = {}
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
async def spark_generate(session: Session,
|
| 48 |
system_prompt: str,
|
| 49 |
user_input: str) -> str:
|
|
@@ -56,12 +83,13 @@ async def spark_generate(session: Session,
|
|
| 56 |
async with httpx.AsyncClient(timeout=60) as c:
|
| 57 |
r = await c.post(SPARK_URL, json=payload)
|
| 58 |
r.raise_for_status()
|
| 59 |
-
|
| 60 |
-
return (
|
| 61 |
-
data.get("model_answer") or
|
| 62 |
-
data.get("text", "")).strip()
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
router = APIRouter()
|
| 66 |
|
| 67 |
@router.get("/")
|
|
@@ -76,14 +104,17 @@ class ChatResponse(BaseModel):
|
|
| 76 |
session_id: str
|
| 77 |
answer: str
|
| 78 |
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
| 80 |
@router.post("/start_session", response_model=ChatResponse)
|
| 81 |
async def start_session(req: StartSessionRequest):
|
| 82 |
if req.project_name not in PROJECTS:
|
| 83 |
raise HTTPException(404, "Unknown project")
|
| 84 |
s = Session(req.project_name)
|
| 85 |
SESSIONS[s.id] = s
|
| 86 |
-
return ChatResponse(session_id=s.id, answer="
|
| 87 |
|
| 88 |
@router.post("/chat", response_model=ChatResponse)
|
| 89 |
async def chat(body: ChatBody, x_session_id: str = Header(...)):
|
|
@@ -94,42 +125,45 @@ async def chat(body: ChatBody, x_session_id: str = Header(...)):
|
|
| 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
|
| 105 |
-
intents_cfg
|
| 106 |
-
intent_raw
|
| 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
|
|
|
|
|
|
|
|
|
|
| 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 |
-
#
|
| 121 |
if len(user_msg.split()) < 3:
|
| 122 |
-
clean = intent_raw
|
| 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
|
| 129 |
s.history.append({"role": "assistant", "content": clean})
|
| 130 |
return ChatResponse(session_id=s.id, answer=clean)
|
| 131 |
|
| 132 |
-
#
|
| 133 |
intent_cfg = _find_intent(s.project, intent_name)
|
| 134 |
if not intent_cfg:
|
| 135 |
err = "รzgรผnรผm, anlayamadฤฑm."
|
|
|
|
| 1 |
"""
|
| 2 |
+
Flare โ Chat Handler (small-talk trim + resmรฎ selamlama)
|
| 3 |
+
=========================================================
|
|
|
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import re, json, uuid, sys, httpx, commentjson
|
|
|
|
| 12 |
|
| 13 |
from prompt_builder import build_intent_prompt, build_parameter_prompt, log
|
| 14 |
|
| 15 |
+
|
| 16 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 17 |
+
# HELPERS
|
| 18 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 19 |
+
def _trim_smalltalk(raw: str) -> str:
|
| 20 |
+
"""
|
| 21 |
+
Kฤฑsa selamlaลma รงฤฑktฤฑsฤฑnda yalnฤฑzca ilk blok (etiketsiz) kฤฑsmฤฑnฤฑ dรถndรผrรผr.
|
| 22 |
+
"""
|
| 23 |
+
# intent etiketi รถncesini al
|
| 24 |
+
pos_intent = raw.find("#DETECTED_INTENT")
|
| 25 |
+
if pos_intent != -1:
|
| 26 |
+
raw = raw[:pos_intent]
|
| 27 |
+
|
| 28 |
+
# arka arkaya 'assistant' bloklarฤฑ varsa ilkini al
|
| 29 |
+
pos_asst = raw.lower().find("assistant")
|
| 30 |
+
if pos_asst != -1:
|
| 31 |
+
raw = raw[:pos_asst]
|
| 32 |
+
|
| 33 |
+
return raw.strip()
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 37 |
+
# CONFIG LOAD
|
| 38 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 39 |
+
def load_config(path: str = "service_config.jsonc") -> dict:
|
| 40 |
try:
|
| 41 |
with open(path, encoding="utf-8") as f:
|
| 42 |
cfg = commentjson.load(f)
|
|
|
|
| 52 |
SPARK_URL = CFG["config"]["spark_endpoint"].rstrip("/") + "/generate"
|
| 53 |
ALLOWED_INTENTS = {"flight-booking", "flight-info", "booking-cancel"}
|
| 54 |
|
| 55 |
+
|
| 56 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 57 |
+
# SESSION
|
| 58 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 59 |
class Session:
|
| 60 |
def __init__(self, project_name: str):
|
| 61 |
self.id = str(uuid.uuid4())
|
|
|
|
| 67 |
|
| 68 |
SESSIONS: Dict[str, Session] = {}
|
| 69 |
|
| 70 |
+
|
| 71 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 72 |
+
# SPARK CLIENT
|
| 73 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 74 |
async def spark_generate(session: Session,
|
| 75 |
system_prompt: str,
|
| 76 |
user_input: str) -> str:
|
|
|
|
| 83 |
async with httpx.AsyncClient(timeout=60) as c:
|
| 84 |
r = await c.post(SPARK_URL, json=payload)
|
| 85 |
r.raise_for_status()
|
| 86 |
+
d = r.json()
|
| 87 |
+
return (d.get("assistant") or d.get("model_answer") or d.get("text", "")).strip()
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
|
| 90 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 91 |
+
# FASTAPI ROUTER
|
| 92 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 93 |
router = APIRouter()
|
| 94 |
|
| 95 |
@router.get("/")
|
|
|
|
| 104 |
session_id: str
|
| 105 |
answer: str
|
| 106 |
|
| 107 |
+
|
| 108 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 109 |
+
# ENDPOINTS
|
| 110 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 111 |
@router.post("/start_session", response_model=ChatResponse)
|
| 112 |
async def start_session(req: StartSessionRequest):
|
| 113 |
if req.project_name not in PROJECTS:
|
| 114 |
raise HTTPException(404, "Unknown project")
|
| 115 |
s = Session(req.project_name)
|
| 116 |
SESSIONS[s.id] = s
|
| 117 |
+
return ChatResponse(session_id=s.id, answer="Hoล geldiniz! Size nasฤฑl yardฤฑmcฤฑ olabilirim?")
|
| 118 |
|
| 119 |
@router.post("/chat", response_model=ChatResponse)
|
| 120 |
async def chat(body: ChatBody, x_session_id: str = Header(...)):
|
|
|
|
| 125 |
user_msg = body.user_input.strip()
|
| 126 |
s.history.append({"role": "user", "content": user_msg})
|
| 127 |
|
| 128 |
+
# follow-up?
|
| 129 |
if s.awaiting:
|
| 130 |
answer = await _followup(s, user_msg)
|
| 131 |
s.history.append({"role": "assistant", "content": answer})
|
| 132 |
return ChatResponse(session_id=s.id, answer=answer)
|
| 133 |
|
| 134 |
+
# intent detect
|
| 135 |
+
gen_prompt = s.project["versions"][0]["general_prompt"]
|
| 136 |
+
intents_cfg = s.project["versions"][0]["intents"]
|
| 137 |
+
intent_raw = await spark_generate(
|
| 138 |
s,
|
| 139 |
build_intent_prompt(gen_prompt, s.history, user_msg, intents_cfg),
|
| 140 |
user_msg
|
| 141 |
)
|
| 142 |
|
| 143 |
+
# small-talk yolu
|
| 144 |
if not intent_raw.startswith("#DETECTED_INTENT:"):
|
| 145 |
+
clean = _trim_smalltalk(intent_raw)
|
| 146 |
+
# Selamlama resmรฎ deฤilse dรผzelt
|
| 147 |
+
if "Hoลgeldin" in clean or "Hoล geldin" in clean:
|
| 148 |
+
clean = clean.replace("Hoลgeldin", "Hoล geldiniz").replace("Hoล geldin", "Hoล geldiniz")
|
| 149 |
s.history.append({"role": "assistant", "content": clean})
|
| 150 |
return ChatResponse(session_id=s.id, answer=clean)
|
| 151 |
|
| 152 |
intent_name = intent_raw.split(":", 1)[1].strip()
|
| 153 |
|
| 154 |
+
# kฤฑsa mesaj guard
|
| 155 |
if len(user_msg.split()) < 3:
|
| 156 |
+
clean = _trim_smalltalk(intent_raw)
|
| 157 |
s.history.append({"role": "assistant", "content": clean})
|
| 158 |
return ChatResponse(session_id=s.id, answer=clean)
|
| 159 |
|
| 160 |
+
# allowed-set
|
| 161 |
if intent_name not in ALLOWED_INTENTS:
|
| 162 |
+
clean = _trim_smalltalk(intent_raw)
|
| 163 |
s.history.append({"role": "assistant", "content": clean})
|
| 164 |
return ChatResponse(session_id=s.id, answer=clean)
|
| 165 |
|
| 166 |
+
# intent handling ...
|
| 167 |
intent_cfg = _find_intent(s.project, intent_name)
|
| 168 |
if not intent_cfg:
|
| 169 |
err = "รzgรผnรผm, anlayamadฤฑm."
|