From 9f9e87eb5cd1bf7809d072921a00359f725a01f8 Mon Sep 17 00:00:00 2001 From: taylor Date: Sun, 13 Oct 2024 20:20:21 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E6=B7=BB=E5=8A=A0=E5=91=BD=E4=BB=A4ai?= =?UTF-8?q?=20model=20list=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- maubot_llmplus/aibot.py | 18 ++++++++++-------- maubot_llmplus/platforms.py | 6 ++++-- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/maubot_llmplus/aibot.py b/maubot_llmplus/aibot.py index 046fbe1..cad42f1 100644 --- a/maubot_llmplus/aibot.py +++ b/maubot_llmplus/aibot.py @@ -27,11 +27,18 @@ class Config(BaseProxyConfig): helper.copy("platforms") helper.copy("additional_prompt") - -class AiBotPlugin(Plugin): +class AbsAiBotPlugin(Plugin): default_username: str user_id: str + def get_bot_name(self) -> str: + return self.config['name'] or \ + self.default_username or \ + self.user_id + + +class AiBotPlugin(AbsAiBotPlugin): + async def start(self) -> None: await super().start() # 加载并更新配置 @@ -69,11 +76,6 @@ class AiBotPlugin(Plugin): 5. 在thread中 """ - def get_bot_name(self, config: BaseProxyConfig): - return config['name'] or \ - self.default_username or \ - self.user_id - async def should_respond(self, event: MessageEvent) -> bool: # 发送者是机器人本身, 返回False if event.sender == self.client.mxid: @@ -94,7 +96,7 @@ class AiBotPlugin(Plugin): return False # 检查是否发送消息中有带上机器人的别名 - if re.search("(^|\\s)(@)?" + self.get_bot_name(self.config) + "([ :,.!?]|$)", event.content.body, re.IGNORECASE): + if re.search("(^|\\s)(@)?" + self.get_bot_name() + "([ :,.!?]|$)", event.content.body, re.IGNORECASE): return True # 当聊天室只有两个人并且其中一个是机器人时 diff --git a/maubot_llmplus/platforms.py b/maubot_llmplus/platforms.py index a118909..55f0ba3 100644 --- a/maubot_llmplus/platforms.py +++ b/maubot_llmplus/platforms.py @@ -8,6 +8,8 @@ from maubot import Plugin from mautrix.types import MessageEvent, EncryptedEvent from mautrix.util.config import BaseProxyConfig +from maubot_llmplus.aibot import AbsAiBotPlugin + """ AI响应对象 """ @@ -60,14 +62,14 @@ class Platform: -async def get_context(plugin: Plugin, platform: Platform, evt: MessageEvent) -> deque: +async def get_context(plugin: AbsAiBotPlugin, platform: Platform, evt: MessageEvent) -> deque: # 创建系统提示词上下文 system_context = deque() # 生成当前时间 timestamp = datetime.today().strftime('%Y-%m-%d %H:%M:%S') # 加入系统提示词 system_prompt = {"role": "system", - "content": plugin.config['system_prompt'].format(name=plugin.name, timestamp=timestamp)} + "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.