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
|
### 0.0.9
|
||||||
|
|
||||||
- 修复在 ChatGPT-Next-Web 网页端修改请求接口后出现 `Failed to fetch` 报错的问题
|
- 修复在 ChatGPT-Next-Web 网页端修改请求接口后出现 `Failed to fetch` 报错的问题
|
||||||
|
@@ -2,7 +2,7 @@ version: '3'
|
|||||||
|
|
||||||
services:
|
services:
|
||||||
backend-to-api:
|
backend-to-api:
|
||||||
image: wizerd/pandora-to-api:0.0.9
|
image: wizerd/pandora-to-api:latest
|
||||||
restart: always
|
restart: always
|
||||||
ports:
|
ports:
|
||||||
- "50011:33333"
|
- "50011:33333"
|
||||||
@@ -16,7 +16,7 @@ services:
|
|||||||
|
|
||||||
|
|
||||||
uploader:
|
uploader:
|
||||||
image: wizerd/pandora-to-api:0.0.9
|
image: wizerd/pandora-to-api:latest
|
||||||
restart: always
|
restart: always
|
||||||
entrypoint: ["python3", "/app/upload.py"]
|
entrypoint: ["python3", "/app/upload.py"]
|
||||||
volumes:
|
volumes:
|
||||||
|
600
main.py
600
main.py
@@ -26,8 +26,8 @@ BASE_URL = os.getenv('BASE_URL', '')
|
|||||||
PROXY_API_PREFIX = os.getenv('PROXY_API_PREFIX', '')
|
PROXY_API_PREFIX = os.getenv('PROXY_API_PREFIX', '')
|
||||||
UPLOAD_BASE_URL = os.getenv('UPLOAD_BASE_URL', '')
|
UPLOAD_BASE_URL = os.getenv('UPLOAD_BASE_URL', '')
|
||||||
|
|
||||||
VERSION = '0.0.9'
|
VERSION = '0.0.10'
|
||||||
UPDATE_INFO = '修复在 ChatGPT-Next-Web 网页端修改请求接口后出现 `Failed to fetch` 报错的问题'
|
UPDATE_INFO = '支持非流式响应'
|
||||||
|
|
||||||
with app.app_context():
|
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,
|
"history_and_training_disabled": False,
|
||||||
"conversation_mode":{"kind":"primary_assistant"},"force_paragen":False,"force_rate_limit":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)
|
# print(response)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@@ -338,302 +338,342 @@ def chat_completions():
|
|||||||
|
|
||||||
upstream_response = send_text_prompt_and_get_response(messages, api_key, stream, model)
|
upstream_response = send_text_prompt_and_get_response(messages, api_key, stream, model)
|
||||||
|
|
||||||
if not stream:
|
# 在非流式响应的情况下,我们需要一个变量来累积所有的 new_text
|
||||||
return Response(upstream_response)
|
all_new_text = ""
|
||||||
else:
|
|
||||||
# 处理流式响应
|
|
||||||
def generate():
|
|
||||||
chat_message_id = generate_unique_id("chatcmpl")
|
|
||||||
# 当前时间戳
|
|
||||||
timestamp = int(time.time())
|
|
||||||
|
|
||||||
buffer = ""
|
# 处理流式响应
|
||||||
last_full_text = "" # 用于存储之前所有出现过的 parts 组成的完整文本
|
def generate():
|
||||||
last_full_code = ""
|
nonlocal all_new_text # 引用外部变量
|
||||||
last_full_code_result = ""
|
chat_message_id = generate_unique_id("chatcmpl")
|
||||||
last_content_type = None # 用于记录上一个消息的内容类型
|
# 当前时间戳
|
||||||
conversation_id = ''
|
timestamp = int(time.time())
|
||||||
citation_buffer = ""
|
|
||||||
citation_accumulating = False
|
buffer = ""
|
||||||
for chunk in upstream_response.iter_content(chunk_size=1024):
|
last_full_text = "" # 用于存储之前所有出现过的 parts 组成的完整文本
|
||||||
if chunk:
|
last_full_code = ""
|
||||||
buffer += chunk.decode('utf-8')
|
last_full_code_result = ""
|
||||||
# 检查是否存在 "event: ping",如果存在,则只保留 "data:" 后面的内容
|
last_content_type = None # 用于记录上一个消息的内容类型
|
||||||
if "event: ping" in buffer:
|
conversation_id = ''
|
||||||
if "data:" in buffer:
|
citation_buffer = ""
|
||||||
buffer = buffer.split("data:", 1)[1]
|
citation_accumulating = False
|
||||||
buffer = "data:" + buffer
|
for chunk in upstream_response.iter_content(chunk_size=1024):
|
||||||
# 使用正则表达式移除特定格式的字符串
|
if chunk:
|
||||||
# print("应用正则表达式之前的 buffer:", buffer.replace('\n', '\\n'))
|
buffer += chunk.decode('utf-8')
|
||||||
buffer = re.sub(r'data: \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{6}(\r\n|\r|\n){2}', '', buffer)
|
# 检查是否存在 "event: ping",如果存在,则只保留 "data:" 后面的内容
|
||||||
# print("应用正则表达式之后的 buffer:", buffer.replace('\n', '\\n'))
|
if "event: ping" in buffer:
|
||||||
|
if "data:" in buffer:
|
||||||
|
buffer = buffer.split("data:", 1)[1]
|
||||||
|
buffer = "data:" + buffer
|
||||||
|
# 使用正则表达式移除特定格式的字符串
|
||||||
|
# print("应用正则表达式之前的 buffer:", buffer.replace('\n', '\\n'))
|
||||||
|
buffer = re.sub(r'data: \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{6}(\r\n|\r|\n){2}', '', buffer)
|
||||||
|
# print("应用正则表达式之后的 buffer:", buffer.replace('\n', '\\n'))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
while 'data:' in buffer and '\n\n' in buffer:
|
while 'data:' in buffer and '\n\n' in buffer:
|
||||||
end_index = buffer.index('\n\n') + 2
|
end_index = buffer.index('\n\n') + 2
|
||||||
complete_data, buffer = buffer[:end_index], buffer[end_index:]
|
complete_data, buffer = buffer[:end_index], buffer[end_index:]
|
||||||
# 解析 data 块
|
# 解析 data 块
|
||||||
|
try:
|
||||||
|
data_json = json.loads(complete_data.replace('data: ', ''))
|
||||||
|
# print(f"data_json: {data_json}")
|
||||||
|
message = data_json.get("message", {})
|
||||||
|
message_status = message.get("status")
|
||||||
|
content = message.get("content", {})
|
||||||
|
role = message.get("author", {}).get("role")
|
||||||
|
content_type = content.get("content_type")
|
||||||
|
print(f"content_type: {content_type}")
|
||||||
|
print(f"last_content_type: {last_content_type}")
|
||||||
|
|
||||||
|
metadata = {}
|
||||||
|
citations = []
|
||||||
try:
|
try:
|
||||||
data_json = json.loads(complete_data.replace('data: ', ''))
|
metadata = message.get("metadata", {})
|
||||||
# print(f"data_json: {data_json}")
|
citations = metadata.get("citations", [])
|
||||||
message = data_json.get("message", {})
|
except:
|
||||||
message_status = message.get("status")
|
pass
|
||||||
content = message.get("content", {})
|
name = message.get("author", {}).get("name")
|
||||||
role = message.get("author", {}).get("role")
|
if (role == "user" or message_status == "finished_successfully" or role == "system") and role != "tool":
|
||||||
content_type = content.get("content_type")
|
# 如果是用户发来的消息,直接舍弃
|
||||||
print(f"content_type: {content_type}")
|
continue
|
||||||
print(f"last_content_type: {last_content_type}")
|
try:
|
||||||
|
conversation_id = data_json.get("conversation_id")
|
||||||
|
print(f"conversation_id: {conversation_id}")
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
# 只获取新的部分
|
||||||
|
new_text = ""
|
||||||
|
is_img_message = False
|
||||||
|
parts = content.get("parts", [])
|
||||||
|
for part in parts:
|
||||||
|
try:
|
||||||
|
# print(f"part: {part}")
|
||||||
|
# print(f"part type: {part.get('content_type')}")
|
||||||
|
if part.get('content_type') == 'image_asset_pointer':
|
||||||
|
print(f"find img message~")
|
||||||
|
is_img_message = True
|
||||||
|
asset_pointer = part.get('asset_pointer').replace('file-service://', '')
|
||||||
|
print(f"asset_pointer: {asset_pointer}")
|
||||||
|
image_url = f"{BASE_URL}/{PROXY_API_PREFIX}/backend-api/files/{asset_pointer}/download"
|
||||||
|
|
||||||
metadata = {}
|
headers = {
|
||||||
citations = []
|
"Authorization": f"Bearer {api_key}"
|
||||||
try:
|
}
|
||||||
metadata = message.get("metadata", {})
|
image_response = requests.get(image_url, headers=headers)
|
||||||
citations = metadata.get("citations", [])
|
|
||||||
except:
|
if image_response.status_code == 200:
|
||||||
pass
|
download_url = image_response.json().get('download_url')
|
||||||
name = message.get("author", {}).get("name")
|
print(f"download_url: {download_url}")
|
||||||
if (role == "user" or message_status == "finished_successfully" or role == "system") and role != "tool":
|
# 从URL下载图片
|
||||||
# 如果是用户发来的消息,直接舍弃
|
# image_data = requests.get(download_url).content
|
||||||
continue
|
image_download_response = requests.get(download_url)
|
||||||
try:
|
# print(f"image_download_response: {image_download_response.text}")
|
||||||
conversation_id = data_json.get("conversation_id")
|
if image_download_response.status_code == 200:
|
||||||
print(f"conversation_id: {conversation_id}")
|
print(f"下载图片成功")
|
||||||
except:
|
image_data = image_download_response.content
|
||||||
pass
|
today_image_url = save_image(image_data) # 保存图片,并获取文件名
|
||||||
# 只获取新的部分
|
new_text = f"\n\n[下载链接]({UPLOAD_BASE_URL}/{today_image_url})\n"
|
||||||
new_text = ""
|
else:
|
||||||
is_img_message = False
|
print(f"下载图片失败: {image_download_response.text}")
|
||||||
parts = content.get("parts", [])
|
if last_content_type == "code":
|
||||||
for part in parts:
|
new_text = "\n```\n" + new_text
|
||||||
try:
|
print(f"new_text: {new_text}")
|
||||||
# print(f"part: {part}")
|
|
||||||
# print(f"part type: {part.get('content_type')}")
|
|
||||||
if part.get('content_type') == 'image_asset_pointer':
|
|
||||||
print(f"find img message~")
|
|
||||||
is_img_message = True
|
is_img_message = True
|
||||||
asset_pointer = part.get('asset_pointer').replace('file-service://', '')
|
else:
|
||||||
print(f"asset_pointer: {asset_pointer}")
|
print(f"获取图片下载链接失败: {image_response.text}")
|
||||||
image_url = f"{BASE_URL}/{PROXY_API_PREFIX}/backend-api/files/{asset_pointer}/download"
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
headers = {
|
if is_img_message == False:
|
||||||
"Authorization": f"Bearer {api_key}"
|
# print(f"data_json: {data_json}")
|
||||||
}
|
if content_type == "multimodal_text" and last_content_type == "code":
|
||||||
image_response = requests.get(image_url, headers=headers)
|
new_text = "\n```\n" + content.get("text", "")
|
||||||
|
elif role == "tool" and name == "dalle.text2im":
|
||||||
|
print(f"无视消息: {content.get('text', '')}")
|
||||||
|
continue
|
||||||
|
# 代码块特殊处理
|
||||||
|
if content_type == "code" and last_content_type != "code" and content_type != None:
|
||||||
|
full_code = ''.join(content.get("text", ""))
|
||||||
|
new_text = "\n```\n" + full_code[len(last_full_code):]
|
||||||
|
# print(f"full_code: {full_code}")
|
||||||
|
# print(f"last_full_code: {last_full_code}")
|
||||||
|
# print(f"new_text: {new_text}")
|
||||||
|
last_full_code = full_code # 更新完整代码以备下次比较
|
||||||
|
|
||||||
|
elif last_content_type == "code" and content_type != "code" and content_type != None:
|
||||||
|
full_code = ''.join(content.get("text", ""))
|
||||||
|
new_text = "\n```\n" + full_code[len(last_full_code):]
|
||||||
|
# print(f"full_code: {full_code}")
|
||||||
|
# print(f"last_full_code: {last_full_code}")
|
||||||
|
# print(f"new_text: {new_text}")
|
||||||
|
last_full_code = "" # 更新完整代码以备下次比较
|
||||||
|
|
||||||
if image_response.status_code == 200:
|
elif content_type == "code" and last_content_type == "code" and content_type != None:
|
||||||
download_url = image_response.json().get('download_url')
|
full_code = ''.join(content.get("text", ""))
|
||||||
print(f"download_url: {download_url}")
|
new_text = full_code[len(last_full_code):]
|
||||||
# 从URL下载图片
|
# print(f"full_code: {full_code}")
|
||||||
# image_data = requests.get(download_url).content
|
# print(f"last_full_code: {last_full_code}")
|
||||||
image_download_response = requests.get(download_url)
|
# print(f"new_text: {new_text}")
|
||||||
# print(f"image_download_response: {image_download_response.text}")
|
last_full_code = full_code # 更新完整代码以备下次比较
|
||||||
if image_download_response.status_code == 200:
|
|
||||||
print(f"下载图片成功")
|
else:
|
||||||
image_data = image_download_response.content
|
# 只获取新的 parts
|
||||||
today_image_url = save_image(image_data) # 保存图片,并获取文件名
|
parts = content.get("parts", [])
|
||||||
new_text = f"\n\n[下载链接]({UPLOAD_BASE_URL}/{today_image_url})\n"
|
full_text = ''.join(parts)
|
||||||
|
new_text = full_text[len(last_full_text):]
|
||||||
|
last_full_text = full_text # 更新完整文本以备下次比较
|
||||||
|
if "\u3010" in new_text and not citation_accumulating:
|
||||||
|
citation_accumulating = True
|
||||||
|
citation_buffer = citation_buffer + new_text
|
||||||
|
print(f"开始积累引用: {citation_buffer}")
|
||||||
|
elif citation_accumulating:
|
||||||
|
citation_buffer += new_text
|
||||||
|
print(f"积累引用: {citation_buffer}")
|
||||||
|
if citation_accumulating:
|
||||||
|
if is_valid_citation_format(citation_buffer):
|
||||||
|
print(f"合法格式: {citation_buffer}")
|
||||||
|
# 继续积累
|
||||||
|
if is_complete_citation_format(citation_buffer):
|
||||||
|
|
||||||
|
# 替换完整的引用格式
|
||||||
|
replaced_text, remaining_text, is_potential_citation = replace_complete_citation(citation_buffer, citations)
|
||||||
|
# print(replaced_text) # 输出替换后的文本
|
||||||
|
new_text = replaced_text
|
||||||
|
|
||||||
|
if(is_potential_citation):
|
||||||
|
citation_buffer = remaining_text
|
||||||
else:
|
else:
|
||||||
print(f"下载图片失败: {image_download_response.text}")
|
citation_accumulating = False
|
||||||
if last_content_type == "code":
|
citation_buffer = ""
|
||||||
new_text = "\n```\n" + new_text
|
print(f"替换完整的引用格式: {new_text}")
|
||||||
print(f"new_text: {new_text}")
|
|
||||||
is_img_message = True
|
|
||||||
else:
|
else:
|
||||||
print(f"获取图片下载链接失败: {image_response.text}")
|
continue
|
||||||
except:
|
else:
|
||||||
pass
|
# 不是合法格式,放弃积累并响应
|
||||||
|
print(f"不合法格式: {citation_buffer}")
|
||||||
|
new_text = citation_buffer
|
||||||
if is_img_message == False:
|
citation_accumulating = False
|
||||||
# print(f"data_json: {data_json}")
|
citation_buffer = ""
|
||||||
if content_type == "multimodal_text" and last_content_type == "code":
|
|
||||||
new_text = "\n```\n" + content.get("text", "")
|
|
||||||
elif role == "tool" and name == "dalle.text2im":
|
|
||||||
print(f"无视消息: {content.get('text', '')}")
|
|
||||||
continue
|
|
||||||
# 代码块特殊处理
|
|
||||||
if content_type == "code" and last_content_type != "code" and content_type != None:
|
|
||||||
full_code = ''.join(content.get("text", ""))
|
|
||||||
new_text = "\n```\n" + full_code[len(last_full_code):]
|
|
||||||
# print(f"full_code: {full_code}")
|
|
||||||
# print(f"last_full_code: {last_full_code}")
|
|
||||||
# print(f"new_text: {new_text}")
|
|
||||||
last_full_code = full_code # 更新完整代码以备下次比较
|
|
||||||
|
|
||||||
elif last_content_type == "code" and content_type != "code" and content_type != None:
|
|
||||||
full_code = ''.join(content.get("text", ""))
|
|
||||||
new_text = "\n```\n" + full_code[len(last_full_code):]
|
|
||||||
# print(f"full_code: {full_code}")
|
|
||||||
# print(f"last_full_code: {last_full_code}")
|
|
||||||
# print(f"new_text: {new_text}")
|
|
||||||
last_full_code = "" # 更新完整代码以备下次比较
|
|
||||||
|
|
||||||
elif content_type == "code" and last_content_type == "code" and content_type != None:
|
|
||||||
full_code = ''.join(content.get("text", ""))
|
|
||||||
new_text = full_code[len(last_full_code):]
|
|
||||||
# print(f"full_code: {full_code}")
|
|
||||||
# print(f"last_full_code: {last_full_code}")
|
|
||||||
# print(f"new_text: {new_text}")
|
|
||||||
last_full_code = full_code # 更新完整代码以备下次比较
|
|
||||||
|
|
||||||
else:
|
|
||||||
# 只获取新的 parts
|
|
||||||
parts = content.get("parts", [])
|
|
||||||
full_text = ''.join(parts)
|
|
||||||
new_text = full_text[len(last_full_text):]
|
|
||||||
last_full_text = full_text # 更新完整文本以备下次比较
|
|
||||||
if "\u3010" in new_text and not citation_accumulating:
|
|
||||||
citation_accumulating = True
|
|
||||||
citation_buffer = citation_buffer + new_text
|
|
||||||
print(f"开始积累引用: {citation_buffer}")
|
|
||||||
elif citation_accumulating:
|
|
||||||
citation_buffer += new_text
|
|
||||||
print(f"积累引用: {citation_buffer}")
|
|
||||||
if citation_accumulating:
|
|
||||||
if is_valid_citation_format(citation_buffer):
|
|
||||||
print(f"合法格式: {citation_buffer}")
|
|
||||||
# 继续积累
|
|
||||||
if is_complete_citation_format(citation_buffer):
|
|
||||||
|
|
||||||
# 替换完整的引用格式
|
|
||||||
replaced_text, remaining_text, is_potential_citation = replace_complete_citation(citation_buffer, citations)
|
|
||||||
# print(replaced_text) # 输出替换后的文本
|
|
||||||
new_text = replaced_text
|
|
||||||
|
|
||||||
if(is_potential_citation):
|
|
||||||
citation_buffer = remaining_text
|
|
||||||
else:
|
|
||||||
citation_accumulating = False
|
|
||||||
citation_buffer = ""
|
|
||||||
print(f"替换完整的引用格式: {new_text}")
|
|
||||||
else:
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
# 不是合法格式,放弃积累并响应
|
|
||||||
print(f"不合法格式: {citation_buffer}")
|
|
||||||
new_text = citation_buffer
|
|
||||||
citation_accumulating = False
|
|
||||||
citation_buffer = ""
|
|
||||||
|
|
||||||
|
|
||||||
# Python 工具执行输出特殊处理
|
# Python 工具执行输出特殊处理
|
||||||
if role == "tool" and name == "python" and last_content_type != "execution_output" and content_type != None:
|
if role == "tool" and name == "python" and last_content_type != "execution_output" and content_type != None:
|
||||||
|
|
||||||
|
|
||||||
full_code_result = ''.join(content.get("text", ""))
|
full_code_result = ''.join(content.get("text", ""))
|
||||||
new_text = "`Result:` \n```\n" + full_code_result[len(last_full_code_result):]
|
new_text = "`Result:` \n```\n" + full_code_result[len(last_full_code_result):]
|
||||||
if last_content_type == "code":
|
if last_content_type == "code":
|
||||||
new_text = "\n```\n" + new_text
|
new_text = "\n```\n" + new_text
|
||||||
# print(f"full_code_result: {full_code_result}")
|
# print(f"full_code_result: {full_code_result}")
|
||||||
# print(f"last_full_code_result: {last_full_code_result}")
|
# print(f"last_full_code_result: {last_full_code_result}")
|
||||||
# print(f"new_text: {new_text}")
|
# print(f"new_text: {new_text}")
|
||||||
last_full_code_result = full_code_result # 更新完整代码以备下次比较
|
last_full_code_result = full_code_result # 更新完整代码以备下次比较
|
||||||
elif last_content_type == "execution_output" and (role != "tool" or name != "python") and content_type != None:
|
elif last_content_type == "execution_output" and (role != "tool" or name != "python") and content_type != None:
|
||||||
# new_text = content.get("text", "") + "\n```"
|
# new_text = content.get("text", "") + "\n```"
|
||||||
full_code_result = ''.join(content.get("text", ""))
|
full_code_result = ''.join(content.get("text", ""))
|
||||||
new_text = full_code_result[len(last_full_code_result):] + "\n```\n"
|
new_text = full_code_result[len(last_full_code_result):] + "\n```\n"
|
||||||
if content_type == "code":
|
if content_type == "code":
|
||||||
new_text = new_text + "\n```\n"
|
new_text = new_text + "\n```\n"
|
||||||
# print(f"full_code_result: {full_code_result}")
|
# print(f"full_code_result: {full_code_result}")
|
||||||
# print(f"last_full_code_result: {last_full_code_result}")
|
# print(f"last_full_code_result: {last_full_code_result}")
|
||||||
# print(f"new_text: {new_text}")
|
# print(f"new_text: {new_text}")
|
||||||
last_full_code_result = "" # 更新完整代码以备下次比较
|
last_full_code_result = "" # 更新完整代码以备下次比较
|
||||||
elif last_content_type == "execution_output" and role == "tool" and name == "python" and content_type != None:
|
elif last_content_type == "execution_output" and role == "tool" and name == "python" and content_type != None:
|
||||||
full_code_result = ''.join(content.get("text", ""))
|
full_code_result = ''.join(content.get("text", ""))
|
||||||
new_text = full_code_result[len(last_full_code_result):]
|
new_text = full_code_result[len(last_full_code_result):]
|
||||||
# print(f"full_code_result: {full_code_result}")
|
# print(f"full_code_result: {full_code_result}")
|
||||||
# print(f"last_full_code_result: {last_full_code_result}")
|
# print(f"last_full_code_result: {last_full_code_result}")
|
||||||
# print(f"new_text: {new_text}")
|
# print(f"new_text: {new_text}")
|
||||||
last_full_code_result = full_code_result
|
last_full_code_result = full_code_result
|
||||||
|
|
||||||
# print(f"[{datetime.now()}] 收到数据: {data_json}")
|
# print(f"[{datetime.now()}] 收到数据: {data_json}")
|
||||||
# print(f"[{datetime.now()}] 收到的完整文本: {full_text}")
|
# print(f"[{datetime.now()}] 收到的完整文本: {full_text}")
|
||||||
# print(f"[{datetime.now()}] 上次收到的完整文本: {last_full_text}")
|
# print(f"[{datetime.now()}] 上次收到的完整文本: {last_full_text}")
|
||||||
# print(f"[{datetime.now()}] 新的文本: {new_text}")
|
# print(f"[{datetime.now()}] 新的文本: {new_text}")
|
||||||
|
|
||||||
# 更新 last_content_type
|
# 更新 last_content_type
|
||||||
if content_type != None:
|
if content_type != None:
|
||||||
last_content_type = content_type if role != "user" else last_content_type
|
last_content_type = content_type if role != "user" else last_content_type
|
||||||
|
|
||||||
|
|
||||||
new_data = {
|
new_data = {
|
||||||
"id": chat_message_id,
|
"id": chat_message_id,
|
||||||
"object": "chat.completion.chunk",
|
"object": "chat.completion.chunk",
|
||||||
"created": timestamp,
|
"created": timestamp,
|
||||||
"model": message.get("metadata", {}).get("model_slug"),
|
"model": message.get("metadata", {}).get("model_slug"),
|
||||||
"choices": [
|
"choices": [
|
||||||
{
|
{
|
||||||
"index": 0,
|
"index": 0,
|
||||||
"delta": {
|
"delta": {
|
||||||
"content": ''.join(new_text)
|
"content": ''.join(new_text)
|
||||||
},
|
},
|
||||||
"finish_reason": None
|
"finish_reason": None
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
|
||||||
# print(f"Role: {role}")
|
|
||||||
print(f"[{datetime.now()}] 发送消息: {new_text}")
|
|
||||||
tmp = 'data: ' + json.dumps(new_data, ensure_ascii=False) + '\n\n'
|
|
||||||
# print(f"[{datetime.now()}] 发送数据: {tmp}")
|
|
||||||
yield 'data: ' + json.dumps(new_data, ensure_ascii=False) + '\n\n'
|
|
||||||
except json.JSONDecodeError:
|
|
||||||
# print("JSON 解析错误")
|
|
||||||
print(f"[{datetime.now()}] 发送数据: {complete_data}")
|
|
||||||
if complete_data == 'data: [DONE]\n\n':
|
|
||||||
print(f"[{datetime.now()}] 会话结束")
|
|
||||||
yield complete_data
|
|
||||||
if citation_buffer != "":
|
|
||||||
new_data = {
|
|
||||||
"id": chat_message_id,
|
|
||||||
"object": "chat.completion.chunk",
|
|
||||||
"created": timestamp,
|
|
||||||
"model": message.get("metadata", {}).get("model_slug"),
|
|
||||||
"choices": [
|
|
||||||
{
|
|
||||||
"index": 0,
|
|
||||||
"delta": {
|
|
||||||
"content": ''.join(citation_buffer)
|
|
||||||
},
|
|
||||||
"finish_reason": None
|
|
||||||
}
|
}
|
||||||
]
|
# print(f"Role: {role}")
|
||||||
}
|
print(f"[{datetime.now()}] 发送消息: {new_text}")
|
||||||
tmp = 'data: ' + json.dumps(new_data) + '\n\n'
|
tmp = 'data: ' + json.dumps(new_data, ensure_ascii=False) + '\n\n'
|
||||||
# print(f"[{datetime.now()}] 发送数据: {tmp}")
|
# print(f"[{datetime.now()}] 发送数据: {tmp}")
|
||||||
yield 'data: ' + json.dumps(new_data) + '\n\n'
|
# 累积 new_text
|
||||||
if buffer:
|
all_new_text += new_text
|
||||||
# print(f"[{datetime.now()}] 最后的数据: {buffer}")
|
yield 'data: ' + json.dumps(new_data, ensure_ascii=False) + '\n\n'
|
||||||
delete_conversation(conversation_id, api_key)
|
except json.JSONDecodeError:
|
||||||
try:
|
# print("JSON 解析错误")
|
||||||
buffer_json = json.loads(buffer)
|
print(f"[{datetime.now()}] 发送数据: {complete_data}")
|
||||||
error_message = buffer_json.get("detail", {}).get("message", "未知错误")
|
if complete_data == 'data: [DONE]\n\n':
|
||||||
error_data = {
|
print(f"[{datetime.now()}] 会话结束")
|
||||||
"id": chat_message_id,
|
yield complete_data
|
||||||
"object": "chat.completion.chunk",
|
if citation_buffer != "":
|
||||||
"created": timestamp,
|
new_data = {
|
||||||
"model": "error",
|
"id": chat_message_id,
|
||||||
"choices": [
|
"object": "chat.completion.chunk",
|
||||||
{
|
"created": timestamp,
|
||||||
"index": 0,
|
"model": message.get("metadata", {}).get("model_slug"),
|
||||||
"delta": {
|
"choices": [
|
||||||
"content": ''.join("```\n" + error_message + "\n```")
|
{
|
||||||
},
|
"index": 0,
|
||||||
"finish_reason": None
|
"delta": {
|
||||||
}
|
"content": ''.join(citation_buffer)
|
||||||
]
|
},
|
||||||
}
|
"finish_reason": None
|
||||||
tmp = 'data: ' + json.dumps(error_data) + '\n\n'
|
}
|
||||||
print(f"[{datetime.now()}] 发送最后的数据: {tmp}")
|
]
|
||||||
yield 'data: ' + json.dumps(error_data) + '\n\n'
|
}
|
||||||
except json.JSONDecodeError:
|
tmp = 'data: ' + json.dumps(new_data) + '\n\n'
|
||||||
# print("JSON 解析错误")
|
# print(f"[{datetime.now()}] 发送数据: {tmp}")
|
||||||
print(f"[{datetime.now()}] 发送最后的数据: {buffer}")
|
# 累积 new_text
|
||||||
yield buffer
|
all_new_text += citation_buffer
|
||||||
|
yield 'data: ' + json.dumps(new_data) + '\n\n'
|
||||||
|
if buffer:
|
||||||
|
# print(f"[{datetime.now()}] 最后的数据: {buffer}")
|
||||||
|
delete_conversation(conversation_id, api_key)
|
||||||
|
try:
|
||||||
|
buffer_json = json.loads(buffer)
|
||||||
|
error_message = buffer_json.get("detail", {}).get("message", "未知错误")
|
||||||
|
error_data = {
|
||||||
|
"id": chat_message_id,
|
||||||
|
"object": "chat.completion.chunk",
|
||||||
|
"created": timestamp,
|
||||||
|
"model": "error",
|
||||||
|
"choices": [
|
||||||
|
{
|
||||||
|
"index": 0,
|
||||||
|
"delta": {
|
||||||
|
"content": ''.join("```\n" + error_message + "\n```")
|
||||||
|
},
|
||||||
|
"finish_reason": None
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
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 解析错误")
|
||||||
|
print(f"[{datetime.now()}] 发送最后的数据: {buffer}")
|
||||||
|
yield buffer
|
||||||
|
|
||||||
delete_conversation(conversation_id, api_key)
|
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')
|
return Response(generate(), mimetype='text/event-stream')
|
||||||
|
|
||||||
@app.after_request
|
@app.after_request
|
||||||
|
Reference in New Issue
Block a user