Merge branch 'dev' of github.com:MaiM-with-u/MaiBot into dev
This commit is contained in:
@@ -502,12 +502,6 @@ class BrainChatting:
|
||||
)
|
||||
return {"action_type": "no_action", "success": True, "reply_text": "", "command": ""}
|
||||
|
||||
elif action_planner_info.action_type == "wait_time":
|
||||
action_planner_info.action_data = action_planner_info.action_data or {}
|
||||
logger.info(f"{self.log_prefix} 等待{action_planner_info.action_data['time']}秒后回复")
|
||||
await asyncio.sleep(action_planner_info.action_data["time"])
|
||||
return {"action_type": "wait_time", "success": True, "reply_text": "", "command": ""}
|
||||
|
||||
elif action_planner_info.action_type == "reply":
|
||||
try:
|
||||
success, llm_response = await generator_api.generate_reply(
|
||||
|
||||
@@ -59,7 +59,7 @@ reply
|
||||
|
||||
no_reply
|
||||
动作描述:
|
||||
保持沉默,等待对方发言
|
||||
等待,保持沉默,等待对方发言
|
||||
{{
|
||||
"action": "no_reply",
|
||||
}}
|
||||
@@ -332,7 +332,7 @@ class BrainPlanner:
|
||||
moderation_prompt=moderation_prompt_block,
|
||||
name_block=name_block,
|
||||
interest=interest,
|
||||
plan_style=global_config.personality.plan_style,
|
||||
plan_style=global_config.personality.private_plan_style,
|
||||
)
|
||||
|
||||
return prompt, message_id_list
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
from typing import Optional
|
||||
import hashlib
|
||||
|
||||
|
||||
def parse_stream_config_to_chat_id(stream_config_str: str) -> Optional[str]:
|
||||
"""
|
||||
解析流配置字符串并生成对应的 chat_id
|
||||
|
||||
Args:
|
||||
stream_config_str: 格式为 "platform:id:type" 的字符串
|
||||
|
||||
Returns:
|
||||
str: 生成的 chat_id,如果解析失败则返回 None
|
||||
"""
|
||||
try:
|
||||
parts = stream_config_str.split(":")
|
||||
if len(parts) != 3:
|
||||
return None
|
||||
|
||||
platform = parts[0]
|
||||
id_str = parts[1]
|
||||
stream_type = parts[2]
|
||||
|
||||
# 判断是否为群聊
|
||||
is_group = stream_type == "group"
|
||||
|
||||
# 使用与 ChatStream.get_stream_id 相同的逻辑生成 chat_id
|
||||
|
||||
if is_group:
|
||||
components = [platform, str(id_str)]
|
||||
else:
|
||||
components = [platform, str(id_str), "private"]
|
||||
key = "_".join(components)
|
||||
return hashlib.md5(key.encode()).hexdigest()
|
||||
|
||||
except (ValueError, IndexError):
|
||||
return None
|
||||
@@ -18,6 +18,7 @@ from src.chat.planner_actions.action_manager import ActionManager
|
||||
from src.chat.heart_flow.hfc_utils import CycleDetail
|
||||
from src.chat.heart_flow.hfc_utils import send_typing, stop_typing
|
||||
from src.chat.express.expression_learner import expression_learner_manager
|
||||
from src.chat.frequency_control.frequency_control import frequency_control_manager
|
||||
from src.person_info.person_info import Person
|
||||
from src.plugin_system.base.component_types import EventType, ActionInfo
|
||||
from src.plugin_system.core import events_manager
|
||||
@@ -202,7 +203,7 @@ class HeartFChatting:
|
||||
# *控制频率用
|
||||
if mentioned_message:
|
||||
await self._observe(recent_messages_list=recent_messages_list, force_reply_message=mentioned_message)
|
||||
elif random.random() < global_config.chat.talk_value:
|
||||
elif random.random() < global_config.chat.talk_value * frequency_control_manager.get_or_create_frequency_control(self.stream_id).get_talk_frequency_adjust():
|
||||
await self._observe(recent_messages_list=recent_messages_list)
|
||||
else:
|
||||
return True
|
||||
|
||||
@@ -106,8 +106,8 @@ class HeartFCMessageReceiver:
|
||||
message.message_info.platform, # type: ignore
|
||||
replace_bot_name=True,
|
||||
)
|
||||
if not processed_plain_text:
|
||||
print(message)
|
||||
# if not processed_plain_text:
|
||||
# print(message)
|
||||
|
||||
logger.info(f"[{mes_name}]{userinfo.user_nickname}:{processed_plain_text}") # type: ignore
|
||||
|
||||
|
||||
@@ -343,6 +343,7 @@ class ActionPlanner:
|
||||
interest=interest,
|
||||
plan_style=global_config.personality.plan_style,
|
||||
)
|
||||
|
||||
|
||||
return prompt, message_id_list
|
||||
except Exception as e:
|
||||
@@ -450,8 +451,8 @@ class ActionPlanner:
|
||||
# 调用LLM
|
||||
llm_content, (reasoning_content, _, _) = await self.planner_llm.generate_response_async(prompt=prompt)
|
||||
|
||||
# logger.info(f"{self.log_prefix}规划器原始提示词: {prompt}")
|
||||
# logger.info(f"{self.log_prefix}规划器原始响应: {llm_content}")
|
||||
logger.info(f"{self.log_prefix}规划器原始提示词: {prompt}")
|
||||
logger.info(f"{self.log_prefix}规划器原始响应: {llm_content}")
|
||||
|
||||
if global_config.debug.show_prompt:
|
||||
logger.info(f"{self.log_prefix}规划器原始提示词: {prompt}")
|
||||
|
||||
@@ -702,9 +702,9 @@ class DefaultReplyer:
|
||||
self._time_and_run_task(
|
||||
self.build_expression_habits(chat_talking_prompt_short, target), "expression_habits"
|
||||
),
|
||||
self._time_and_run_task(
|
||||
self.build_relation_info(chat_talking_prompt_short, sender, person_list_short), "relation_info"
|
||||
),
|
||||
# self._time_and_run_task(
|
||||
# self.build_relation_info(chat_talking_prompt_short, sender, person_list_short), "relation_info"
|
||||
# ),
|
||||
# self._time_and_run_task(self.build_memory_block(message_list_before_short, target), "memory_block"),
|
||||
self._time_and_run_task(
|
||||
self.build_tool_info(chat_talking_prompt_short, sender, target, enable_tool=enable_tool), "tool_info"
|
||||
@@ -745,7 +745,7 @@ class DefaultReplyer:
|
||||
expression_habits_block, selected_expressions = results_dict["expression_habits"]
|
||||
expression_habits_block: str
|
||||
selected_expressions: List[int]
|
||||
relation_info: str = results_dict["relation_info"]
|
||||
# relation_info: str = results_dict["relation_info"]
|
||||
# memory_block: str = results_dict["memory_block"]
|
||||
tool_info: str = results_dict["tool_info"]
|
||||
prompt_info: str = results_dict["prompt_info"] # 直接使用格式化后的结果
|
||||
@@ -786,7 +786,7 @@ class DefaultReplyer:
|
||||
tool_info_block=tool_info,
|
||||
knowledge_prompt=prompt_info,
|
||||
# memory_block=memory_block,
|
||||
relation_info_block=relation_info,
|
||||
# relation_info_block=relation_info,
|
||||
extra_info_block=extra_info_block,
|
||||
identity=personality_prompt,
|
||||
action_descriptions=actions_info,
|
||||
@@ -806,7 +806,7 @@ class DefaultReplyer:
|
||||
tool_info_block=tool_info,
|
||||
knowledge_prompt=prompt_info,
|
||||
# memory_block=memory_block,
|
||||
relation_info_block=relation_info,
|
||||
# relation_info_block=relation_info,
|
||||
extra_info_block=extra_info_block,
|
||||
identity=personality_prompt,
|
||||
action_descriptions=actions_info,
|
||||
@@ -856,9 +856,9 @@ class DefaultReplyer:
|
||||
)
|
||||
|
||||
# 并行执行2个构建任务
|
||||
(expression_habits_block, _), relation_info, personality_prompt = await asyncio.gather(
|
||||
(expression_habits_block, _), personality_prompt = await asyncio.gather(
|
||||
self.build_expression_habits(chat_talking_prompt_half, target),
|
||||
self.build_relation_info(chat_talking_prompt_half, sender, []),
|
||||
# self.build_relation_info(chat_talking_prompt_half, sender, []),
|
||||
self.build_personality_prompt(),
|
||||
)
|
||||
|
||||
@@ -909,7 +909,7 @@ class DefaultReplyer:
|
||||
return await global_prompt_manager.format_prompt(
|
||||
template_name,
|
||||
expression_habits_block=expression_habits_block,
|
||||
relation_info_block=relation_info,
|
||||
# relation_info_block=relation_info,
|
||||
chat_target=chat_target_1,
|
||||
time_block=time_block,
|
||||
chat_info=chat_talking_prompt_half,
|
||||
|
||||
@@ -216,25 +216,25 @@ class PrivateReplyer:
|
||||
traceback.print_exc()
|
||||
return False, llm_response
|
||||
|
||||
async def build_relation_info(self, chat_content: str, sender: str):
|
||||
if not global_config.relationship.enable_relationship:
|
||||
return ""
|
||||
# async def build_relation_info(self, chat_content: str, sender: str):
|
||||
# if not global_config.relationship.enable_relationship:
|
||||
# return ""
|
||||
|
||||
if not sender:
|
||||
return ""
|
||||
# if not sender:
|
||||
# return ""
|
||||
|
||||
if sender == global_config.bot.nickname:
|
||||
return ""
|
||||
# if sender == global_config.bot.nickname:
|
||||
# return ""
|
||||
|
||||
# 获取用户ID
|
||||
person = Person(person_name=sender)
|
||||
if not is_person_known(person_name=sender):
|
||||
logger.warning(f"未找到用户 {sender} 的ID,跳过信息提取")
|
||||
return f"你完全不认识{sender},不理解ta的相关信息。"
|
||||
# # 获取用户ID
|
||||
# person = Person(person_name=sender)
|
||||
# if not is_person_known(person_name=sender):
|
||||
# logger.warning(f"未找到用户 {sender} 的ID,跳过信息提取")
|
||||
# return f"你完全不认识{sender},不理解ta的相关信息。"
|
||||
|
||||
sender_relation = await person.build_relationship(chat_content)
|
||||
# sender_relation = await person.build_relationship(chat_content)
|
||||
|
||||
return f"{sender_relation}"
|
||||
# return f"{sender_relation}"
|
||||
|
||||
async def build_expression_habits(self, chat_history: str, target: str) -> Tuple[str, List[int]]:
|
||||
# sourcery skip: for-append-to-extend
|
||||
@@ -724,9 +724,9 @@ class PrivateReplyer:
|
||||
)
|
||||
|
||||
# 并行执行2个构建任务
|
||||
(expression_habits_block, _), relation_info, personality_prompt = await asyncio.gather(
|
||||
(expression_habits_block, _), personality_prompt = await asyncio.gather(
|
||||
self.build_expression_habits(chat_talking_prompt_half, target),
|
||||
self.build_relation_info(chat_talking_prompt_half, sender),
|
||||
# self.build_relation_info(chat_talking_prompt_half, sender),
|
||||
self.build_personality_prompt(),
|
||||
)
|
||||
|
||||
@@ -777,7 +777,7 @@ class PrivateReplyer:
|
||||
return await global_prompt_manager.format_prompt(
|
||||
template_name,
|
||||
expression_habits_block=expression_habits_block,
|
||||
relation_info_block=relation_info,
|
||||
# relation_info_block=relation_info,
|
||||
chat_target=chat_target_1,
|
||||
time_block=time_block,
|
||||
chat_info=chat_talking_prompt_half,
|
||||
|
||||
@@ -12,7 +12,7 @@ def init_replyer_prompt():
|
||||
|
||||
|
||||
Prompt(
|
||||
"""{knowledge_prompt}{relation_info_block}{tool_info_block}{extra_info_block}
|
||||
"""{knowledge_prompt}{tool_info_block}{extra_info_block}
|
||||
{expression_habits_block}
|
||||
|
||||
你正在qq群里聊天,下面是群里正在聊的内容:
|
||||
@@ -33,7 +33,7 @@ def init_replyer_prompt():
|
||||
|
||||
|
||||
Prompt(
|
||||
"""{knowledge_prompt}{relation_info_block}{tool_info_block}{extra_info_block}
|
||||
"""{knowledge_prompt}{tool_info_block}{extra_info_block}
|
||||
{expression_habits_block}
|
||||
|
||||
你正在qq群里聊天,下面是群里正在聊的内容:
|
||||
@@ -54,7 +54,7 @@ def init_replyer_prompt():
|
||||
|
||||
|
||||
Prompt(
|
||||
"""{knowledge_prompt}{relation_info_block}{tool_info_block}{extra_info_block}
|
||||
"""{knowledge_prompt}{tool_info_block}{extra_info_block}
|
||||
{expression_habits_block}
|
||||
|
||||
你正在和{sender_name}聊天,这是你们之前聊的内容:
|
||||
@@ -73,7 +73,7 @@ def init_replyer_prompt():
|
||||
|
||||
|
||||
Prompt(
|
||||
"""{knowledge_prompt}{relation_info_block}{tool_info_block}{extra_info_block}
|
||||
"""{knowledge_prompt}{tool_info_block}{extra_info_block}
|
||||
{expression_habits_block}
|
||||
|
||||
你正在和{sender_name}聊天,这是你们之前聊的内容:
|
||||
|
||||
@@ -13,8 +13,6 @@ def init_rewrite_prompt():
|
||||
Prompt(
|
||||
"""
|
||||
{expression_habits_block}
|
||||
{relation_info_block}
|
||||
|
||||
{chat_target}
|
||||
{time_block}
|
||||
{chat_info}
|
||||
|
||||
@@ -52,6 +52,9 @@ class PersonalityConfig(ConfigBase):
|
||||
|
||||
visual_style: str = ""
|
||||
"""图片提示词"""
|
||||
|
||||
private_plan_style: str = ""
|
||||
"""私聊说话规则,行为风格"""
|
||||
|
||||
|
||||
@dataclass
|
||||
|
||||
@@ -420,7 +420,7 @@ class Person:
|
||||
except Exception as e:
|
||||
logger.error(f"同步用户 {self.person_id} 信息到数据库时出错: {e}")
|
||||
|
||||
async def build_relationship(self,chat_content:str = ""):
|
||||
async def build_relationship(self,chat_content:str = "",info_type = ""):
|
||||
if not self.is_known:
|
||||
return ""
|
||||
# 构建points文本
|
||||
@@ -446,8 +446,8 @@ class Person:
|
||||
如果没有相关的分类,请输出<none>"""
|
||||
|
||||
response, _ = await relation_selection_model.generate_response_async(prompt)
|
||||
print(prompt)
|
||||
print(response)
|
||||
# print(prompt)
|
||||
# print(response)
|
||||
category_list = extract_categories_from_response(response)
|
||||
if "none" not in category_list:
|
||||
for category in category_list:
|
||||
@@ -456,7 +456,26 @@ class Person:
|
||||
random_memory_str = "\n".join([get_memory_content_from_memory(memory) for memory in random_memory])
|
||||
points_text = f"有关 {category} 的内容:{random_memory_str}"
|
||||
break
|
||||
elif info_type:
|
||||
prompt = f"""你需要获取用户{self.person_name}的 **{info_type}** 信息。
|
||||
|
||||
现有信息类别列表:
|
||||
{category_list}
|
||||
**要求**:请你根据**{info_type}**,从以下分类中选择一个与**{info_type}**相关的分类,并用<>包裹输出,不要输出其他内容,不要输出引号或[],严格用<>包裹:
|
||||
例如:
|
||||
<分类1><分类2><分类3>......
|
||||
如果没有相关的分类,请输出<none>"""
|
||||
response, _ = await relation_selection_model.generate_response_async(prompt)
|
||||
print(prompt)
|
||||
print(response)
|
||||
category_list = extract_categories_from_response(response)
|
||||
if "none" not in category_list:
|
||||
for category in category_list:
|
||||
random_memory = self.get_random_memory_by_category(category, 3)
|
||||
if random_memory:
|
||||
random_memory_str = "\n".join([get_memory_content_from_memory(memory) for memory in random_memory])
|
||||
points_text = f"有关 {category} 的内容:{random_memory_str}"
|
||||
break
|
||||
else:
|
||||
|
||||
for category in category_list:
|
||||
|
||||
@@ -91,6 +91,8 @@ class ToolExecutor:
|
||||
# 缓存未命中,执行工具调用
|
||||
# 获取可用工具
|
||||
tools = self._get_tool_definitions()
|
||||
|
||||
# print(f"tools: {tools}")
|
||||
|
||||
# 获取当前时间
|
||||
time_now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
|
||||
|
||||
@@ -140,7 +140,7 @@ class EmojiAction(BaseAction):
|
||||
# 存储动作信息
|
||||
await self.store_action_info(
|
||||
action_build_into_prompt=True,
|
||||
action_prompt_display=f"发送了表情包,原因:{reason}",
|
||||
action_prompt_display=f"你发送了表情包,原因:{reason}",
|
||||
action_done=True,
|
||||
)
|
||||
return True, f"成功发送表情包:{emoji_description}"
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
from typing import List, Tuple, Type
|
||||
from typing import List, Tuple, Type, Any
|
||||
|
||||
# 导入新插件系统
|
||||
from src.plugin_system import BasePlugin, register_plugin, ComponentInfo
|
||||
from src.plugin_system.base.config_types import ConfigField
|
||||
from src.person_info.person_info import Person
|
||||
from src.plugin_system.base.base_tool import BaseTool, ToolParamType
|
||||
|
||||
# 导入依赖的系统组件
|
||||
from src.common.logger import get_logger
|
||||
@@ -12,6 +14,42 @@ from src.plugins.built_in.relation.relation import BuildRelationAction
|
||||
logger = get_logger("relation_actions")
|
||||
|
||||
|
||||
|
||||
class GetPersonInfoTool(BaseTool):
|
||||
"""获取用户信息"""
|
||||
|
||||
name = "get_person_info"
|
||||
description = "获取某个人的信息,包括印象,特征点,与用户的关系等等"
|
||||
parameters = [
|
||||
("person_name", ToolParamType.STRING, "需要获取信息的人的名称", True, None),
|
||||
("info_type", ToolParamType.STRING, "需要获取信息的类型", True, None),
|
||||
]
|
||||
|
||||
available_for_llm = True
|
||||
|
||||
async def execute(self, function_args: dict[str, Any]) -> dict[str, Any]:
|
||||
"""执行比较两个数的大小
|
||||
|
||||
Args:
|
||||
function_args: 工具参数
|
||||
|
||||
Returns:
|
||||
dict: 工具执行结果
|
||||
"""
|
||||
person_name: str = function_args.get("person_name") # type: ignore
|
||||
info_type: str = function_args.get("info_type") # type: ignore
|
||||
|
||||
person = Person(person_name=person_name)
|
||||
if not person:
|
||||
return {"content": f"用户 {person_name} 不存在"}
|
||||
if not person.is_known:
|
||||
return {"content": f"不认识用户 {person_name}"}
|
||||
|
||||
relation_str = await person.build_relationship(info_type=info_type)
|
||||
|
||||
return {"content": relation_str}
|
||||
|
||||
|
||||
@register_plugin
|
||||
class RelationActionsPlugin(BasePlugin):
|
||||
"""关系动作插件
|
||||
@@ -54,5 +92,6 @@ class RelationActionsPlugin(BasePlugin):
|
||||
# --- 根据配置注册组件 ---
|
||||
components = []
|
||||
components.append((BuildRelationAction.get_action_info(), BuildRelationAction))
|
||||
components.append((GetPersonInfoTool.get_tool_info(), GetPersonInfoTool))
|
||||
|
||||
return components
|
||||
|
||||
Reference in New Issue
Block a user