smgc commited on
Commit
8c30e80
1 Parent(s): 69075e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -23
app.py CHANGED
@@ -158,16 +158,11 @@ def messages():
158
 
159
  def on_query_progress(data):
160
  nonlocal response_text
161
- try:
162
- if 'text' in data:
163
- text = json.loads(data['text']) # 尝试解析为JSON
164
- chunk = text['chunks'][-1] if text['chunks'] else None
165
- if chunk:
166
- response_text.append(chunk)
167
- except json.JSONDecodeError:
168
- # 如果解析失败,说明不是有效的JSON,忽略或记录错误
169
- logging.error("Failed to parse response as JSON")
170
- response_text.append("Invalid response format from API.")
171
 
172
  # 检查是否是最终响应
173
  if data.get('final', False):
@@ -232,7 +227,7 @@ def messages():
232
 
233
  def handle_non_stream(previous_messages, msg_id, model):
234
  """
235
- 处理 stream 为 false 的情况,返回完整的响应。
236
  """
237
  try:
238
  response_event = Event()
@@ -258,16 +253,11 @@ def handle_non_stream(previous_messages, msg_id, model):
258
 
259
  def on_query_progress(data):
260
  nonlocal response_text
261
- try:
262
- if 'text' in data:
263
- text = json.loads(data['text']) # 尝试解析为JSON
264
- chunk = text['chunks'][-1] if text['chunks'] else None
265
- if chunk:
266
- response_text.append(chunk)
267
- except json.JSONDecodeError:
268
- # 如果解析失败,说明不是有效的JSON,忽略或记录错误
269
- logging.error("Failed to parse response as JSON")
270
- response_text.append("Invalid response format from API.")
271
 
272
  # 检查是否是最终响应
273
  if data.get('final', False):
@@ -292,13 +282,15 @@ def handle_non_stream(previous_messages, msg_id, model):
292
  # 等待响应完成
293
  response_event.wait(timeout=30)
294
 
295
- # 生成完整的响应
296
  full_response = {
297
  "id": msg_id,
298
- "content": [{"text": ''.join(response_text)}], # 合并所有文本块
299
  "model": model, # 动态模型
 
300
  "stop_reason": "end_turn",
301
  "stop_sequence": None,
 
302
  "usage": {
303
  "input_tokens": 8,
304
  "output_tokens": len(''.join(response_text)),
 
158
 
159
  def on_query_progress(data):
160
  nonlocal response_text
161
+ if 'text' in data:
162
+ text = json.loads(data['text'])
163
+ chunk = text['chunks'][-1] if text['chunks'] else None
164
+ if chunk:
165
+ response_text.append(chunk)
 
 
 
 
 
166
 
167
  # 检查是否是最终响应
168
  if data.get('final', False):
 
227
 
228
  def handle_non_stream(previous_messages, msg_id, model):
229
  """
230
+ 处理 stream 为 false 的情况,返回符合 Claude API 格式的完整响应。
231
  """
232
  try:
233
  response_event = Event()
 
253
 
254
  def on_query_progress(data):
255
  nonlocal response_text
256
+ if 'text' in data:
257
+ text = json.loads(data['text'])
258
+ chunk = text['chunks'][-1] if text['chunks'] else None
259
+ if chunk:
260
+ response_text.append(chunk)
 
 
 
 
 
261
 
262
  # 检查是否是最终响应
263
  if data.get('final', False):
 
282
  # 等待响应完成
283
  response_event.wait(timeout=30)
284
 
285
+ # 生成符合 Claude API 格式的完整响应
286
  full_response = {
287
  "id": msg_id,
288
+ "content": [{"text": ''.join(response_text), "type": "text"}], # 合并所有文本块,并设置类型
289
  "model": model, # 动态模型
290
+ "role": "assistant", # Claude 的角色
291
  "stop_reason": "end_turn",
292
  "stop_sequence": None,
293
+ "type": "message",
294
  "usage": {
295
  "input_tokens": 8,
296
  "output_tokens": len(''.join(response_text)),