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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -18
app.py CHANGED
@@ -35,24 +35,36 @@ async def send_chat_message(LLM_URL, LLM_API, category, file_id):
35
  }
36
  ]
37
  }
38
- async with aiohttp.ClientSession() as session:
39
- async with session.post(
40
- f"{LLM_URL}/chat-messages",
41
- headers={"Authorization": f"Bearer {LLM_API}"},
42
- json=payload
43
- ) as response:
44
- if response.status == 404:
45
- return "Error: Endpoint not found (404)"
46
- last_thought = None
47
- async for line in response.content:
48
- if line:
49
- try:
50
- data = json.loads(line.split(b"data: ")[1].decode("utf-8"))
51
- if data.get("event") == "agent_thought":
52
- last_thought = data.get("thought")
53
- except (IndexError, json.JSONDecodeError):
54
- continue
55
- return last_thought.strip() if last_thought else "Error: No thought found in the response"
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  async def upload_file(LLM_URL, LLM_API, file_path, user_id):
58
  if not os.path.exists(file_path):
 
35
  }
36
  ]
37
  }
38
+
39
+ try:
40
+ async with aiohttp.ClientSession() as session:
41
+ async with session.post(
42
+ f"{LLM_URL}/chat-messages",
43
+ headers={"Authorization": f"Bearer {LLM_API}"},
44
+ json=payload
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):