Update app.py
Browse files
app.py
CHANGED
|
@@ -35,24 +35,36 @@ async def send_chat_message(LLM_URL, LLM_API, category, file_id):
|
|
| 35 |
}
|
| 36 |
]
|
| 37 |
}
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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):
|