From 987811437602e57e87561053e4a03daac161e7c7 Mon Sep 17 00:00:00 2001 From: Asankilp Date: Mon, 17 Mar 2025 05:25:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=B8=E8=B5=9E=E5=90=8D?= =?UTF-8?q?=E5=8D=95=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_plugin_marshoai/handler.py | 2 +- nonebot_plugin_marshoai/marsho.py | 2 +- nonebot_plugin_marshoai/util.py | 16 ++++++++-------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/nonebot_plugin_marshoai/handler.py b/nonebot_plugin_marshoai/handler.py index bd80baf..4949785 100644 --- a/nonebot_plugin_marshoai/handler.py +++ b/nonebot_plugin_marshoai/handler.py @@ -103,7 +103,7 @@ class MarshoHandler: 处理单条聊天 """ - context_msg = get_prompt(model_name) + ( + context_msg = await get_prompt(model_name) + ( self.context.build(self.target.id, self.target.private) ) response = await make_chat_openai( diff --git a/nonebot_plugin_marshoai/marsho.py b/nonebot_plugin_marshoai/marsho.py index 47c70aa..1944b25 100644 --- a/nonebot_plugin_marshoai/marsho.py +++ b/nonebot_plugin_marshoai/marsho.py @@ -288,7 +288,7 @@ with contextlib.suppress(ImportError): # 优化先不做() response = await make_chat_openai( client=client, model_name=model_name, - msg=get_prompt(model_name) + msg=await get_prompt(model_name) + [ UserMessage( content=f"*{user_nickname}{config.marshoai_poke_suffix}" diff --git a/nonebot_plugin_marshoai/util.py b/nonebot_plugin_marshoai/util.py index 7efa812..4f3d7ac 100755 --- a/nonebot_plugin_marshoai/util.py +++ b/nonebot_plugin_marshoai/util.py @@ -136,15 +136,15 @@ async def make_chat_openai( @from_cache("praises") -def get_praises(): +async def get_praises(): praises_file = store.get_plugin_data_file( "praises.json" ) # 夸赞名单文件使用localstore存储 if not praises_file.exists(): - with open(praises_file, "w", encoding="utf-8") as f: + async with aiofiles.open(praises_file, "w", encoding="utf-8") as f: json.dump(_praises_init_data, f, ensure_ascii=False, indent=4) - with open(praises_file, "r", encoding="utf-8") as f: - data = json.load(f) + async with aiofiles.open(praises_file, "r", encoding="utf-8") as f: + data = json.loads(await f.read()) praises_json = data return praises_json @@ -160,8 +160,8 @@ async def refresh_praises_json(): return data -def build_praises() -> str: - praises = get_praises() +async def build_praises() -> str: + praises = await get_praises() result = ["你喜欢以下几个人物,他们有各自的优点:"] for item in praises["like"]: result.append(f"名字:{item['name']},优点:{item['advantages']}") @@ -237,11 +237,11 @@ async def refresh_nickname_json(): logger.error("刷新 nickname_json 表错误:无法载入 nickname.json 文件") -def get_prompt(model: str) -> List[Dict[str, Any]]: +async def get_prompt(model: str) -> List[Dict[str, Any]]: """获取系统提示词""" prompts = config.marshoai_additional_prompt if config.marshoai_enable_praises: - praises_prompt = build_praises() + praises_prompt = await build_praises() prompts += praises_prompt if config.marshoai_enable_time_prompt: