dan92 commited on
Commit
64db2a9
·
verified ·
1 Parent(s): 5e3aa0d

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +33 -25
main.py CHANGED
@@ -221,13 +221,17 @@ async def chat_completions(request: Request, background_tasks: BackgroundTasks,
221
  print(f"跳过非字典类型的消息: {message}")
222
  continue
223
 
224
- content = message.get("content", "")
225
  role = message.get("role")
226
 
227
  if not role:
228
  print(f"跳过没有role的消息: {message}")
229
  continue
230
 
 
 
 
 
231
  if isinstance(content, list):
232
  text_parts = []
233
  images = []
@@ -236,9 +240,14 @@ async def chat_completions(request: Request, background_tasks: BackgroundTasks,
236
  print(f"跳过非字典类型的内容项: {item}")
237
  continue
238
 
239
- if "text" in item and isinstance(item["text"], str):
 
 
 
 
 
240
  text_parts.append(item["text"])
241
- elif "image_url" in item and isinstance(item["image_url"], dict):
242
  has_image = True
243
  image_info = item["image_url"]
244
  url = image_info.get("url", "")
@@ -258,30 +267,29 @@ async def chat_completions(request: Request, background_tasks: BackgroundTasks,
258
  continue
259
  else:
260
  images.append({"data": url})
 
 
 
261
 
262
  # 合并文本部分
263
  extracted_content = " ".join(text_parts).strip()
 
 
264
  if extracted_content:
265
  has_text = True
266
- new_message = {
267
- "role": role,
268
- "content": extracted_content
269
- }
270
- if images:
271
- new_message["images"] = images
272
- cleaned_messages.append(new_message)
273
- print("Extracted:", extracted_content)
274
- elif images:
275
- has_image = True
276
- new_message = {
277
- "role": role,
278
- "content": "",
279
- "images": images
280
- }
281
  cleaned_messages.append(new_message)
282
- print("Extracted image only.")
283
  else:
284
- print("Deleted message with empty content.")
 
285
  elif isinstance(content, str):
286
  content_str = content.strip()
287
  if content_str:
@@ -290,11 +298,11 @@ async def chat_completions(request: Request, background_tasks: BackgroundTasks,
290
  "role": role,
291
  "content": content_str
292
  })
293
- print("Retained content:", content_str)
294
  else:
295
- print("Deleted message with empty content.")
296
  else:
297
- print(f"Deleted non-expected type of content message: {type(content)}")
298
 
299
  if not cleaned_messages:
300
  raise HTTPException(status_code=400, detail="所有消息的内容均为空。")
@@ -336,7 +344,7 @@ async def chat_completions(request: Request, background_tasks: BackgroundTasks,
336
  }
337
 
338
  if is_stream:
339
- # 流式响��处理
340
  async def event_generator():
341
  async with httpx.AsyncClient(timeout=None) as client_stream:
342
  try:
@@ -374,7 +382,7 @@ async def chat_completions(request: Request, background_tasks: BackgroundTasks,
374
  new_sse_line = f"data: {json.dumps(new_sse_json, ensure_ascii=False)}\n\n"
375
  yield new_sse_line
376
  except json.JSONDecodeError:
377
- print("JSON解析错误")
378
  continue
379
  except httpx.RequestError as exc:
380
  print(f"外部API请求失败: {exc}")
 
221
  print(f"跳过非字典类型的消息: {message}")
222
  continue
223
 
224
+ content = message.get("content")
225
  role = message.get("role")
226
 
227
  if not role:
228
  print(f"跳过没有role的消息: {message}")
229
  continue
230
 
231
+ if not content:
232
+ print(f"跳过空内容的消息: {message}")
233
+ continue
234
+
235
  if isinstance(content, list):
236
  text_parts = []
237
  images = []
 
240
  print(f"跳过非字典类型的内容项: {item}")
241
  continue
242
 
243
+ item_type = item.get("type")
244
+ if not item_type:
245
+ print(f"跳过没有type的内容项: {item}")
246
+ continue
247
+
248
+ if item_type == "text" and isinstance(item.get("text"), str):
249
  text_parts.append(item["text"])
250
+ elif item_type == "image_url" and isinstance(item.get("image_url"), dict):
251
  has_image = True
252
  image_info = item["image_url"]
253
  url = image_info.get("url", "")
 
267
  continue
268
  else:
269
  images.append({"data": url})
270
+ else:
271
+ print(f"跳过未知类型的内容项: {item}")
272
+ continue
273
 
274
  # 合并文本部分
275
  extracted_content = " ".join(text_parts).strip()
276
+ new_message = {"role": role}
277
+
278
  if extracted_content:
279
  has_text = True
280
+ new_message["content"] = extracted_content
281
+ else:
282
+ new_message["content"] = "" # 确保content字段存在
283
+
284
+ if images:
285
+ new_message["images"] = images
286
+
287
+ if extracted_content or images: # 只有当有文本或图片时才添加消息
 
 
 
 
 
 
 
288
  cleaned_messages.append(new_message)
289
+ print(f"添加了新消息: {new_message}")
290
  else:
291
+ print("跳过没有文本和图片的消息")
292
+
293
  elif isinstance(content, str):
294
  content_str = content.strip()
295
  if content_str:
 
298
  "role": role,
299
  "content": content_str
300
  })
301
+ print("保留了文本内容:", content_str)
302
  else:
303
+ print("删除了空内容的消息")
304
  else:
305
+ print(f"删除了非预期类型的内容消息: {type(content)}")
306
 
307
  if not cleaned_messages:
308
  raise HTTPException(status_code=400, detail="所有消息的内容均为空。")
 
344
  }
345
 
346
  if is_stream:
347
+ # 流式响应处理
348
  async def event_generator():
349
  async with httpx.AsyncClient(timeout=None) as client_stream:
350
  try:
 
382
  new_sse_line = f"data: {json.dumps(new_sse_json, ensure_ascii=False)}\n\n"
383
  yield new_sse_line
384
  except json.JSONDecodeError:
385
+ print("JSON解析错��")
386
  continue
387
  except httpx.RequestError as exc:
388
  print(f"外部API请求失败: {exc}")