From ce1d2f21ae190b264cec2a587084a70134d36119 Mon Sep 17 00:00:00 2001 From: Rikka-desu <2273379351@qq.com> Date: Sun, 24 Nov 2024 15:00:44 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E2=9C=A8=E6=B7=BB=E5=8A=A0=E4=BA=86at?= =?UTF-8?q?=E6=97=B6=E5=93=8D=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_marshoai/azure.py | 30 ++++++++++++++------- nonebot_plugin_marshoai/config.py | 1 + nonebot_plugin_marshoai/config_example.yaml | 2 ++ 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/nonebot_plugin_marshoai/azure.py b/nonebot_plugin_marshoai/azure.py index 3561e4b..1a981f9 100644 --- a/nonebot_plugin_marshoai/azure.py +++ b/nonebot_plugin_marshoai/azure.py @@ -15,10 +15,11 @@ from azure.ai.inference.models import ( ChatCompletionsToolCall, ) from azure.core.credentials import AzureKeyCredential -from nonebot import on_command, logger +from nonebot import on_command, on_message, logger from nonebot.adapters import Message, Event from nonebot.params import CommandArg from nonebot.permission import SUPERUSER +from nonebot.rule import Rule, to_me from nonebot_plugin_alconna import on_alconna, MsgTarget from nonebot_plugin_alconna.uniseg import UniMessage, UniMsg import nonebot_plugin_localstore as store @@ -29,6 +30,11 @@ from .metadata import metadata from .models import MarshoContext, MarshoTools from .util import * + +async def at_enable(): + return config.marshoai_at + + driver = get_driver() changemodel_cmd = on_command("changemodel", permission=SUPERUSER) @@ -47,6 +53,7 @@ marsho_cmd = on_alconna( ), aliases=config.marshoai_aliases, ) +marsho_at = on_message(rule=to_me()&at_enable) nickname_cmd = on_alconna( Alconna( "nickname", @@ -63,6 +70,7 @@ endpoint = config.marshoai_azure_endpoint client = ChatCompletionsClient(endpoint=endpoint, credential=AzureKeyCredential(token)) target_list = [] # 记录需保存历史上下文的列表 + @driver.on_startup async def _preload_tools(): tools_dir = store.get_plugin_data_dir() / "tools" @@ -71,6 +79,7 @@ async def _preload_tools(): tools.load_tools(Path(__file__).parent / "tools") tools.load_tools(store.get_plugin_data_dir() / "tools") + @add_usermsg_cmd.handle() async def add_usermsg(target: MsgTarget, arg: Message = CommandArg()): if msg := arg.extract_plain_text(): @@ -89,7 +98,7 @@ async def add_assistantmsg(target: MsgTarget, arg: Message = CommandArg()): @praises_cmd.handle() async def praises(): - #await UniMessage(await tools.call("marshoai-weather.get_weather", {"location":"杭州"})).send() + # await UniMessage(await tools.call("marshoai-weather.get_weather", {"location":"杭州"})).send() await praises_cmd.finish(build_praises()) @@ -114,7 +123,7 @@ async def save_context(target: MsgTarget, arg: Message = CommandArg()): @load_context_cmd.handle() async def load_context(target: MsgTarget, arg: Message = CommandArg()): if msg := arg.extract_plain_text(): - await get_backup_context(target.id, target.private) # 为了将当前会话添加到"已恢复过备份"的列表而添加,防止上下文被覆盖(好奇怪QwQ + await get_backup_context(target.id, target.private) # 为了将当前会话添加到"已恢复过备份"的列表而添加,防止上下文被覆盖(好奇怪QwQ context.set_context( await load_context_from_json(msg, "contexts"), target.id, target.private ) @@ -160,9 +169,12 @@ async def refresh_data(): await refresh_data_cmd.finish("已刷新数据") +@marsho_at.handle() @marsho_cmd.handle() async def marsho(target: MsgTarget, event: Event, text: Optional[UniMsg] = None): global target_list + if event.get_message() and not text: + text = event.get_message() if not text: # 发送说明 await UniMessage(metadata.usage + "\n当前使用的模型:" + model_name).send() @@ -175,9 +187,9 @@ async def marsho(target: MsgTarget, event: Event, text: Optional[UniMsg] = None) nickname_prompt = f"\n*此消息的说话者:{user_nickname}*" else: nickname_prompt = "" - #用户名无法获取,暂时注释 - #user_nickname = event.sender.nickname # 未设置昵称时获取用户名 - #nickname_prompt = f"\n*此消息的说话者:{user_nickname}" + # 用户名无法获取,暂时注释 + # user_nickname = event.sender.nickname # 未设置昵称时获取用户名 + # nickname_prompt = f"\n*此消息的说话者:{user_nickname}" if config.marshoai_enable_nickname_tip: await UniMessage( "*你未设置自己的昵称。推荐使用'nickname [昵称]'命令设置昵称来获得个性化(可能)回答。" @@ -242,14 +254,14 @@ async def marsho(target: MsgTarget, event: Event, text: Optional[UniMsg] = None) response = await make_chat( client=client, model_name=model_name, - msg = context_msg + [UserMessage(content=usermsg)] + tool_msg, + msg=context_msg + [UserMessage(content=usermsg)] + tool_msg, tools=tools.get_tools_list() - ) + ) choice = response.choices[0] context.append( UserMessage(content=usermsg).as_dict(), target.id, target.private ) - #context.append(tool_msg, target.id, target.private) + # context.append(tool_msg, target.id, target.private) context.append(choice.message.as_dict(), target.id, target.private) await UniMessage(str(choice.message.content)).send(reply_to=True) except Exception as e: diff --git a/nonebot_plugin_marshoai/config.py b/nonebot_plugin_marshoai/config.py index df7c4e4..65f3289 100644 --- a/nonebot_plugin_marshoai/config.py +++ b/nonebot_plugin_marshoai/config.py @@ -12,6 +12,7 @@ class ConfigModel(BaseModel): marshoai_token: str = "" # marshoai_support_image_models: list = ["gpt-4o","gpt-4o-mini"] marshoai_default_name: str = "marsho" + marshoai_at: bool = False marshoai_aliases: set[str] = { "小棉", } diff --git a/nonebot_plugin_marshoai/config_example.yaml b/nonebot_plugin_marshoai/config_example.yaml index 5716400..27b23e1 100644 --- a/nonebot_plugin_marshoai/config_example.yaml +++ b/nonebot_plugin_marshoai/config_example.yaml @@ -6,6 +6,8 @@ marshoai_default_name: "marsho" # 默认名称,设定为marsho。 marshoai_aliases: - 小棉 +marshoai_at: false # 决定是否开启at响应 + marshoai_default_model: "gpt-4o-mini" # 默认模型,设定为gpt-4o-mini。 # 主提示词,定义了Marsho的性格和行为,包含多语言名字翻译规则和对特定问题的回答约束。 From eabdae04fa3dd4726de3134191826d5fbeaa3371 Mon Sep 17 00:00:00 2001 From: Rikka-desu <2273379351@qq.com> Date: Sun, 24 Nov 2024 15:19:51 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0readme=E9=87=8C=E9=9D=A2?= =?UTF-8?q?=E7=9A=84=E9=85=8D=E7=BD=AE=E9=A1=B9=E4=BB=8B=E7=BB=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2106d7c..26673b8 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,7 @@ _✨ 使用 OpenAI 标准格式 API 的聊天机器人插件 ✨_ | --------------------- | ---------- | ----------- | ----------------- | | MARSHOAI_DEFAULT_NAME | `str` | `marsho` | 调用 Marsho 默认的命令前缀 | | MARSHOAI_ALIASES | `set[str]` | `set{"小棉"}` | 调用 Marsho 的命令别名 | +| MARSHOAI_AT | `bool` | `False` | 决定是否使用at触发 #### AI 调用 From 62f49eb381f6d8aaadcf947f15309f6b01fa82f0 Mon Sep 17 00:00:00 2001 From: Rikka-desu <2273379351@qq.com> Date: Sun, 24 Nov 2024 15:59:06 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86at=E6=97=B6?= =?UTF-8?q?=E8=BE=93=E5=85=A5=E5=91=BD=E4=BB=A4=E4=BC=9A=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E8=81=8A=E5=A4=A9=E4=B9=9F=E8=A7=A6=E5=8F=91=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- nonebot_plugin_marshoai/azure.py | 27 ++++++++++++++++----------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 26673b8..4975fa9 100644 --- a/README.md +++ b/README.md @@ -142,7 +142,7 @@ _✨ 使用 OpenAI 标准格式 API 的聊天机器人插件 ✨_ | --------------------- | ---------- | ----------- | ----------------- | | MARSHOAI_DEFAULT_NAME | `str` | `marsho` | 调用 Marsho 默认的命令前缀 | | MARSHOAI_ALIASES | `set[str]` | `set{"小棉"}` | 调用 Marsho 的命令别名 | -| MARSHOAI_AT | `bool` | `False` | 决定是否使用at触发 +| MARSHOAI_AT | `bool` | `false` | 决定是否使用at触发 #### AI 调用 diff --git a/nonebot_plugin_marshoai/azure.py b/nonebot_plugin_marshoai/azure.py index 1a981f9..f5f0f42 100644 --- a/nonebot_plugin_marshoai/azure.py +++ b/nonebot_plugin_marshoai/azure.py @@ -37,31 +37,36 @@ async def at_enable(): driver = get_driver() -changemodel_cmd = on_command("changemodel", permission=SUPERUSER) -resetmem_cmd = on_command("reset") +changemodel_cmd = on_command("changemodel", permission=SUPERUSER, priority=10, block=True) +resetmem_cmd = on_command("reset", priority=10, block=True) # setprompt_cmd = on_command("prompt",permission=SUPERUSER) -praises_cmd = on_command("praises", permission=SUPERUSER) -add_usermsg_cmd = on_command("usermsg", permission=SUPERUSER) -add_assistantmsg_cmd = on_command("assistantmsg", permission=SUPERUSER) -contexts_cmd = on_command("contexts", permission=SUPERUSER) -save_context_cmd = on_command("savecontext", permission=SUPERUSER) -load_context_cmd = on_command("loadcontext", permission=SUPERUSER) +praises_cmd = on_command("praises", permission=SUPERUSER, priority=10, block=True) +add_usermsg_cmd = on_command("usermsg", permission=SUPERUSER, priority=10, block=True) +add_assistantmsg_cmd = on_command("assistantmsg", permission=SUPERUSER, priority=10, block=True) +contexts_cmd = on_command("contexts", permission=SUPERUSER, priority=10, block=True) +save_context_cmd = on_command("savecontext", permission=SUPERUSER, priority=10, block=True) +load_context_cmd = on_command("loadcontext", permission=SUPERUSER, priority=10, block=True) marsho_cmd = on_alconna( Alconna( config.marshoai_default_name, Args["text?", AllParam], ), aliases=config.marshoai_aliases, + priority=10, + block=True ) -marsho_at = on_message(rule=to_me()&at_enable) +marsho_at = on_message(rule=to_me()&at_enable, priority=11) nickname_cmd = on_alconna( Alconna( "nickname", Args["name?", str], - ) + ), + priority = 10, + block = True ) -refresh_data_cmd = on_command("refresh_data", permission=SUPERUSER) +refresh_data_cmd = on_command("refresh_data", permission=SUPERUSER, priority=10, block=True) +command_start = driver.config.command_start model_name = config.marshoai_default_model context = MarshoContext() tools = MarshoTools() From 677fa98a3f62f318718bf92c62f9668e90c5ddf2 Mon Sep 17 00:00:00 2001 From: Rikka-desu <2273379351@qq.com> Date: Sun, 24 Nov 2024 16:03:38 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E8=BE=93?= =?UTF-8?q?=E5=85=A5marsho=E4=BC=9A=E8=A7=A6=E5=8F=91=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_marshoai/azure.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nonebot_plugin_marshoai/azure.py b/nonebot_plugin_marshoai/azure.py index f5f0f42..f1f948d 100644 --- a/nonebot_plugin_marshoai/azure.py +++ b/nonebot_plugin_marshoai/azure.py @@ -178,8 +178,9 @@ async def refresh_data(): @marsho_cmd.handle() async def marsho(target: MsgTarget, event: Event, text: Optional[UniMsg] = None): global target_list - if event.get_message() and not text: + if event.get_message() and (not text and text == config.marshoai_default_name): text = event.get_message() + print(text) if not text: # 发送说明 await UniMessage(metadata.usage + "\n当前使用的模型:" + model_name).send() From 2b4d8a939a7b49855b02619e28b27fc45413539a Mon Sep 17 00:00:00 2001 From: Rikka-desu <2273379351@qq.com> Date: Sun, 24 Nov 2024 16:11:42 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E9=83=A8?= =?UTF-8?q?=E5=88=86=E6=83=85=E5=86=B5=E4=B8=8Bat=E8=81=8A=E5=A4=A9?= =?UTF-8?q?=E5=87=BA=E7=8E=B0=E9=97=AE=E9=A2=98=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_marshoai/azure.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nonebot_plugin_marshoai/azure.py b/nonebot_plugin_marshoai/azure.py index f1f948d..832e390 100644 --- a/nonebot_plugin_marshoai/azure.py +++ b/nonebot_plugin_marshoai/azure.py @@ -178,9 +178,8 @@ async def refresh_data(): @marsho_cmd.handle() async def marsho(target: MsgTarget, event: Event, text: Optional[UniMsg] = None): global target_list - if event.get_message() and (not text and text == config.marshoai_default_name): + if event.get_message() and (not text or text == config.marshoai_default_name): text = event.get_message() - print(text) if not text: # 发送说明 await UniMessage(metadata.usage + "\n当前使用的模型:" + model_name).send() From 8af65405d5ac5d46e9c270edf2c2ded0df20e5c4 Mon Sep 17 00:00:00 2001 From: Rikka-desu <2273379351@qq.com> Date: Sun, 24 Nov 2024 16:34:42 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=88=91=E4=B9=9F?= =?UTF-8?q?=E5=BF=98=E8=AE=B0=E4=BF=AE=E5=A4=8D=E4=BA=86=E4=BB=80=E4=B9=88?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_marshoai/azure.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nonebot_plugin_marshoai/azure.py b/nonebot_plugin_marshoai/azure.py index 832e390..ed5251b 100644 --- a/nonebot_plugin_marshoai/azure.py +++ b/nonebot_plugin_marshoai/azure.py @@ -178,7 +178,7 @@ async def refresh_data(): @marsho_cmd.handle() async def marsho(target: MsgTarget, event: Event, text: Optional[UniMsg] = None): global target_list - if event.get_message() and (not text or text == config.marshoai_default_name): + if event.get_message().extract_plain_text() and (not text and event.get_message().extract_plain_text() != config.marshoai_default_name): text = event.get_message() if not text: # 发送说明