yangtb24 commited on
Commit
44a7d85
·
verified ·
1 Parent(s): 186320d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -12
app.py CHANGED
@@ -215,6 +215,22 @@ def list_models():
215
  "owned_by": "openai",
216
  "root": "deepseek-reasoner",
217
  "parent": None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
  }
219
  ]
220
 
@@ -312,8 +328,14 @@ def handsome_chat_completions():
312
  return jsonify({"error": "Invalid request data"}), 400
313
 
314
  model_name = data['model']
 
 
 
 
 
 
315
 
316
- api_key = select_key(model_name)
317
 
318
  if not api_key:
319
  return jsonify(
@@ -326,11 +348,6 @@ def handsome_chat_completions():
326
  }
327
  ), 429
328
 
329
- if model_name == "deepseek-reasoner":
330
- for param in ["temperature", "top_p", "presence_penalty", "frequency_penalty", "logprobs", "top_logprobs"]:
331
- if param in data:
332
- del data[param]
333
-
334
  headers = {
335
  "Authorization": f"Bearer {api_key}",
336
  "Content-Type": "application/json"
@@ -343,7 +360,7 @@ def handsome_chat_completions():
343
  headers=headers,
344
  json=data,
345
  stream=data.get("stream", False),
346
- timeout=60
347
  )
348
 
349
  if response.status_code == 429:
@@ -364,8 +381,6 @@ def handsome_chat_completions():
364
  full_response_content += chunk.decode("utf-8")
365
 
366
  for line in chunk.decode("utf-8").splitlines():
367
- # print(line)
368
-
369
  if line.startswith("data:"):
370
  try:
371
  chunk_json = json.loads(line.lstrip("data: ").strip())
@@ -382,8 +397,6 @@ def handsome_chat_completions():
382
 
383
  if delta.get("content") is not None:
384
  if not first_reasoning_chunk:
385
- # yield f"data: {json.dumps({'choices': [{'delta': {'content': '\n```\n'}, 'index': 0}]})}\n\n"
386
- # yield f"data: {json.dumps({'choices': [{'delta': {'content': '\n\n---\n\n### 结果输出\n'}, 'index': 0}]})}\n\n"
387
  yield f"data: {json.dumps({'choices': [{'delta': {'content': '\n\n'}, 'index': 0}]})}\n\n"
388
  first_reasoning_chunk = True
389
  yield f"data: {json.dumps({'choices': [{'delta': {'content': delta["content"]}, 'index': 0}]})}\n\n"
@@ -493,7 +506,7 @@ def handsome_chat_completions():
493
  completion_tokens = response_json["usage"]["completion_tokens"]
494
  response_content = ""
495
 
496
- if model_name == "deepseek-reasoner" and "choices" in response_json and len(response_json["choices"]) > 0:
497
  choice = response_json["choices"][0]
498
  if "message" in choice:
499
  if "reasoning_content" in choice["message"]:
@@ -507,6 +520,19 @@ def handsome_chat_completions():
507
  elif "choices" in response_json and len(response_json["choices"]) > 0:
508
  response_content = response_json["choices"][0]["message"]["content"]
509
 
 
 
 
 
 
 
 
 
 
 
 
 
 
510
  except (KeyError, ValueError, IndexError) as e:
511
  logging.error(
512
  f"解析非流式响应 JSON 失败: {e}, "
 
215
  "owned_by": "openai",
216
  "root": "deepseek-reasoner",
217
  "parent": None
218
+ },
219
+ {
220
+ "id": "deepseek-reasoner-thinking",
221
+ "object": "model",
222
+ "created": 1678888889,
223
+ "owned_by": "openai",
224
+ "root": "deepseek-reasoner",
225
+ "parent": None
226
+ },
227
+ {
228
+ "id": "deepseek-reasoner-openwebui",
229
+ "object": "model",
230
+ "created": 1678888889,
231
+ "owned_by": "openai",
232
+ "root": "deepseek-reasoner",
233
+ "parent": None
234
  }
235
  ]
236
 
 
328
  return jsonify({"error": "Invalid request data"}), 400
329
 
330
  model_name = data['model']
331
+ if model_name == "deepseek-reasoner-thinking" or model_name == "deepseek-reasoner-openwebui":
332
+ model_realname = "deepseek-reasoner"
333
+ else:
334
+ model_realname = model_name
335
+
336
+ data['model'] = model_realname
337
 
338
+ api_key = select_key(model_realname)
339
 
340
  if not api_key:
341
  return jsonify(
 
348
  }
349
  ), 429
350
 
 
 
 
 
 
351
  headers = {
352
  "Authorization": f"Bearer {api_key}",
353
  "Content-Type": "application/json"
 
360
  headers=headers,
361
  json=data,
362
  stream=data.get("stream", False),
363
+ timeout=120
364
  )
365
 
366
  if response.status_code == 429:
 
381
  full_response_content += chunk.decode("utf-8")
382
 
383
  for line in chunk.decode("utf-8").splitlines():
 
 
384
  if line.startswith("data:"):
385
  try:
386
  chunk_json = json.loads(line.lstrip("data: ").strip())
 
397
 
398
  if delta.get("content") is not None:
399
  if not first_reasoning_chunk:
 
 
400
  yield f"data: {json.dumps({'choices': [{'delta': {'content': '\n\n'}, 'index': 0}]})}\n\n"
401
  first_reasoning_chunk = True
402
  yield f"data: {json.dumps({'choices': [{'delta': {'content': delta["content"]}, 'index': 0}]})}\n\n"
 
506
  completion_tokens = response_json["usage"]["completion_tokens"]
507
  response_content = ""
508
 
509
+ if model_name == "deepseek-reasoner-thingking" and "choices" in response_json and len(response_json["choices"]) > 0:
510
  choice = response_json["choices"][0]
511
  if "message" in choice:
512
  if "reasoning_content" in choice["message"]:
 
520
  elif "choices" in response_json and len(response_json["choices"]) > 0:
521
  response_content = response_json["choices"][0]["message"]["content"]
522
 
523
+ if model_name == "deepseek-reasoner-openwebui" and "choices" in response_json and len(response_json["choices"]) > 0:
524
+ choice = response_json["choices"][0]
525
+ if "message" in choice:
526
+ if "reasoning_content" in choice["message"]:
527
+ response_content += f"<think>\n"
528
+ reasoning_content = choice["message"]["reasoning_content"]
529
+ formatted_reasoning = f"\n<\think>\n"
530
+ response_content += formatted_reasoning
531
+ if "content" in choice["message"]:
532
+ response_content += choice["message"]["content"]
533
+ elif "choices" in response_json and len(response_json["choices"]) > 0:
534
+ response_content = response_json["choices"][0]["message"]["content"]
535
+
536
  except (KeyError, ValueError, IndexError) as e:
537
  logging.error(
538
  f"解析非流式响应 JSON 失败: {e}, "