Update app.py
Browse files
app.py
CHANGED
|
@@ -43,59 +43,69 @@ def home() -> str:
|
|
| 43 |
|
| 44 |
@app.post("/chat")
|
| 45 |
async def chat(request: Request) -> JSONResponse:
|
| 46 |
-
raw_body: Any = None
|
| 47 |
try:
|
| 48 |
-
raw_body =
|
| 49 |
-
except Exception:
|
| 50 |
try:
|
| 51 |
-
raw_body =
|
| 52 |
except Exception:
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
"
|
| 91 |
-
"
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
},
|
| 97 |
-
|
| 98 |
-
|
| 99 |
|
| 100 |
|
| 101 |
@app.post("/log/session/start")
|
|
|
|
| 43 |
|
| 44 |
@app.post("/chat")
|
| 45 |
async def chat(request: Request) -> JSONResponse:
|
|
|
|
| 46 |
try:
|
| 47 |
+
raw_body: Any = None
|
|
|
|
| 48 |
try:
|
| 49 |
+
raw_body = await request.json()
|
| 50 |
except Exception:
|
| 51 |
+
try:
|
| 52 |
+
raw_body = (await request.body()).decode("utf-8", errors="ignore")
|
| 53 |
+
except Exception:
|
| 54 |
+
raw_body = None
|
| 55 |
+
|
| 56 |
+
req_data: Dict[str, Any] = raw_body if isinstance(raw_body, dict) else {}
|
| 57 |
+
req = ChatRequest(**req_data) if isinstance(req_data, dict) else ChatRequest()
|
| 58 |
+
|
| 59 |
+
full_text = get_user_text(req, raw_body)
|
| 60 |
+
hidden_context, actual_user_message = split_unity_message(full_text)
|
| 61 |
+
game_fields = extract_game_context_fields(hidden_context)
|
| 62 |
+
|
| 63 |
+
question_text = (getattr(req, "question_text", None) or "").strip() or game_fields["question"]
|
| 64 |
+
options_text = game_fields["options"]
|
| 65 |
+
|
| 66 |
+
tone = clamp01(req_data.get("tone", getattr(req, "tone", 0.5)), 0.5)
|
| 67 |
+
verbosity = clamp01(req_data.get("verbosity", getattr(req, "verbosity", 0.5)), 0.5)
|
| 68 |
+
transparency = clamp01(req_data.get("transparency", getattr(req, "transparency", 0.5)), 0.5)
|
| 69 |
+
|
| 70 |
+
incoming_help_mode = req_data.get("help_mode", getattr(req, "help_mode", None))
|
| 71 |
+
intent = detect_intent(actual_user_message or full_text, incoming_help_mode)
|
| 72 |
+
help_mode = intent_to_help_mode(intent)
|
| 73 |
+
|
| 74 |
+
result = engine.generate_response(
|
| 75 |
+
raw_user_text=actual_user_message or full_text,
|
| 76 |
+
tone=tone,
|
| 77 |
+
verbosity=verbosity,
|
| 78 |
+
transparency=transparency,
|
| 79 |
+
intent=intent,
|
| 80 |
+
help_mode=help_mode,
|
| 81 |
+
chat_history=getattr(req, "chat_history", None) or getattr(req, "history", None) or [],
|
| 82 |
+
question_text=question_text,
|
| 83 |
+
options_text=options_text,
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
return JSONResponse(
|
| 87 |
+
{
|
| 88 |
+
"reply": result.reply,
|
| 89 |
+
"meta": {
|
| 90 |
+
"domain": result.domain,
|
| 91 |
+
"solved": result.solved,
|
| 92 |
+
"help_mode": result.help_mode,
|
| 93 |
+
"answer_letter": result.answer_letter,
|
| 94 |
+
"answer_value": result.answer_value,
|
| 95 |
+
"topic": result.topic,
|
| 96 |
+
"used_retrieval": result.used_retrieval,
|
| 97 |
+
"used_generator": result.used_generator,
|
| 98 |
+
},
|
| 99 |
+
}
|
| 100 |
+
)
|
| 101 |
+
except Exception as e:
|
| 102 |
+
return JSONResponse(
|
| 103 |
+
{
|
| 104 |
+
"error": type(e).__name__,
|
| 105 |
+
"detail": str(e),
|
| 106 |
},
|
| 107 |
+
status_code=500,
|
| 108 |
+
)
|
| 109 |
|
| 110 |
|
| 111 |
@app.post("/log/session/start")
|