|
@@ -1,5 +1,6 @@
|
|
|
import json
|
|
|
import logging
|
|
|
+import time
|
|
|
from collections.abc import Generator
|
|
|
from datetime import UTC, datetime
|
|
|
|
|
@@ -204,7 +205,7 @@ class ChatApiForRobot(Resource):
|
|
|
headers = {
|
|
|
"Content-Type": "application/json",
|
|
|
"User-Agent": "Robot",
|
|
|
- "Accept": "text/stream",
|
|
|
+ "Accept": "*/*",
|
|
|
"Authorization": f'Bearer {access_token}',
|
|
|
"Connection": "keep-alive",
|
|
|
}
|
|
@@ -214,12 +215,42 @@ class ChatApiForRobot(Resource):
|
|
|
# 4) 封装请求url并发送请求
|
|
|
chat_message_url = f'{request.host_url}console/api/installed-apps/{installed_app_id}/chat-messages'
|
|
|
logging.info("Sending request to %s", chat_message_url)
|
|
|
- response = requests.post(chat_message_url, data=json.dumps(data), headers=headers)
|
|
|
|
|
|
# 5) 按照输出要求处理返回的流式数据
|
|
|
if args["stream"]:
|
|
|
def after_response_generator():
|
|
|
+ timestamp = int(time.time())
|
|
|
+ new_content = {
|
|
|
+ "id": id,
|
|
|
+ "model": "advanced-chat",
|
|
|
+ "created": timestamp,
|
|
|
+ "choices": [],
|
|
|
+ }
|
|
|
+
|
|
|
i = 0
|
|
|
+ choice = {
|
|
|
+ "index": 0,
|
|
|
+ "delta": {
|
|
|
+ "role": "assistant",
|
|
|
+ "content": ""
|
|
|
+ },
|
|
|
+ "finish_reason": None
|
|
|
+ }
|
|
|
+ new_content["choices"].append(choice)
|
|
|
+ yield f"id: {i}\ndata: {json.dumps(new_content)}\n\n"
|
|
|
+ new_content["choices"].pop()
|
|
|
+
|
|
|
+ i = i + 1
|
|
|
+ choice["delta"] = {
|
|
|
+ "role": None,
|
|
|
+ "content": "您好!您的问题已收到,正在为您检索,请稍等..."
|
|
|
+ }
|
|
|
+ new_content["choices"].append(choice)
|
|
|
+ yield f"id: {i}\ndata: {json.dumps(new_content)}\n\n"
|
|
|
+ new_content["choices"].pop()
|
|
|
+
|
|
|
+ i = i + 1
|
|
|
+ response = requests.post(chat_message_url, data=json.dumps(data), headers=headers)
|
|
|
for line in response.iter_lines():
|
|
|
line_str = line.decode("utf-8")
|
|
|
if not line_str.startswith('data:'):
|
|
@@ -230,47 +261,15 @@ class ChatApiForRobot(Resource):
|
|
|
if event not in ["message", "message_end"]:
|
|
|
continue
|
|
|
|
|
|
- new_content = {
|
|
|
- "id": id,
|
|
|
- "model": "advanced-chat",
|
|
|
- "created": content["created_at"],
|
|
|
- "choices": [],
|
|
|
- }
|
|
|
-
|
|
|
- if i == 0:
|
|
|
- choice = {
|
|
|
- "index": 0,
|
|
|
- "delta": {
|
|
|
- "role": "assistant",
|
|
|
- "content": ""
|
|
|
- },
|
|
|
- "finish_reason": None
|
|
|
- }
|
|
|
- new_content["choices"].append(choice)
|
|
|
- yield f"id: {i}\ndata: {json.dumps(new_content)}\n\n"
|
|
|
- new_content["choices"].pop()
|
|
|
+ new_content["created"] = content["created_at"]
|
|
|
|
|
|
i = i + 1
|
|
|
if content["event"] == "message":
|
|
|
- choice = {
|
|
|
- "index": 0,
|
|
|
- "delta": {
|
|
|
- "role": None,
|
|
|
- "content": content["answer"]
|
|
|
- },
|
|
|
- "finish_reason": None
|
|
|
- }
|
|
|
- new_content["choices"].append(choice)
|
|
|
+ choice["delta"]["content"] = content["answer"]
|
|
|
else:
|
|
|
- choice = {
|
|
|
- "index": 0,
|
|
|
- "delta": {
|
|
|
- "role": None,
|
|
|
- "content": ""
|
|
|
- },
|
|
|
- "finish_reason": "stop"
|
|
|
- }
|
|
|
- new_content["choices"].append(choice)
|
|
|
+ choice["delta"]["content"] = ""
|
|
|
+ choice["finish_reason"] = "stop"
|
|
|
+ new_content["choices"] = [choice]
|
|
|
|
|
|
yield f"id: {i}\ndata: {json.dumps(new_content)}\n\n"
|
|
|
|
|
@@ -281,6 +280,7 @@ class ChatApiForRobot(Resource):
|
|
|
|
|
|
return helper.compact_generate_response(generate())
|
|
|
else:
|
|
|
+ response = requests.post(chat_message_url, data=json.dumps(data), headers=headers)
|
|
|
content = json.loads(response.text)
|
|
|
new_response = {
|
|
|
"id": id,
|