DeepLearning101 commited on
Commit
68b68de
·
verified ·
1 Parent(s): cb016c3

Update app.py

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