From 7dde27d289d25413de3f9fadcfcb9d800608da6e Mon Sep 17 00:00:00 2001 From: Asankilp Date: Fri, 25 Oct 2024 13:40:58 +0800 Subject: [PATCH] =?UTF-8?q?v0.3.4=EF=BC=8C=E5=AE=9E=E7=8E=B0=E4=B8=8A?= =?UTF-8?q?=E4=B8=8B=E6=96=87=E4=B8=8E=E6=B7=BB=E5=8A=A0=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=EF=BC=8C=E8=BE=93=E5=87=BA=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 21 +++++++++++++++++++++ marshoai/__plugin__.py | 37 ++++++++++++++++++++++++++++++++++--- marshoai/constants.py | 6 +++++- 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a1ab0cf..7b4d94a 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,27 @@ _✨ 使用 Azure OpenAI 推理服务的聊天机器人(施工中) ✨_ #### 👉 戳一戳 当 melobot 连接到支持的 OneBot v11 实现端时,可以接收头像双击戳一戳消息并进行响应。详见`MARSHOAI_POKE_SUFFIX`配置项。 + +## 👍 夸赞名单 +夸赞名单存储于插件数据目录下的`praises.json`里(该目录路径会在 Bot 启动时输出到日志),当配置项为`true`时发起一次聊天后自动生成,包含人物名字与人物优点两个基本数据。 +存储于其中的人物会被 Marsho “认识”和“喜欢”。 +其结构类似于: +```json +{ + "like": [ + { + "name": "Asankilp", + "advantages": "赋予了Marsho猫娘人格,使用vim与vscode为Marsho写了许多代码,使Marsho更加可爱" + }, + { + "name": "律回(aicorein,Melorenae)", + "advantages": "创造了Melobot(同样是可爱的猫娘),Marsho与她和Melobot亲密接触" + }, + ... + ] +} +``` + ## ⚙️ 配置 在 `bot.py` 所在目录的`.env`文件中添加下表中的配置 diff --git a/marshoai/__plugin__.py b/marshoai/__plugin__.py index d6ae827..daafe25 100644 --- a/marshoai/__plugin__.py +++ b/marshoai/__plugin__.py @@ -1,7 +1,8 @@ import traceback from azure.ai.inference.aio import ChatCompletionsClient -from azure.ai.inference.models import UserMessage, TextContentItem, ImageContentItem, ImageUrl, CompletionsFinishReason +from azure.ai.inference.models import UserMessage, AssistantMessage, TextContentItem, ImageContentItem, ImageUrl, CompletionsFinishReason from melobot import Plugin, send_text +from melobot.log import get_logger from melobot.protocols.onebot.v11 import on_start_match, on_message, on_command, on_notice, on_event, Adapter from melobot.protocols.onebot.v11.handle import Args from melobot.protocols.onebot.v11.utils import MsgChecker, LevelRole, MsgCheckerFactory, StartMatcher, ParseArgs, Parser @@ -25,11 +26,41 @@ client = ChatCompletionsClient( endpoint=endpoint, credential=AzureKeyCredential(token) ) +logger = get_logger() + +logger.info(f"Marsho 的插件数据存储于 : {str(store.get_plugin_data_dir())} 哦~🐾") +if config.marshoai_token == "": + logger.warning("token 未配置。可能无法进行聊天。") +else: + logger.info("token 已配置~!🐾") +logger.info("マルショは、高性能ですから!") + +@on_command(checker=superuser_checker, cmd_start="/", cmd_sep=" ", targets="usermsg") +async def add_usermsg(event: MessageEvent, args: ParseArgs = Args()): + context.append(UserMessage(content=" ".join(args.vals)).as_dict(), get_target_id(event), event.is_private) + await send_text("已添加用户消息") + +@on_command(checker=superuser_checker, cmd_start="/", cmd_sep=" ", targets="assistantmsg") +async def add_assistantmsg(event: MessageEvent, args: ParseArgs = Args()): + context.append(AssistantMessage(content=" ".join(args.vals)).as_dict(), get_target_id(event), event.is_private) + await send_text("已添加助手消息") @on_command(checker=superuser_checker, cmd_start="/", cmd_sep=" ", targets="praises") async def praises(): await send_text(build_praises()) +@on_command(checker=superuser_checker, cmd_start="/", cmd_sep=" ", targets="savecontext") +async def save_context(event: MessageEvent, args: ParseArgs = Args()): + contexts = context.build(get_target_id(event), event.is_private)[1:] + await save_context_to_json(" ".join(args.vals), contexts) + await send_text("已保存上下文") + +@on_command(checker=superuser_checker, cmd_start="/", cmd_sep=" ", targets="loadcontext") +async def load_context(event: MessageEvent, args: ParseArgs = Args()): + context.set_context(await load_context_from_json(" ".join(args.vals)), get_target_id(event), event.is_private) + await send_text("已加载并覆盖上下文") + + @on_command(checker=superuser_checker, cmd_start="/", cmd_sep=" ", targets="changemodel") async def changemodel(args: ParseArgs = Args()): global model_name @@ -66,7 +97,7 @@ async def marsho(event: Union[GroupMessageEvent, PrivateMessageEvent]): async def marsho_main(event: Union[GroupMessageEvent, PrivateMessageEvent], is_group: bool): if event.text.lstrip("marsho") == "": - await send_text(USAGE) + await send_text(USAGE+"\n当前使用的模型:"+model_name) await send_text(INTRODUCTION) await send_text(str(store.get_plugin_data_dir())) return @@ -148,4 +179,4 @@ async def poke(event: PokeNotifyEvent, adapter: Adapter): # 尚未实现私聊 class MarshoAI(Plugin): version = VERSION - flows = [changemodel,marsho,reset,poke,contexts,praises,nickname] + flows = [changemodel,marsho,reset,poke,contexts,praises,nickname,add_assistantmsg,add_usermsg,load_context,save_context] diff --git a/marshoai/constants.py b/marshoai/constants.py index e20b6f9..1428814 100644 --- a/marshoai/constants.py +++ b/marshoai/constants.py @@ -1,4 +1,4 @@ -__version__ = "0.3.2" +__version__ = "0.3.4" VERSION = __version__ PLUGIN_NAME = "marshoai" USAGE: str = f"""MarshoAI-Melobot Beta v{__version__} by Asankilp @@ -10,6 +10,10 @@ USAGE: str = f"""MarshoAI-Melobot Beta v{__version__} by Asankilp /changemodel <模型名> : 切换全局 AI 模型。 /contexts : 返回当前会话的上下文列表。 ※当上下文包含图片时,不要使用此命令。 /praises : 返回夸赞名单的提示词。 + /usermsg <消息> : 往当前会话添加用户消息(UserMessage)。 + /assistantmsg <消息> : 往当前会话添加助手消息(AssistantMessage)。 + /savecontext <文件名> : 保存当前会话的上下文至插件数据目录下的contexts/<文件名>.json里。 + /loadcontext <文件名> : 从插件数据目录下的contexts/<文件名>.json里读取上下文并覆盖到当前会话。 ※本AI的回答"按原样"提供,不提供任何担保。AI也会犯错,请仔细甄别回答的准确性。""" SUPPORT_IMAGE_MODELS: list = ["gpt-4o","gpt-4o-mini","llama-3.2-90b-vision-instruct","llama-3.2-11b-vision-instruct"]