From 8f4da2989915c0de056c085767ee9ba5f501e055 Mon Sep 17 00:00:00 2001 From: ChensenCHX <2087826155@qq.com> Date: Thu, 27 Mar 2025 16:00:57 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=A7=E7=BB=AD=E7=BB=A7=E7=BB=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +-- config/auto_update.py | 7 +++---- src/plugins/action_executer/__init__.py | 0 .../action_executer/action_executer.py | 14 +++++++++----- src/plugins/chat/bot.py | 11 ++++++----- src/plugins/chat/llm_generator.py | 4 ++-- template/actions_template.py | 19 +++++++++++++------ template/bot_config_template.toml | 1 + 8 files changed, 35 insertions(+), 24 deletions(-) create mode 100644 src/plugins/action_executer/__init__.py diff --git a/.gitignore b/.gitignore index 0cd14149..2215acb4 100644 --- a/.gitignore +++ b/.gitignore @@ -17,9 +17,8 @@ memory_graph.gml config/bot_config_dev.toml config/bot_config.toml config/bot_config.toml.bak -config/actions.py -config/__init__.py src/plugins/remote/client_uuid.json +src/plugins/action_executer/actions.py # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/config/auto_update.py b/config/auto_update.py index 241e97b2..90e9bd16 100644 --- a/config/auto_update.py +++ b/config/auto_update.py @@ -15,13 +15,12 @@ def update_config(): old_config_path = config_dir / "bot_config.toml" new_config_path = config_dir / "bot_config.toml" action_template_path = template_dir / "actions_template.py" - action_config_path = config_dir / "actions.py" + action_config_path = root_dir / "src" / "plugins" / "action_executer" / "actions.py" - # 如果config里没有actions.py就复制一份过来 + # 如果action_executer里没有actions.py就复制一份过来 if not action_config_path.exists(): shutil.copy(action_template_path, action_config_path) - Path('config/__init__.py').touch() - + # 读取旧配置文件 old_config = {} if old_config_path.exists(): diff --git a/src/plugins/action_executer/__init__.py b/src/plugins/action_executer/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/plugins/action_executer/action_executer.py b/src/plugins/action_executer/action_executer.py index 30b5a55d..09c73ecc 100644 --- a/src/plugins/action_executer/action_executer.py +++ b/src/plugins/action_executer/action_executer.py @@ -1,3 +1,6 @@ +from src.common.logger import get_module_logger +logger = get_module_logger("action_executer") + class ResponseAction: def __init__(self): self.tags = [] @@ -5,6 +8,7 @@ class ResponseAction: def parse_action(self, msg: str, action: str) -> str: if action in msg: + logger.info(f"从消息中解析到{action}标签") self.tags.append(action) return msg.replace(action, '') return msg @@ -20,14 +24,14 @@ class ResponseAction: raise TypeError - - -from ....config.actions import usable_action_description +from .actions import usable_action_description extern_prompt = f""" `` -{'\n'.join(usable_action_description)} +{usable_action_description} `` 你可以使用**``**中给出的标签来执行特定动作,请参考对应部分的描述。 注意,标签一定是以“[内容]”形式输出的,你可以在一次响应中执行多个动作。 -""" \ No newline at end of file +""" + +logger.info("成功加载可用动作") \ No newline at end of file diff --git a/src/plugins/chat/bot.py b/src/plugins/chat/bot.py index b5bf4e29..7027a3df 100644 --- a/src/plugins/chat/bot.py +++ b/src/plugins/chat/bot.py @@ -189,10 +189,10 @@ class ChatBot: willing_manager.change_reply_willing_not_sent(chat) # print(f"response: {response}") - response_action = None + response_actions = None if global_config.enable_action_execute: from ..action_executer.action_executer import ResponseAction - from ....config.actions import usable_action + from ..action_executer.actions import usable_action if isinstance(response, ResponseAction): response_actions = response.tags response = response.msgs @@ -227,9 +227,10 @@ class ChatBot: return # 清理掉思考消息后开始做发送前处理 - if global_config.enable_action_execute: - for action in response_action: - await response = usable_action[action](response) + if global_config.enable_action_execute and response_actions: + for action in response_actions: + response = usable_action[action](response) + logger.info(f"正在处理{action}") # 记录开始思考的时间,避免从思考到回复的时间太久 thinking_start_time = thinking_message.thinking_start_time diff --git a/src/plugins/chat/llm_generator.py b/src/plugins/chat/llm_generator.py index ad3a2c78..d3711a06 100644 --- a/src/plugins/chat/llm_generator.py +++ b/src/plugins/chat/llm_generator.py @@ -70,15 +70,15 @@ class ResponseGenerator: if global_config.enable_action_execute: from ..action_executer.action_executer import ResponseAction - from ....config.actions import usable_action + from ..action_executer.actions import usable_action actions = ResponseAction() + logger.info(f"{global_config.BOT_NICKNAME}的原始回复是:{model_response}") for action in usable_action: model_response = actions.parse_action(model_response, action) if not actions.empty(): actions.msgs = await self._process_response(model_response) if actions.msgs: return actions, raw_content - return None, raw_content if model_response: logger.info(f"{global_config.BOT_NICKNAME}的回复是:{model_response}") diff --git a/template/actions_template.py b/template/actions_template.py index b9f373ba..bf0f74d6 100644 --- a/template/actions_template.py +++ b/template/actions_template.py @@ -1,10 +1,17 @@ from typing import Callable import time +from ..chat.config import global_config +from src.common.logger import get_module_logger +logger = get_module_logger("Actions") + # 示例函数 -async def refuse_response(response: list[str]) -> list[str]: +def refuse_response(response: list[str]) -> list[str]: + logger.info(f"{global_config.BOT_NICKNAME}认为不需要进行回复。") return [] -async def ping_response(response: list[str]) -> list[str]: + +def ping_response(response: list[str]) -> list[str]: + logger.info(f"{global_config.BOT_NICKNAME}认为这是测试是否在线。") return [f"Pong! at {time.asctime()}."] # 可用函数表 注意每个函数都应该接收一个list[str](输入的响应), 输出一个list[str](输出的响应) @@ -15,7 +22,7 @@ usable_action: dict[str, Callable[[list[str]], list[str]]] = { "[refuse]" : refuse_response, } -usable_action_description = [ - "[ping]: 此标签**仅**用于用户确认系统是否在线,输出此标签会**导致消息被替换为**`Pong! at %当前时间%`" - "[refuse]: 此标签用于标识认为无需回复的情况,输出此标签会**使得消息不被发送**。", -] \ No newline at end of file +usable_action_description = """ +[ping]: 此标签**仅在用户输入--Ping时**使用,输出此标签会**导致消息被替换为**`Pong! at %当前时间%`; +[refuse]: 此标签用于标识认为无需回复的情况,输出此标签会**使得消息不被发送**; +""" \ No newline at end of file diff --git a/template/bot_config_template.toml b/template/bot_config_template.toml index 172a0387..a700fe47 100644 --- a/template/bot_config_template.toml +++ b/template/bot_config_template.toml @@ -135,6 +135,7 @@ enable = true enable_friend_chat = false # 是否启用好友聊天 enable_think_flow = false # 是否启用思维流 注意:可能会消耗大量token,请谨慎开启 #思维流适合搭配低能耗普通模型使用,例如qwen2.5 32b +enable_action_execute = false # 是否启用动作执行器 #下面的模型若使用硅基流动则不需要更改,使用ds官方则改成.env.prod自定义的宏,使用自定义模型则选择定位相似的模型自己填写 #推理模型