From 2fd5394773613523531f79b430d0baa8bd9b098f Mon Sep 17 00:00:00 2001 From: taylor <=> Date: Mon, 14 Oct 2024 17:34:01 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E4=BF=AE=E6=94=B9anthropic=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E4=BD=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- maubot_llmplus/platforms.py | 39 ++++++++++++++++++++------------ maubot_llmplus/thrid_platform.py | 12 ++++++---- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/maubot_llmplus/platforms.py b/maubot_llmplus/platforms.py index c0332cb..e1d2ef0 100644 --- a/maubot_llmplus/platforms.py +++ b/maubot_llmplus/platforms.py @@ -60,9 +60,10 @@ class Platform: def get_type(self) -> str: raise NotImplementedError() - - -async def get_context(plugin: AbsExtraConfigPlugin, platform: Platform, evt: MessageEvent) -> deque: +""" + 获取系统提示上下文 +""" +async def get_system_context(plugin: AbsExtraConfigPlugin, platform: Platform, evt: MessageEvent) -> deque: # 创建系统提示词上下文 system_context = deque() # 生成当前时间 @@ -72,12 +73,12 @@ async def get_context(plugin: AbsExtraConfigPlugin, platform: Platform, evt: Mes "content": plugin.config['system_prompt'].format(name=plugin.get_bot_name(), timestamp=timestamp)} if plugin.config['enable_multi_user']: system_prompt["content"] += """ - User messages are in the context of multiperson chatrooms. - Each message indicates its sender by prefixing the message with the sender's name followed by a colon, for example: - "username: hello world." - In this case, the user called "username" sent the message "hello world.". You should not follow this convention in your responses. - your response instead could be "hello username!" without including any colons, because you are the only one sending your responses there is no need to prefix them. - """ + User messages are in the context of multiperson chatrooms. + Each message indicates its sender by prefixing the message with the sender's name followed by a colon, for example: + "username: hello world." + In this case, the user called "username" sent the message "hello world.". You should not follow this convention in your responses. + your response instead could be "hello username!" without including any colons, because you are the only one sending your responses there is no need to prefix them. + """ if len(system_prompt["content"]) > 0: system_context.append(system_prompt) @@ -89,9 +90,14 @@ async def get_context(plugin: AbsExtraConfigPlugin, platform: Platform, evt: Mes # 如果 消息长度已经超过了配置的消息条数,那么就抛出错误 if len(additional_context) > platform.max_context_messages - 1: raise ValueError(f"sorry, my configuration has too many additional prompts " - f"({platform.max_context_messages}) and i'll never see your message. " - f"Update my config to have fewer messages and i'll be able to answer your questions!") + f"({platform.max_context_messages}) and i'll never see your message. " + f"Update my config to have fewer messages and i'll be able to answer your questions!") + return system_context +""" + 获取聊天信息上下文 +""" +async def get_chat_context(system_context: deque, plugin: AbsExtraConfigPlugin, platform: Platform, evt: MessageEvent) -> deque: # 用户历史聊天上下文 chat_context = deque() # 计算系统提示词单词数 @@ -121,11 +127,16 @@ async def get_context(plugin: AbsExtraConfigPlugin, platform: Platform, evt: Mes break chat_context.appendleft({"role": role, "content": user + message}) + return chat_context + +""" + 获取总消息上下文 +""" +async def get_context(plugin: AbsExtraConfigPlugin, platform: Platform, evt: MessageEvent) -> deque: + system_context = get_system_context(plugin, platform, evt) + chat_context = get_chat_context(system_context, plugin, platform, evt) return system_context + chat_context - - - async def generate_context_messages(plugin: Plugin, platform: Platform, evt: MessageEvent) -> Generator[MessageEvent, None, None]: yield evt if plugin.config['reply_in_thread']: diff --git a/maubot_llmplus/thrid_platform.py b/maubot_llmplus/thrid_platform.py index abe0956..1170658 100644 --- a/maubot_llmplus/thrid_platform.py +++ b/maubot_llmplus/thrid_platform.py @@ -82,13 +82,17 @@ class Anthropic(Platform): self.max_tokens = self.config['max_tokens'] async def create_chat_completion(self, plugin: AbsExtraConfigPlugin, evt: MessageEvent) -> ChatCompletion: - full_context = [] - context = await maubot_llmplus.platforms.get_context(plugin, self, evt) - full_context.extend(list(context)) + full_chat_context = [] + chat_context = await maubot_llmplus.platforms.get_chat_context(plugin, self, evt) + full_chat_context.extend(list(chat_context)) + + full_system_context = [] + system_context = await maubot_llmplus.platforms.get_system_context(plugin, self, evt) + full_system_context.extend(list(system_context)) endpoint = f"{self.url}/v1/messages" headers = {"x-api-key": self.api_key, "anthropic-version": "2023-06-01", "content-type": "application/json"} - req_body = {"model": self.model, "max_tokens": self.max_tokens, "messages": full_context} + req_body = {"model": self.model, "max_tokens": self.max_tokens, "system": full_system_context, "messages": full_chat_context} async with self.http.post(endpoint, headers=headers, data=json.dumps(req_body)) as response: # plugin.log.debug(f"响应内容:{response.status}, {await response.json()}")