From aa605afc9e451ee1138ab4147573d5fe439da557 Mon Sep 17 00:00:00 2001 From: Asankilp Date: Thu, 3 Oct 2024 11:58:33 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=BEv0.1.4=EF=BC=8C=E5=B0=86=E5=A4=B8?= =?UTF-8?q?=E8=B5=9E=E5=90=8D=E5=8D=95=E6=94=B9=E4=B8=BA=E5=AD=98=E5=82=A8?= =?UTF-8?q?=E8=87=B3=E5=8D=95=E7=8B=AC=E7=9A=84=E6=95=B0=E6=8D=AE=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E8=80=8C=E9=9D=9E=E5=B7=A5=E4=BD=9C=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- nonebot_plugin_marshoai/__init__.py | 13 +++++++--- nonebot_plugin_marshoai/util.py | 39 ++++++++++++++--------------- pyproject.toml | 3 ++- 4 files changed, 32 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index d5f0d4c..0e457c2 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ _✨ 使用 Azure OpenAI 推理服务的聊天机器人插件 ✨_ 发送`marsho`指令可以获取使用说明 ## 👍 夸赞名单 -夸赞名单存储于 Bot 工作目录的`praises.json`下,当配置项为`true`时发起一次聊天后自动生成,包含人物名字与人物优点两个基本数据。 +夸赞名单存储于插件数据目录下的`praises.json`里(该目录路径会在 Bot 启动时输出到日志),当配置项为`true`时发起一次聊天后自动生成,包含人物名字与人物优点两个基本数据。 存储于其中的人物会被 Marsho “认识”和“喜欢”。 其结构类似于: ```json diff --git a/nonebot_plugin_marshoai/__init__.py b/nonebot_plugin_marshoai/__init__.py index c323ca0..3a1afa3 100644 --- a/nonebot_plugin_marshoai/__init__.py +++ b/nonebot_plugin_marshoai/__init__.py @@ -1,8 +1,10 @@ from nonebot.plugin import PluginMetadata, inherit_supported_adapters, require require("nonebot_plugin_alconna") +require("nonebot_plugin_localstore") from .azure import * -from nonebot import get_driver -from .config import ConfigModel +from nonebot import get_driver, logger +from .config import ConfigModel, config +import nonebot_plugin_localstore as store usage = """MarshoAI Alpha by Asankilp 用法: marsho <聊天内容> : 与 Marsho 进行对话。当模型为 GPT-4o(-mini) 等时,可以带上图片进行对话。 @@ -33,5 +35,10 @@ driver = get_driver() @driver.on_startup async def _(): - pass + logger.info("MarshoAI 已经加载~🐾") + logger.info(f"Marsho 的插件数据存储于 : {str(store.get_plugin_data_dir())} 哦~🐾") + if config.marshoai_token == "": + logger.warning("token 未配置。可能无法进行聊天。") + else: + logger.info("token 已配置~!🐾") diff --git a/nonebot_plugin_marshoai/util.py b/nonebot_plugin_marshoai/util.py index c33db3c..0751a18 100644 --- a/nonebot_plugin_marshoai/util.py +++ b/nonebot_plugin_marshoai/util.py @@ -3,6 +3,7 @@ import mimetypes import os import json import httpx +import nonebot_plugin_localstore as store from datetime import datetime from zhDateTime import DateTime from azure.ai.inference.models import SystemMessage @@ -28,16 +29,16 @@ async def get_image_b64(url): return None def get_praises(): - filename = "praises.json" - if not os.path.exists("praises.json"): + praises_file = store.get_plugin_data_file("praises.json") # 夸赞名单文件使用localstore存储 + if not os.path.exists(praises_file): init_data = { "like": [ {"name":"Asankilp","advantages":"赋予了Marsho猫娘人格,使用vim与vscode为Marsho写了许多代码,使Marsho更加可爱"} ] } - with open(filename,"w",encoding="utf-8") as f: + with open(praises_file,"w",encoding="utf-8") as f: json.dump(init_data,f,ensure_ascii=False,indent=4) - with open(filename,"r",encoding="utf-8") as f: + with open(praises_file,"r",encoding="utf-8") as f: data = json.load(f) return data @@ -63,19 +64,17 @@ def get_prompt(): spell = SystemMessage(content=marsho_prompt+prompts) return spell -def suggest_solution(errinfo: str): - suggestion = "" - if "content_filter" in errinfo: - suggestion = "消息已被内容过滤器过滤。请调整聊天内容后重试。" - elif "RateLimitReached" in errinfo: - suggestion = "模型达到调用速率限制。请稍等一段时间或联系Bot管理员。" - elif "tokens_limit_reached" in errinfo: - suggestion = "请求token达到上限。请重置上下文。" - elif "content_length_limit" in errinfo: - suggestion = "请求体过大。请重置上下文。" - elif "unauthorized" in errinfo: - suggestion = "Azure凭据无效。请联系Bot管理员。" - if suggestion != "": - return "\n"+suggestion - else: - return suggestion \ No newline at end of file +def suggest_solution(errinfo: str) -> str: + suggestions = { + "content_filter": "消息已被内容过滤器过滤。请调整聊天内容后重试。", + "RateLimitReached": "模型达到调用速率限制。请稍等一段时间或联系Bot管理员。", + "tokens_limit_reached": "请求token达到上限。请重置上下文。", + "content_length_limit": "请求体过大。请重置上下文。", + "unauthorized": "Azure凭据无效。请联系Bot管理员。" + } + + for key, suggestion in suggestions.items(): + if key in errinfo: + return f"\n{suggestion}" + + return "" \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index e2e15ad..2935916 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "nonebot-plugin-marshoai" -version = "0.1.3" +version = "0.1.4" description = "Nonebot2插件,调用Azure OpenAI服务实现猫娘聊天" readme = "README.md" requires-python = "<4.0,>=3.9" @@ -8,6 +8,7 @@ authors = [{ name = "Asankilp", email = "asankilp@outlook.com" }] dependencies = [ "nonebot2>=2.2.0", "nonebot-plugin-alconna>=0.48.0", + "nonebot-plugin-localstore>=0.7.1", "azure-ai-inference>=1.0.0b4", "zhDatetime>=1.1.1", "aiohttp>=3.9",