mirror of https://github.com/Mai-with-u/MaiBot.git
爆改一下
parent
1192614d92
commit
56f16a35d1
|
|
@ -17,6 +17,8 @@ memory_graph.gml
|
||||||
config/bot_config_dev.toml
|
config/bot_config_dev.toml
|
||||||
config/bot_config.toml
|
config/bot_config.toml
|
||||||
config/bot_config.toml.bak
|
config/bot_config.toml.bak
|
||||||
|
config/actions.py
|
||||||
|
config/__init__.py
|
||||||
src/plugins/remote/client_uuid.json
|
src/plugins/remote/client_uuid.json
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,13 @@ def update_config():
|
||||||
template_path = template_dir / "bot_config_template.toml"
|
template_path = template_dir / "bot_config_template.toml"
|
||||||
old_config_path = config_dir / "bot_config.toml"
|
old_config_path = config_dir / "bot_config.toml"
|
||||||
new_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 = {}
|
old_config = {}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
class ResponseAction:
|
class ResponseAction:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.tags = []
|
self.tags = []
|
||||||
|
self.msgs = []
|
||||||
|
|
||||||
def parse_action(self, msg: str, action: str) -> str:
|
def parse_action(self, msg: str, action: str) -> str:
|
||||||
if action in msg:
|
if action in msg:
|
||||||
|
|
@ -18,6 +19,15 @@ class ResponseAction:
|
||||||
# 非str输入直接抛异常
|
# 非str输入直接抛异常
|
||||||
raise TypeError
|
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>`**中给出的标签来执行特定动作,请参考对应部分的描述。
|
||||||
|
注意,标签一定是以“[内容]”形式输出的,你可以在一次响应中执行多个动作。
|
||||||
|
"""
|
||||||
|
|
@ -186,9 +186,17 @@ class ChatBot:
|
||||||
willing_manager.change_reply_willing_not_sent(chat)
|
willing_manager.change_reply_willing_not_sent(chat)
|
||||||
|
|
||||||
# print(f"response: {response}")
|
# 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:
|
if response:
|
||||||
stream_id = message.chat_stream.stream_id
|
stream_id = message.chat_stream.stream_id
|
||||||
|
|
||||||
if global_config.enable_think_flow:
|
if global_config.enable_think_flow:
|
||||||
chat_talking_prompt = ""
|
chat_talking_prompt = ""
|
||||||
if stream_id:
|
if stream_id:
|
||||||
|
|
@ -215,6 +223,11 @@ class ChatBot:
|
||||||
logger.warning("未找到对应的思考消息,可能已超时被移除")
|
logger.warning("未找到对应的思考消息,可能已超时被移除")
|
||||||
return
|
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
|
thinking_start_time = thinking_message.thinking_start_time
|
||||||
message_set = MessageSet(chat, think_id)
|
message_set = MessageSet(chat, think_id)
|
||||||
|
|
|
||||||
|
|
@ -115,7 +115,7 @@ class BotConfig:
|
||||||
# experimental
|
# experimental
|
||||||
enable_friend_chat: bool = False # 是否启用好友聊天
|
enable_friend_chat: bool = False # 是否启用好友聊天
|
||||||
enable_think_flow: bool = False # 是否启用思考流程
|
enable_think_flow: bool = False # 是否启用思考流程
|
||||||
enable_think_flow: bool = False # 是否允许执行动作
|
enable_action_execute: bool = False # 是否允许执行动作
|
||||||
|
|
||||||
|
|
||||||
# 模型配置
|
# 模型配置
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,18 @@ class ResponseGenerator:
|
||||||
# print(f"raw_content: {raw_content}")
|
# print(f"raw_content: {raw_content}")
|
||||||
# print(f"model_response: {model_response}")
|
# 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:
|
if model_response:
|
||||||
logger.info(f"{global_config.BOT_NICKNAME}的回复是:{model_response}")
|
logger.info(f"{global_config.BOT_NICKNAME}的回复是:{model_response}")
|
||||||
model_response = await self._process_response(model_response)
|
model_response = await self._process_response(model_response)
|
||||||
|
|
|
||||||
|
|
@ -168,6 +168,10 @@ class PromptBuilder:
|
||||||
请回复的平淡一些,简短一些,说中文,不要刻意突出自身学科背景,
|
请回复的平淡一些,简短一些,说中文,不要刻意突出自身学科背景,
|
||||||
请注意不要输出多余内容(包括前后缀,冒号和引号,括号,表情等),只输出回复内容。
|
请注意不要输出多余内容(包括前后缀,冒号和引号,括号,表情等),只输出回复内容。
|
||||||
{moderation_prompt}不要输出多余内容(包括前后缀,冒号和引号,括号,表情包,at或 @等 )。"""
|
{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 = ""
|
prompt_check_if_response = ""
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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]: 此标签用于标识认为无需回复的情况,输出此标签会**使得消息不被发送**。",
|
||||||
|
]
|
||||||
Loading…
Reference in New Issue