Update app.py
Browse files
app.py
CHANGED
|
@@ -583,6 +583,7 @@ def handsome_chat_completions():
|
|
| 583 |
|
| 584 |
if model_name in image_models:
|
| 585 |
# Handle image generation
|
|
|
|
| 586 |
siliconflow_data = {
|
| 587 |
"model": model_name,
|
| 588 |
"prompt": data.get("messages", [{}])[0].get("content") if isinstance(data.get("messages"), list) else "",
|
|
@@ -643,30 +644,22 @@ def handsome_chat_completions():
|
|
| 643 |
image_url = images[0]
|
| 644 |
logging.info(f"Extracted image URL: {image_url}")
|
| 645 |
|
| 646 |
-
#
|
| 647 |
-
|
| 648 |
-
# 对于图像模型,usage 可以设置为固定的值, 因为实际的token消耗难以计算
|
| 649 |
response_data = {
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
"
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
],
|
| 665 |
-
"usage": {
|
| 666 |
-
"completion_tokens": 0,
|
| 667 |
-
"prompt_tokens": 0,
|
| 668 |
-
"total_tokens": 0,
|
| 669 |
-
},
|
| 670 |
}
|
| 671 |
|
| 672 |
except (KeyError, ValueError, IndexError) as e:
|
|
@@ -675,26 +668,20 @@ def handsome_chat_completions():
|
|
| 675 |
f"完整内容: {response_json}"
|
| 676 |
)
|
| 677 |
response_data = {
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
|
| 686 |
-
"
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
],
|
| 693 |
-
"usage": {
|
| 694 |
-
"completion_tokens": 0,
|
| 695 |
-
"prompt_tokens": 0,
|
| 696 |
-
"total_tokens": 0,
|
| 697 |
-
},
|
| 698 |
}
|
| 699 |
|
| 700 |
logging.info(
|
|
@@ -833,36 +820,10 @@ def handsome_chat_completions():
|
|
| 833 |
request_timestamps.append(time.time())
|
| 834 |
token_counts.append(prompt_tokens+completion_tokens)
|
| 835 |
|
| 836 |
-
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
|
| 840 |
-
"object": "chat.completion",
|
| 841 |
-
"created": int(time.time()),
|
| 842 |
-
"model": model_name,
|
| 843 |
-
"system_fingerprint": system_fingerprint,
|
| 844 |
-
"choices": [
|
| 845 |
-
{
|
| 846 |
-
"index": 0,
|
| 847 |
-
"message": {
|
| 848 |
-
"role": "assistant",
|
| 849 |
-
"content": response_content,
|
| 850 |
-
},
|
| 851 |
-
"finish_reason": "stop",
|
| 852 |
-
}
|
| 853 |
-
],
|
| 854 |
-
"usage": {
|
| 855 |
-
"completion_tokens": completion_tokens,
|
| 856 |
-
"prompt_tokens": prompt_tokens,
|
| 857 |
-
"total_tokens": prompt_tokens + completion_tokens,
|
| 858 |
-
},
|
| 859 |
-
}
|
| 860 |
-
|
| 861 |
-
return Response(
|
| 862 |
-
json.dumps(response_data),
|
| 863 |
-
content_type="application/json"
|
| 864 |
-
)
|
| 865 |
-
return generate()
|
| 866 |
else:
|
| 867 |
response.raise_for_status()
|
| 868 |
end_time = time.time()
|
|
@@ -885,30 +846,6 @@ def handsome_chat_completions():
|
|
| 885 |
prompt_tokens = 0
|
| 886 |
completion_tokens = 0
|
| 887 |
response_content = ""
|
| 888 |
-
# 构建符合 OpenAI 的响应格式,包括 system_fingerprint 和 usage
|
| 889 |
-
response_data = {
|
| 890 |
-
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 891 |
-
"object": "chat.completion",
|
| 892 |
-
"created": int(time.time()),
|
| 893 |
-
"model": model_name,
|
| 894 |
-
"system_fingerprint": system_fingerprint,
|
| 895 |
-
"choices": [
|
| 896 |
-
{
|
| 897 |
-
"index": 0,
|
| 898 |
-
"message": {
|
| 899 |
-
"role": "assistant",
|
| 900 |
-
"content": response_content,
|
| 901 |
-
},
|
| 902 |
-
"finish_reason": "stop",
|
| 903 |
-
}
|
| 904 |
-
],
|
| 905 |
-
"usage": {
|
| 906 |
-
"completion_tokens": completion_tokens,
|
| 907 |
-
"prompt_tokens": prompt_tokens,
|
| 908 |
-
"total_tokens": prompt_tokens + completion_tokens,
|
| 909 |
-
},
|
| 910 |
-
}
|
| 911 |
-
|
| 912 |
|
| 913 |
user_content = ""
|
| 914 |
messages = data.get("messages", [])
|
|
@@ -953,7 +890,7 @@ def handsome_chat_completions():
|
|
| 953 |
else:
|
| 954 |
token_counts.append(0)
|
| 955 |
|
| 956 |
-
return jsonify(
|
| 957 |
|
| 958 |
except requests.exceptions.RequestException as e:
|
| 959 |
logging.error(f"请求转发异常: {e}")
|
|
|
|
| 583 |
|
| 584 |
if model_name in image_models:
|
| 585 |
# Handle image generation
|
| 586 |
+
# Map OpenAI-style parameters to SiliconFlow's parameters
|
| 587 |
siliconflow_data = {
|
| 588 |
"model": model_name,
|
| 589 |
"prompt": data.get("messages", [{}])[0].get("content") if isinstance(data.get("messages"), list) else "",
|
|
|
|
| 644 |
image_url = images[0]
|
| 645 |
logging.info(f"Extracted image URL: {image_url}")
|
| 646 |
|
| 647 |
+
# Construct the expected JSON output - Mimicking OpenAI
|
|
|
|
|
|
|
| 648 |
response_data = {
|
| 649 |
+
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 650 |
+
"object": "chat.completion",
|
| 651 |
+
"created": int(time.time()),
|
| 652 |
+
"model": model_name,
|
| 653 |
+
"choices": [
|
| 654 |
+
{
|
| 655 |
+
"index": 0,
|
| 656 |
+
"message": {
|
| 657 |
+
"role": "assistant",
|
| 658 |
+
"content": image_url if image_url else "Failed to generate image", # Directly return the URL in content
|
| 659 |
+
},
|
| 660 |
+
"finish_reason": "stop",
|
| 661 |
+
}
|
| 662 |
+
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 663 |
}
|
| 664 |
|
| 665 |
except (KeyError, ValueError, IndexError) as e:
|
|
|
|
| 668 |
f"完整内容: {response_json}"
|
| 669 |
)
|
| 670 |
response_data = {
|
| 671 |
+
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 672 |
+
"object": "chat.completion",
|
| 673 |
+
"created": int(time.time()),
|
| 674 |
+
"model": model_name,
|
| 675 |
+
"choices": [
|
| 676 |
+
{
|
| 677 |
+
"index": 0,
|
| 678 |
+
"message": {
|
| 679 |
+
"role": "assistant",
|
| 680 |
+
"content": "Failed to process image data",
|
| 681 |
+
},
|
| 682 |
+
"finish_reason": "stop",
|
| 683 |
+
}
|
| 684 |
+
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 685 |
}
|
| 686 |
|
| 687 |
logging.info(
|
|
|
|
| 820 |
request_timestamps.append(time.time())
|
| 821 |
token_counts.append(prompt_tokens+completion_tokens)
|
| 822 |
|
| 823 |
+
return Response(
|
| 824 |
+
stream_with_context(generate()),
|
| 825 |
+
content_type=response.headers['Content-Type']
|
| 826 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 827 |
else:
|
| 828 |
response.raise_for_status()
|
| 829 |
end_time = time.time()
|
|
|
|
| 846 |
prompt_tokens = 0
|
| 847 |
completion_tokens = 0
|
| 848 |
response_content = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 849 |
|
| 850 |
user_content = ""
|
| 851 |
messages = data.get("messages", [])
|
|
|
|
| 890 |
else:
|
| 891 |
token_counts.append(0)
|
| 892 |
|
| 893 |
+
return jsonify(response_json)
|
| 894 |
|
| 895 |
except requests.exceptions.RequestException as e:
|
| 896 |
logging.error(f"请求转发异常: {e}")
|