From 569fc6f985f2c3ab57976cd0c8a045d81f03342d Mon Sep 17 00:00:00 2001 From: xiaoan <419177984@qq.com> Date: Mon, 20 May 2024 20:30:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=9C=BA=E5=99=A8=E4=BA=BA?= =?UTF-8?q?=E8=81=94=E7=B3=BB=E4=B8=8A=E4=B8=8B=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wechat/wechat_test.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/wechat/wechat_test.py b/wechat/wechat_test.py index c2e7ba7..0e544b4 100644 --- a/wechat/wechat_test.py +++ b/wechat/wechat_test.py @@ -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": "机器人"})