更新机器人联系上下文
This commit is contained in:
+11
-6
@@ -7,10 +7,15 @@ import json
|
|||||||
import websockets
|
import websockets
|
||||||
import re
|
import re
|
||||||
from zhipuai import ZhipuAI
|
from zhipuai import ZhipuAI
|
||||||
|
conversation_history = []
|
||||||
async def gml_chat(messages):
|
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")
|
client = ZhipuAI(api_key="92a0e4868cda21d22fe6fd0f4c0bf6fb.oZ4IE9PPDOhsLKPp")
|
||||||
|
conversation_history.append({"role": "user", "content": messages})
|
||||||
|
# conversation_history.extend(messages)
|
||||||
response = client.chat.completions.create(
|
response = client.chat.completions.create(
|
||||||
model="glm-3-turbo",
|
model="glm-3-turbo",
|
||||||
# messages=[
|
# messages=[
|
||||||
@@ -19,7 +24,7 @@ async def gml_chat(messages):
|
|||||||
# "content": prompt
|
# "content": prompt
|
||||||
# }
|
# }
|
||||||
# ],
|
# ],
|
||||||
messages = messages,
|
messages = conversation_history,
|
||||||
top_p=0.7,
|
top_p=0.7,
|
||||||
temperature=0.95,
|
temperature=0.95,
|
||||||
max_tokens=1024,
|
max_tokens=1024,
|
||||||
@@ -29,10 +34,10 @@ async def gml_chat(messages):
|
|||||||
for trunk in response:
|
for trunk in response:
|
||||||
# print(trunk.choices[0].delta)
|
# print(trunk.choices[0].delta)
|
||||||
content = content + trunk.choices[0].delta.content
|
content = content + trunk.choices[0].delta.content
|
||||||
|
conversation_history.append({"role": "assistant", "content": content})
|
||||||
robot_message = json.dumps(
|
robot_message = json.dumps(
|
||||||
{"type": "user", "content": content, "from": "机器人"})
|
{"type": "user", "content": content, "from": "机器人"})
|
||||||
await asyncio.wait([user.send(robot_message) for user in USERS.values()])
|
await asyncio.wait([user.send(robot_message) for user in USERS.values()])
|
||||||
|
|
||||||
return content
|
return content
|
||||||
# print(gml_chat("你好"))
|
# print(gml_chat("你好"))
|
||||||
|
|
||||||
@@ -62,7 +67,7 @@ async def chat(websocket, path):
|
|||||||
message = json.dumps(
|
message = json.dumps(
|
||||||
{"type": "user", "content": data["content"], "from": name})
|
{"type": "user", "content": data["content"], "from": name})
|
||||||
await asyncio.wait([user.send(message) for user in USERS.values()])
|
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()])
|
# await asyncio.wait([user.send(message) for user in USERS.values()])
|
||||||
# robot_message = json.dumps(
|
# robot_message = json.dumps(
|
||||||
# {"type": "user", "content": robot_reply, "from": "机器人"})
|
# {"type": "user", "content": robot_reply, "from": "机器人"})
|
||||||
|
|||||||
Reference in New Issue
Block a user