更新机器人联系上下文

This commit is contained in:
2024-05-20 20:30:51 +08:00
parent 8b03089aca
commit 569fc6f985
+11 -6
View File
@@ -7,10 +7,15 @@ import json
import websockets
import re
from zhipuai import ZhipuAI
async def gml_chat(messages):
conversation_history = []
async def gml_chat(messages,conversation_history):
if len(conversation_history) >= 4:
conversation_history.pop(0)
print("-==========================---")
print(conversation_history)
client = ZhipuAI(api_key="92a0e4868cda21d22fe6fd0f4c0bf6fb.oZ4IE9PPDOhsLKPp")
conversation_history.append({"role": "user", "content": messages})
# conversation_history.extend(messages)
response = client.chat.completions.create(
model="glm-3-turbo",
# messages=[
@@ -19,7 +24,7 @@ async def gml_chat(messages):
# "content": prompt
# }
# ],
messages = messages,
messages = conversation_history,
top_p=0.7,
temperature=0.95,
max_tokens=1024,
@@ -29,10 +34,10 @@ async def gml_chat(messages):
for trunk in response:
# print(trunk.choices[0].delta)
content = content + trunk.choices[0].delta.content
conversation_history.append({"role": "assistant", "content": content})
robot_message = json.dumps(
{"type": "user", "content": content, "from": "机器人"})
await asyncio.wait([user.send(robot_message) for user in USERS.values()])
return content
# print(gml_chat("你好"))
@@ -62,7 +67,7 @@ async def chat(websocket, path):
message = json.dumps(
{"type": "user", "content": data["content"], "from": name})
await asyncio.wait([user.send(message) for user in USERS.values()])
robot_reply = await gml_chat(question_rebot) # 获取机器人的回复
robot_reply = await gml_chat(question_rebot,conversation_history) # 获取机器人的回复
# await asyncio.wait([user.send(message) for user in USERS.values()])
# robot_message = json.dumps(
# {"type": "user", "content": robot_reply, "from": "机器人"})