From c54f8d6005eebea3f425ac7a3f70a63db295d697 Mon Sep 17 00:00:00 2001 From: xuminglong <494462498@qq.com> Date: Tue, 11 Mar 2025 18:21:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E4=BD=BF=E7=94=A8=20coze?= =?UTF-8?q?=20=E4=BC=9A=E8=AF=9D=E3=80=82=20=E5=8E=86=E5=8F=B2=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E5=9C=A8coze=20=E4=B8=8A=E5=8F=AF=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=8E=86=E5=8F=B2=E6=B6=88=E6=81=AF=E7=9A=84=E8=BD=AE=E6=AC=A1?= =?UTF-8?q?=E3=80=82=20=E5=90=8C=E4=B8=AA=E4=BC=9A=E8=AF=9Did=EF=BC=8Ccoze?= =?UTF-8?q?=20=E4=B8=8A=E8=87=AA=E5=8A=A8=E5=B8=A6=E5=8E=86=E5=8F=B2?= =?UTF-8?q?=E4=B8=8A=E4=B8=8B=E6=96=87=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 会话和当前session关联。一次连接一个会话,断开后,重连是新会话无历史消息。 --- .../core/providers/llm/coze/coze.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/main/xiaozhi-server/core/providers/llm/coze/coze.py b/main/xiaozhi-server/core/providers/llm/coze/coze.py index feb62ea64..b1b14d91d 100644 --- a/main/xiaozhi-server/core/providers/llm/coze/coze.py +++ b/main/xiaozhi-server/core/providers/llm/coze/coze.py @@ -11,19 +11,31 @@ from cozepy import Coze, TokenAuth, Message, ChatStatus, MessageContentType, Cha TAG = __name__ logger = setup_logging() + class LLMProvider(LLMProviderBase): def __init__(self, config): self.personal_access_token = config.get("personal_access_token") self.bot_id = config.get("bot_id") self.user_id = config.get("user_id") + self.session_conversation_map = {} # 存储session_id和conversation_id的映射 def response(self, session_id, dialogue): coze_api_token = self.personal_access_token coze_api_base = COZE_CN_BASE_URL last_msg = next(m for m in reversed(dialogue) if m["role"] == "user") - + coze = Coze(auth=TokenAuth(token=coze_api_token), base_url=coze_api_base) + conversation_id = self.session_conversation_map.get(session_id) + + # 如果没有找到conversation_id,则创建新的对话 + if not conversation_id: + conversation = coze.conversations.create( + messages=[ + ] + ) + conversation_id = conversation.id + self.session_conversation_map[session_id] = conversation_id # 更新映射 for event in coze.chat.stream( bot_id=self.bot_id, @@ -31,6 +43,7 @@ class LLMProvider(LLMProviderBase): additional_messages=[ Message.build_user_question_text(last_msg["content"]), ], + conversation_id=conversation_id, ): if event.event == ChatEventType.CONVERSATION_MESSAGE_DELTA: print(event.message.content, end="", flush=True)