fix:微调了关系参

This commit is contained in:
SengokuCola
2025-06-09 19:15:39 +08:00
parent 86c3d4be1e
commit ad478a88b7
4 changed files with 151 additions and 17 deletions

1
.gitignore vendored
View File

@@ -315,3 +315,4 @@ run_pet.bat
!/src/plugins/tts_plugin/
!/src/plugins/vtb_action/
!/src/plugins/__init__.py
!/src/plugins/example_commands/

View File

@@ -146,10 +146,18 @@ class RelationshipProcessor(BaseProcessor):
time_elapsed = current_time - record["start_time"]
message_count = len(get_raw_msg_by_timestamp_with_chat(self.subheartflow_id, record["start_time"], current_time))
if (record["rounds"] > 50 or
time_elapsed > 1800 or # 30分钟
message_count > 75):
logger.info(f"{self.log_prefix} 用户 {record['person_id']} 满足关系构建条件,开始构建关系。")
print(record)
# 根据消息数量和时间设置不同的触发条件
should_trigger = (
message_count >= 50 or # 50条消息必定满足
(message_count >= 35 and time_elapsed >= 600) or # 35条且10分钟
(message_count >= 25 and time_elapsed >= 1800) or # 25条且30分钟
(message_count >= 10 and time_elapsed >= 3600) # 10条且1小时
)
if should_trigger:
logger.info(f"{self.log_prefix} 用户 {record['person_id']} 满足关系构建条件,开始构建关系。消息数:{message_count},时长:{time_elapsed:.0f}")
asyncio.create_task(
self.update_impression_on_cache_expiry(
record["person_id"],
@@ -228,11 +236,14 @@ class RelationshipProcessor(BaseProcessor):
logger.info(f"{self.log_prefix} 调取用户 {person_name}{info_type} 信息。")
self.person_engaged_cache.append({
"person_id": person_id,
"start_time": time.time(),
"rounds": 0
})
# 检查person_engaged_cache中是否已存在该person_id
person_exists = any(record["person_id"] == person_id for record in self.person_engaged_cache)
if not person_exists:
self.person_engaged_cache.append({
"person_id": person_id,
"start_time": time.time(),
"rounds": 0
})
asyncio.create_task(self.fetch_person_info(person_id, [info_type], start_time=time.time()))
else:

View File

@@ -240,7 +240,7 @@ class RelationshipManager:
prompt = f"""
你的名字是{global_config.bot.nickname}{global_config.bot.nickname}的别名是{alias_str}
请不要混淆你自己和{global_config.bot.nickname}{person_name}
请你基于用户 {person_name}(昵称:{nickname}) 的最近发言,总结出其中是否有有关{person_name}的内容引起了你的兴趣,或者有什么需要你记忆的点。
请你基于用户 {person_name}(昵称:{nickname}) 的最近发言,总结出其中是否有有关{person_name}的内容引起了你的兴趣,或者有什么需要你记忆的点,或者对你友好或者不友好的点
如果没有就输出none
{current_time}的聊天内容:
@@ -359,7 +359,7 @@ class RelationshipManager:
else:
current_points = points_list
# 如果points超过30条按权重随机选择多余的条目移动到forgotten_points
# 如果points超过10条按权重随机选择多余的条目移动到forgotten_points
if len(current_points) > 10:
# 获取现有forgotten_points
forgotten_points = await person_info_manager.get_value(person_id, "forgotten_points") or []
@@ -394,7 +394,7 @@ class RelationshipManager:
# 计算保留概率(权重越高越可能保留)
keep_probability = weight / total_weight
if len(remaining_points) < 30:
if len(remaining_points) < 10:
# 如果还没达到30条直接保留
remaining_points.append(point)
else:
@@ -412,7 +412,7 @@ class RelationshipManager:
current_points = remaining_points
forgotten_points.extend(points_to_move)
# 检查forgotten_points是否达到100
# 检查forgotten_points是否达到5
if len(forgotten_points) >= 5:
# 构建压缩总结提示词
alias_str = ", ".join(global_config.bot.alias_names)
@@ -433,19 +433,26 @@ class RelationshipManager:
你的名字是{global_config.bot.nickname}{global_config.bot.nickname}的别名是{alias_str}
请不要混淆你自己和{global_config.bot.nickname}{person_name}
请根据以下历史记录,添加,修改,整合,原有的印象和关系,总结出对用户 {person_name}(昵称:{nickname})的信息
请根据你对ta过去的了解和ta最近的行为,修改,整合,原有的了解,总结出对用户 {person_name}(昵称:{nickname})新的了解
你之前对他的印象和关系是:
印象impression{impression}
了解可以包含性格关系感受态度你推测的ta的性别年龄外貌身份习惯爱好重要事件重要经历等等内容。也可以包含其他点。
关注友好和不友好的因素,不要忽略。
请严格按照以下给出的信息,不要新增额外内容。
你之前对他的了解是:
{impression}
你记得ta最近做的事
{points_text}
请输出impression:对这个人的总体印象你对ta的感觉你们的交互方式对方的性格特点身份外貌年龄性别习惯爱好等等内容
请输出一段平文本,以陈诉自白的语气,输出你对{person_name}的了解,不要输出任何其他内容
"""
# 调用LLM生成压缩总结
compressed_summary, _ = await self.relationship_llm.generate_response_async(prompt=compress_prompt)
current_time = datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")
compressed_summary = f"截至{current_time},你对{person_name}的了解:{compressed_summary}"
await person_info_manager.update_one_field(person_id, "impression", compressed_summary)

View File

@@ -0,0 +1,115 @@
from src.common.logger_manager import get_logger
from src.chat.message_receive.command_handler import BaseCommand, register_command, _COMMAND_REGISTRY
from typing import Tuple, Optional
logger = get_logger("help_command")
@register_command
class HelpCommand(BaseCommand):
"""帮助命令,显示所有可用命令的帮助信息"""
command_name = "help"
command_description = "显示所有可用命令的帮助信息"
command_pattern = r"^/help(?:\s+(?P<command>\w+))?$" # 匹配 /help 或 /help 命令名
command_help = "使用方法: /help [命令名] - 显示所有命令或特定命令的帮助信息"
command_examples = ["/help", "/help echo"]
enable_command = True
async def execute(self) -> Tuple[bool, Optional[str]]:
"""执行帮助命令
Returns:
Tuple[bool, Optional[str]]: (是否执行成功, 回复消息)
"""
try:
# 获取匹配到的命令名(如果有)
command_name = self.matched_groups.get("command")
# 如果指定了命令名,显示该命令的详细帮助
if command_name:
logger.info(f"{self.log_prefix} 查询命令帮助: {command_name}")
return self._show_command_help(command_name)
# 否则,显示所有命令的简要帮助
logger.info(f"{self.log_prefix} 查询所有命令帮助")
return self._show_all_commands()
except Exception as e:
logger.error(f"{self.log_prefix} 执行帮助命令时出错: {e}")
return False, f"执行命令时出错: {str(e)}"
def _show_command_help(self, command_name: str) -> Tuple[bool, str]:
"""显示特定命令的详细帮助信息
Args:
command_name: 命令名称
Returns:
Tuple[bool, str]: (是否执行成功, 回复消息)
"""
# 查找命令
command_cls = _COMMAND_REGISTRY.get(command_name)
if not command_cls:
return False, f"未找到命令: {command_name}"
# 获取命令信息
description = getattr(command_cls, "command_description", "无描述")
help_text = getattr(command_cls, "command_help", "无帮助信息")
examples = getattr(command_cls, "command_examples", [])
# 构建帮助信息
help_info = [
f"【命令】: {command_name}",
f"【描述】: {description}",
f"【用法】: {help_text}"
]
# 添加示例
if examples:
help_info.append("【示例】:")
for example in examples:
help_info.append(f" {example}")
return True, "\n".join(help_info)
def _show_all_commands(self) -> Tuple[bool, str]:
"""显示所有可用命令的简要帮助信息
Returns:
Tuple[bool, str]: (是否执行成功, 回复消息)
"""
# 获取所有已启用的命令
enabled_commands = {
name: cls for name, cls in _COMMAND_REGISTRY.items()
if getattr(cls, "enable_command", True)
}
if not enabled_commands:
return True, "当前没有可用的命令"
# 构建命令列表
command_list = ["可用命令列表:"]
for name, cls in sorted(enabled_commands.items()):
description = getattr(cls, "command_description", "无描述")
# 获取命令前缀示例
examples = getattr(cls, "command_examples", [])
prefix = ""
if examples and len(examples) > 0:
# 从第一个示例中提取前缀
example = examples[0]
# 找到第一个空格前的内容作为前缀
space_pos = example.find(" ")
if space_pos > 0:
prefix = example[:space_pos]
else:
prefix = example
else:
# 默认使用/name作为前缀
prefix = f"/{name}"
command_list.append(f"{prefix} - {description}")
command_list.append("\n使用 /help <命令名> 获取特定命令的详细帮助")
return True, "\n".join(command_list)