mirror of
https://github.com/Yanyutin753/RefreshToV1Api.git
synced 2025-10-17 16:43:57 +00:00
[feat] 支持非流式响应与latest版本镜像标签
This commit is contained in:
@@ -8,6 +8,12 @@
|
||||
|
||||
# 更新日志
|
||||
|
||||
### 0.0.10
|
||||
|
||||
- 已支持非流式响应
|
||||
|
||||
- 更新latest版本镜像
|
||||
|
||||
### 0.0.9
|
||||
|
||||
- 修复在 ChatGPT-Next-Web 网页端修改请求接口后出现 `Failed to fetch` 报错的问题
|
||||
|
@@ -2,7 +2,7 @@ version: '3'
|
||||
|
||||
services:
|
||||
backend-to-api:
|
||||
image: wizerd/pandora-to-api:0.0.9
|
||||
image: wizerd/pandora-to-api:latest
|
||||
restart: always
|
||||
ports:
|
||||
- "50011:33333"
|
||||
@@ -16,7 +16,7 @@ services:
|
||||
|
||||
|
||||
uploader:
|
||||
image: wizerd/pandora-to-api:0.0.9
|
||||
image: wizerd/pandora-to-api:latest
|
||||
restart: always
|
||||
entrypoint: ["python3", "/app/upload.py"]
|
||||
volumes:
|
||||
|
52
main.py
52
main.py
@@ -26,8 +26,8 @@ BASE_URL = os.getenv('BASE_URL', '')
|
||||
PROXY_API_PREFIX = os.getenv('PROXY_API_PREFIX', '')
|
||||
UPLOAD_BASE_URL = os.getenv('UPLOAD_BASE_URL', '')
|
||||
|
||||
VERSION = '0.0.9'
|
||||
UPDATE_INFO = '修复在 ChatGPT-Next-Web 网页端修改请求接口后出现 `Failed to fetch` 报错的问题'
|
||||
VERSION = '0.0.10'
|
||||
UPDATE_INFO = '支持非流式响应'
|
||||
|
||||
with app.app_context():
|
||||
# 输出版本信息
|
||||
@@ -202,7 +202,7 @@ def send_text_prompt_and_get_response(messages, api_key, stream, model):
|
||||
"history_and_training_disabled": False,
|
||||
"conversation_mode":{"kind":"primary_assistant"},"force_paragen":False,"force_rate_limit":False
|
||||
}
|
||||
response = requests.post(url, headers=headers, json=payload, stream=stream)
|
||||
response = requests.post(url, headers=headers, json=payload, stream=True)
|
||||
# print(response)
|
||||
return response
|
||||
|
||||
@@ -338,11 +338,12 @@ def chat_completions():
|
||||
|
||||
upstream_response = send_text_prompt_and_get_response(messages, api_key, stream, model)
|
||||
|
||||
if not stream:
|
||||
return Response(upstream_response)
|
||||
else:
|
||||
# 在非流式响应的情况下,我们需要一个变量来累积所有的 new_text
|
||||
all_new_text = ""
|
||||
|
||||
# 处理流式响应
|
||||
def generate():
|
||||
nonlocal all_new_text # 引用外部变量
|
||||
chat_message_id = generate_unique_id("chatcmpl")
|
||||
# 当前时间戳
|
||||
timestamp = int(time.time())
|
||||
@@ -576,6 +577,8 @@ def chat_completions():
|
||||
print(f"[{datetime.now()}] 发送消息: {new_text}")
|
||||
tmp = 'data: ' + json.dumps(new_data, ensure_ascii=False) + '\n\n'
|
||||
# print(f"[{datetime.now()}] 发送数据: {tmp}")
|
||||
# 累积 new_text
|
||||
all_new_text += new_text
|
||||
yield 'data: ' + json.dumps(new_data, ensure_ascii=False) + '\n\n'
|
||||
except json.JSONDecodeError:
|
||||
# print("JSON 解析错误")
|
||||
@@ -601,6 +604,8 @@ def chat_completions():
|
||||
}
|
||||
tmp = 'data: ' + json.dumps(new_data) + '\n\n'
|
||||
# print(f"[{datetime.now()}] 发送数据: {tmp}")
|
||||
# 累积 new_text
|
||||
all_new_text += citation_buffer
|
||||
yield 'data: ' + json.dumps(new_data) + '\n\n'
|
||||
if buffer:
|
||||
# print(f"[{datetime.now()}] 最后的数据: {buffer}")
|
||||
@@ -625,6 +630,8 @@ def chat_completions():
|
||||
}
|
||||
tmp = 'data: ' + json.dumps(error_data) + '\n\n'
|
||||
print(f"[{datetime.now()}] 发送最后的数据: {tmp}")
|
||||
# 累积 new_text
|
||||
all_new_text += ''.join("```\n" + error_message + "\n```")
|
||||
yield 'data: ' + json.dumps(error_data) + '\n\n'
|
||||
except json.JSONDecodeError:
|
||||
# print("JSON 解析错误")
|
||||
@@ -634,6 +641,39 @@ def chat_completions():
|
||||
delete_conversation(conversation_id, api_key)
|
||||
|
||||
|
||||
if not stream:
|
||||
# 执行流式响应的生成函数来累积 all_new_text
|
||||
# 迭代生成器对象以执行其内部逻辑
|
||||
for _ in generate():
|
||||
pass
|
||||
# 构造响应的 JSON 结构
|
||||
response_json = {
|
||||
"id": generate_unique_id("chatcmpl"),
|
||||
"object": "chat.completion",
|
||||
"created": int(time.time()), # 使用当前时间戳
|
||||
"model": model, # 使用请求中指定的模型
|
||||
"choices": [
|
||||
{
|
||||
"index": 0,
|
||||
"message": {
|
||||
"role": "assistant",
|
||||
"content": all_new_text # 使用累积的文本
|
||||
},
|
||||
"finish_reason": "stop"
|
||||
}
|
||||
],
|
||||
"usage": {
|
||||
# 这里的 token 计数需要根据实际情况计算
|
||||
"prompt_tokens": 0,
|
||||
"completion_tokens": 0,
|
||||
"total_tokens": 0
|
||||
},
|
||||
"system_fingerprint": None
|
||||
}
|
||||
|
||||
# 返回 JSON 响应
|
||||
return jsonify(response_json)
|
||||
else:
|
||||
return Response(generate(), mimetype='text/event-stream')
|
||||
|
||||
@app.after_request
|
||||
|
Reference in New Issue
Block a user