Update app.py
Browse files
app.py
CHANGED
|
@@ -680,6 +680,18 @@ def handsome_chat_completions():
|
|
| 680 |
stream_with_context(generate()),
|
| 681 |
content_type='text/event-stream'
|
| 682 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 683 |
except (KeyError, ValueError, IndexError) as e:
|
| 684 |
logging.error(
|
| 685 |
f"解析响应 JSON 失败: {e}, "
|
|
@@ -701,17 +713,36 @@ def handsome_chat_completions():
|
|
| 701 |
}
|
| 702 |
],
|
| 703 |
}
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
)
|
| 709 |
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
|
|
|
|
|
|
| 713 |
|
| 714 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 715 |
except requests.exceptions.RequestException as e:
|
| 716 |
logging.error(f"请求转发异常: {e}")
|
| 717 |
return jsonify({"error": str(e)}), 500
|
|
|
|
| 680 |
stream_with_context(generate()),
|
| 681 |
content_type='text/event-stream'
|
| 682 |
)
|
| 683 |
+
else:
|
| 684 |
+
logging.info(
|
| 685 |
+
f"使用的key: {api_key}, "
|
| 686 |
+
f"总共用时: {total_time:.4f}秒, "
|
| 687 |
+
f"使用的模型: {model_name}"
|
| 688 |
+
)
|
| 689 |
+
|
| 690 |
+
with data_lock:
|
| 691 |
+
request_timestamps.append(time.time())
|
| 692 |
+
token_counts.append(0) # Image generation doesn't use tokens
|
| 693 |
+
|
| 694 |
+
return jsonify(response_data)
|
| 695 |
except (KeyError, ValueError, IndexError) as e:
|
| 696 |
logging.error(
|
| 697 |
f"解析响应 JSON 失败: {e}, "
|
|
|
|
| 713 |
}
|
| 714 |
],
|
| 715 |
}
|
| 716 |
+
if data.get("stream", False):
|
| 717 |
+
def generate():
|
| 718 |
+
yield f"data: {json.dumps(response_data)}\n\n"
|
| 719 |
+
yield "data: [DONE]\n\n"
|
|
|
|
| 720 |
|
| 721 |
+
logging.info(
|
| 722 |
+
f"使用的key: {api_key}, "
|
| 723 |
+
f"总共用时: {total_time:.4f}秒, "
|
| 724 |
+
f"使用的模型: {model_name}"
|
| 725 |
+
)
|
| 726 |
|
| 727 |
+
with data_lock:
|
| 728 |
+
request_timestamps.append(time.time())
|
| 729 |
+
token_counts.append(0) # Image generation doesn't use tokens
|
| 730 |
+
return Response(
|
| 731 |
+
stream_with_context(generate()),
|
| 732 |
+
content_type='text/event-stream'
|
| 733 |
+
)
|
| 734 |
+
else:
|
| 735 |
+
logging.info(
|
| 736 |
+
f"使用的key: {api_key}, "
|
| 737 |
+
f"总共用时: {total_time:.4f}秒, "
|
| 738 |
+
f"使用的模型: {model_name}"
|
| 739 |
+
)
|
| 740 |
+
|
| 741 |
+
with data_lock:
|
| 742 |
+
request_timestamps.append(time.time())
|
| 743 |
+
token_counts.append(0) # Image generation doesn't use tokens
|
| 744 |
+
|
| 745 |
+
return jsonify(response_data)
|
| 746 |
except requests.exceptions.RequestException as e:
|
| 747 |
logging.error(f"请求转发异常: {e}")
|
| 748 |
return jsonify({"error": str(e)}), 500
|