DeepLearning101 commited on
Commit
0231211
·
verified ·
1 Parent(s): 18fe40d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -6
app.py CHANGED
@@ -26,7 +26,7 @@ async def send_chat_message(LLM_URL, LLM_API, category, file_url):
26
  "query": category,
27
  "conversation_id": "",
28
  "user": USER_ID,
29
- "response_mode": "streaming", # ⬅️ 一定要加
30
  "files": [
31
  {
32
  "type": "image",
@@ -36,6 +36,7 @@ async def send_chat_message(LLM_URL, LLM_API, category, file_url):
36
  ]
37
  }
38
 
 
39
  async with aiohttp.ClientSession() as session:
40
  async with session.post(
41
  f"{LLM_URL}/chat-messages",
@@ -45,11 +46,16 @@ async def send_chat_message(LLM_URL, LLM_API, category, file_url):
45
  },
46
  json=payload
47
  ) as response:
48
- if response.status != 200:
49
- error_text = await response.text()
50
- return f"Error: Server returned status {response.status} - {error_text}"
51
- data = await response.json()
52
- return data.get("answer") or data.get("thought") or "No answer returned"
 
 
 
 
 
53
 
54
  # async def send_chat_message(LLM_URL, LLM_API, category, file_id):
55
  # payload = {
 
26
  "query": category,
27
  "conversation_id": "",
28
  "user": USER_ID,
29
+ "response_mode": "streaming",
30
  "files": [
31
  {
32
  "type": "image",
 
36
  ]
37
  }
38
 
39
+ answer = ""
40
  async with aiohttp.ClientSession() as session:
41
  async with session.post(
42
  f"{LLM_URL}/chat-messages",
 
46
  },
47
  json=payload
48
  ) as response:
49
+ async for line_bytes in response.content:
50
+ line = line_bytes.decode("utf-8").strip()
51
+ if line.startswith("data: "):
52
+ try:
53
+ data = json.loads(line[6:])
54
+ if "answer" in data:
55
+ answer += data["answer"]
56
+ except:
57
+ continue
58
+ return answer or "No answer returned"
59
 
60
  # async def send_chat_message(LLM_URL, LLM_API, category, file_id):
61
  # payload = {