DeepLearning101 commited on
Commit
5a9d2f3
·
verified ·
1 Parent(s): 10cc800

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -20
app.py CHANGED
@@ -24,7 +24,7 @@ async def send_chat_message(LLM_URL, LLM_API, category, file_id):
24
  payload = {
25
  "inputs": {},
26
  "query": category,
27
- "response_mode": "streaming",
28
  "conversation_id": "",
29
  "user": USER_ID,
30
  "files": [
@@ -45,26 +45,17 @@ async def send_chat_message(LLM_URL, LLM_API, category, file_id):
45
  ) as response:
46
 
47
  if response.status != 200:
48
- err = await response.text()
49
- return f"Error: Server returned status {response.status}: {err}"
50
-
51
- last_thought = None
52
- async for line in response.content:
53
- if line:
54
- try:
55
- if b"data: " in line:
56
- json_part = line.split(b"data: ")[1].strip()
57
- data = json.loads(json_part.decode("utf-8"))
58
- if data.get("event") == "agent_thought":
59
- last_thought = data.get("thought")
60
- except Exception as e:
61
- print(f"[stream error] {e} line={line}")
62
- continue
63
- return last_thought.strip() if last_thought else "Error: No thought found in response"
64
- except aiohttp.ClientPayloadError as e:
65
- return f"Error: Incomplete response stream ({str(e)})"
66
  except Exception as e:
67
- return f"Error: Unexpected exception - {str(e)}"
68
 
69
  async def upload_file(LLM_URL, LLM_API, file_path, user_id):
70
  if not os.path.exists(file_path):
 
24
  payload = {
25
  "inputs": {},
26
  "query": category,
27
+ "response_mode": "blocking", # ⬅️ 改這裡
28
  "conversation_id": "",
29
  "user": USER_ID,
30
  "files": [
 
45
  ) as response:
46
 
47
  if response.status != 200:
48
+ error_text = await response.text()
49
+ return f"Error: Server returned status {response.status} - {error_text}"
50
+
51
+ try:
52
+ data = await response.json()
53
+ return data.get("thought", "Error: No thought in response")
54
+ except Exception as e:
55
+ return f"Error: Failed to parse JSON - {e}"
56
+
 
 
 
 
 
 
 
 
 
57
  except Exception as e:
58
+ return f"Error: Unexpected exception - {e}"
59
 
60
  async def upload_file(LLM_URL, LLM_API, file_path, user_id):
61
  if not os.path.exists(file_path):