爆改一下

pull/601/head
ChensenCHX 2025-03-27 13:21:39 +08:00
parent a6469f6cc7
commit a9179856eb
8 changed files with 74 additions and 5 deletions

2
.gitignore vendored
View File

@ -17,6 +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
# Byte-compiled / optimized / DLL files
__pycache__/

View File

@ -14,6 +14,13 @@ def update_config():
template_path = template_dir / "bot_config_template.toml"
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"
# 如果config里没有actions.py就复制一份过来
if not action_config_path.exists():
shutil.copy(action_template_path, action_config_path)
Path('config/__init__.py').touch()
# 读取旧配置文件
old_config = {}

View File

@ -1,6 +1,7 @@
class ResponseAction:
def __init__(self):
self.tags = []
self.msgs = []
def parse_action(self, msg: str, action: str) -> str:
if action in msg:
@ -18,6 +19,15 @@ class ResponseAction:
# 非str输入直接抛异常
raise TypeError
def __bool__(self):
# 这是为了直接嵌入到llm_generator.py的处理流里 便于跳过消息处理流程
return False
from ....config.actions import usable_action_description
extern_prompt = f"""
`<UseableAction>`
{'\n'.join(usable_action_description)}
`</UseableAction>`
你可以使用**`<UseableAction>`**中给出的标签来执行特定动作请参考对应部分的描述
注意标签一定是以[内容]形式输出的你可以在一次响应中执行多个动作
"""

View File

@ -191,9 +191,17 @@ class ChatBot:
willing_manager.change_reply_willing_not_sent(chat)
# print(f"response: {response}")
response_action = None
if global_config.enable_action_execute:
from ..action_executer.action_executer import ResponseAction
from ....config.actions import usable_action
if isinstance(response, ResponseAction):
response_actions = response.tags
response = response.msgs
if response:
stream_id = message.chat_stream.stream_id
if global_config.enable_think_flow:
chat_talking_prompt = ""
if stream_id:
@ -220,6 +228,11 @@ class ChatBot:
logger.warning("未找到对应的思考消息,可能已超时被移除")
return
# 清理掉思考消息后开始做发送前处理
if global_config.enable_action_execute:
for action in response_action:
await response = usable_action[action](response)
# 记录开始思考的时间,避免从思考到回复的时间太久
thinking_start_time = thinking_message.thinking_start_time
message_set = MessageSet(chat, think_id)

View File

@ -68,6 +68,18 @@ class ResponseGenerator:
# print(f"raw_content: {raw_content}")
# print(f"model_response: {model_response}")
if global_config.enable_action_execute:
from ..action_executer.action_executer import ResponseAction
from ....config.actions import usable_action
actions = ResponseAction()
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}")
model_response = await self._process_response(model_response)

View File

@ -174,6 +174,10 @@ class PromptBuilder:
请回复的平淡一些简短一些说中文不要刻意突出自身学科背景
请注意不要输出多余内容(包括前后缀冒号和引号括号表情等)只输出回复内容
{moderation_prompt}不要输出多余内容(包括前后缀冒号和引号括号表情包at或 @等 )"""
if global_config.enable_action_execute:
from ..action_executer.action_executer import extern_prompt
prompt = prompt + extern_prompt
prompt_check_if_response = ""

View File

@ -115,7 +115,7 @@ class BotConfig:
# experimental
enable_friend_chat: bool = False # 是否启用好友聊天
enable_think_flow: bool = False # 是否启用思考流程
enable_think_flow: bool = False # 是否允许执行动作
enable_action_execute: bool = False # 是否允许执行动作
# 模型配置

View File

@ -0,0 +1,21 @@
from typing import Callable
import time
# 示例函数
async def refuse_response(response: list[str]) -> list[str]:
return []
async def ping_response(response: list[str]) -> list[str]:
return [f"Pong! at {time.asctime()}."]
# 可用函数表 注意每个函数都应该接收一个list[str](输入的响应), 输出一个list[str](输出的响应)
# 显然 你可以在函数里做各种操作来修改响应做出其他动作etc
# 注意到MaiMBot基于Python 3.9, 所以这里的注册顺序实际上决定了tag的执行顺序越上方的越靠前
usable_action: dict[str, Callable[[list[str]], list[str]]] = {
"[ping]" : ping_response,
"[refuse]" : refuse_response,
}
usable_action_description = [
"[ping]: 此标签**仅**用于用户确认系统是否在线,输出此标签会**导致消息被替换为**`Pong! at %当前时间%`"
"[refuse]: 此标签用于标识认为无需回复的情况,输出此标签会**使得消息不被发送**。",
]