Update app.py
Browse files
app.py
CHANGED
@@ -63,7 +63,10 @@ async def send_chat_message(LLM_URL, LLM_API, user_input):
|
|
63 |
"conversation_id": "",
|
64 |
"user": USER_ID,
|
65 |
}
|
66 |
-
print("Sending chat message payload:", payload)
|
|
|
|
|
|
|
67 |
|
68 |
async with aiohttp.ClientSession() as session:
|
69 |
try:
|
@@ -74,29 +77,47 @@ async def send_chat_message(LLM_URL, LLM_API, user_input):
|
|
74 |
timeout=aiohttp.ClientTimeout(total=60)
|
75 |
) as response:
|
76 |
if response.status != 200:
|
77 |
-
|
|
|
78 |
return f"Error: {response.status}"
|
79 |
|
80 |
-
|
81 |
-
async for
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
if full_response:
|
97 |
return ''.join(full_response).strip()
|
98 |
else:
|
99 |
-
|
|
|
|
|
100 |
except Exception as e:
|
101 |
print(f"Exception: {e}")
|
102 |
return f"Exception: {e}"
|
|
|
63 |
"conversation_id": "",
|
64 |
"user": USER_ID,
|
65 |
}
|
66 |
+
print("Sending chat message payload:", payload)
|
67 |
+
|
68 |
+
full_response = []
|
69 |
+
buffer = b"" # 使用字節緩衝區來處理不完整的行
|
70 |
|
71 |
async with aiohttp.ClientSession() as session:
|
72 |
try:
|
|
|
77 |
timeout=aiohttp.ClientTimeout(total=60)
|
78 |
) as response:
|
79 |
if response.status != 200:
|
80 |
+
error_text = await response.text()
|
81 |
+
print(f"Error: {response.status}, Body: {error_text}")
|
82 |
return f"Error: {response.status}"
|
83 |
|
84 |
+
# 手動迭代數據塊 (chunk)
|
85 |
+
async for chunk in response.content.iter_any():
|
86 |
+
buffer += chunk
|
87 |
+
# 嘗試按行分割緩衝區中的數據
|
88 |
+
while b'\n' in buffer:
|
89 |
+
line, buffer = buffer.split(b'\n', 1)
|
90 |
+
line_str = line.decode('utf-8').strip()
|
91 |
+
|
92 |
+
if not line_str or "data: " not in line_str:
|
93 |
+
continue
|
94 |
+
|
95 |
+
try:
|
96 |
+
# 檢查是否是我們要的答案,而不是工作流日誌
|
97 |
+
# 這些日誌訊息 (e.g., event: "node_started") 就是造成問題的元兇
|
98 |
+
# 我們只關心包含 "answer" 的 message_chunk
|
99 |
+
if '"event": "message_chunk"' in line_str or '"answer"' in line_str:
|
100 |
+
print("Received line:", line_str) # 只打印有用的行
|
101 |
+
data_part = line_str.split("data: ", 1)[1]
|
102 |
+
data = json.loads(data_part)
|
103 |
+
|
104 |
+
if "answer" in data:
|
105 |
+
full_response.append(data["answer"])
|
106 |
+
# 如果是 Dify API,答案可能在 data['data']['answer']
|
107 |
+
elif "data" in data and "answer" in data["data"]:
|
108 |
+
full_response.append(data["data"]["answer"])
|
109 |
+
|
110 |
+
except (IndexError, json.JSONDecodeError) as e:
|
111 |
+
# 忽略無法解析的行,它們很可能是日誌
|
112 |
+
# print(f"Skipping unparsable line: {line_str}, error: {e}")
|
113 |
+
pass
|
114 |
|
115 |
if full_response:
|
116 |
return ''.join(full_response).strip()
|
117 |
else:
|
118 |
+
# 如果循環結束都沒有收到答案,返回提示
|
119 |
+
return "抱歉,我無法從 API 取得有效的回覆。"
|
120 |
+
|
121 |
except Exception as e:
|
122 |
print(f"Exception: {e}")
|
123 |
return f"Exception: {e}"
|