j-js commited on
Commit
eae4443
·
verified ·
1 Parent(s): 626316f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -49
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 = await request.json()
49
- except Exception:
50
  try:
51
- raw_body = (await request.body()).decode("utf-8", errors="ignore")
52
  except Exception:
53
- raw_body = None
54
-
55
- req_data: Dict[str, Any] = raw_body if isinstance(raw_body, dict) else {}
56
- req = ChatRequest(**req_data) if isinstance(req_data, dict) else ChatRequest()
57
-
58
- full_text = get_user_text(req, raw_body)
59
- hidden_context, actual_user_message = split_unity_message(full_text)
60
- game_fields = extract_game_context_fields(hidden_context)
61
-
62
- question_text = (req.question_text or "").strip() or game_fields["question"]
63
- options_text = game_fields["options"]
64
-
65
- tone = clamp01(req_data.get("tone", req.tone), 0.5)
66
- verbosity = clamp01(req_data.get("verbosity", req.verbosity), 0.5)
67
- transparency = clamp01(req_data.get("transparency", req.transparency), 0.5)
68
-
69
- intent = detect_intent(actual_user_message or full_text, req_data.get("help_mode", req.help_mode))
70
- help_mode = intent_to_help_mode(intent)
71
-
72
- result = engine.generate_response(
73
- raw_user_text=actual_user_message or full_text,
74
- tone=tone,
75
- verbosity=verbosity,
76
- transparency=transparency,
77
- intent=intent,
78
- help_mode=help_mode,
79
- chat_history=req.chat_history or req.history or [],
80
- question_text=question_text,
81
- options_text=options_text,
82
- )
83
-
84
- return JSONResponse(
85
- {
86
- "reply": result.reply,
87
- "meta": {
88
- "domain": result.domain,
89
- "solved": result.solved,
90
- "help_mode": result.help_mode,
91
- "answer_letter": result.answer_letter,
92
- "answer_value": result.answer_value,
93
- "topic": result.topic,
94
- "used_retrieval": result.used_retrieval,
95
- "used_generator": result.used_generator,
 
 
 
 
 
 
 
 
 
 
 
 
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")