🤖 自动格式化代码 [skip ci]
This commit is contained in:
@@ -28,7 +28,6 @@ from src.chat.focus_chat.planners.action_manager import ActionManager
|
||||
from src.config.config import global_config
|
||||
from src.chat.focus_chat.hfc_performance_logger import HFCPerformanceLogger
|
||||
from src.chat.focus_chat.hfc_version_manager import get_hfc_version
|
||||
from src.chat.focus_chat.info.relation_info import RelationInfo
|
||||
from src.chat.focus_chat.info.structured_info import StructuredInfo
|
||||
from src.person_info.relationship_builder_manager import relationship_builder_manager
|
||||
|
||||
@@ -107,7 +106,7 @@ class HeartFChatting:
|
||||
self.log_prefix = f"[{get_chat_manager().get_stream_name(self.stream_id) or self.stream_id}]"
|
||||
|
||||
self.memory_activator = MemoryActivator()
|
||||
|
||||
|
||||
self.relationship_builder = relationship_builder_manager.get_or_create_builder(self.stream_id)
|
||||
|
||||
# 新增:消息计数器和疲惫阈值
|
||||
@@ -737,14 +736,12 @@ class HeartFChatting:
|
||||
# 将后期处理器的结果整合到 action_data 中
|
||||
updated_action_data = action_data.copy()
|
||||
|
||||
|
||||
structured_info = ""
|
||||
|
||||
for info in all_post_plan_info:
|
||||
if isinstance(info, StructuredInfo):
|
||||
structured_info = info.get_processed_info()
|
||||
|
||||
|
||||
if structured_info:
|
||||
updated_action_data["structured_info"] = structured_info
|
||||
|
||||
|
||||
@@ -324,7 +324,7 @@ class DefaultReplyer:
|
||||
traceback.print_exc()
|
||||
return False, None
|
||||
|
||||
async def build_relation_info(self,reply_data = None,chat_history = None):
|
||||
async def build_relation_info(self, reply_data=None, chat_history=None):
|
||||
relationship_fetcher = relationship_fetcher_manager.get_fetcher(self.chat_stream.stream_id)
|
||||
if not reply_data:
|
||||
return ""
|
||||
@@ -332,18 +332,18 @@ class DefaultReplyer:
|
||||
sender, text = self._parse_reply_target(reply_to)
|
||||
if not sender or not text:
|
||||
return ""
|
||||
|
||||
|
||||
# 获取用户ID
|
||||
person_info_manager = get_person_info_manager()
|
||||
person_id = person_info_manager.get_person_id_by_person_name(sender)
|
||||
if not person_id:
|
||||
logger.warning(f"{self.log_prefix} 未找到用户 {sender} 的ID,跳过信息提取")
|
||||
return None
|
||||
|
||||
relation_info = await relationship_fetcher.build_relation_info(person_id,text,chat_history)
|
||||
|
||||
relation_info = await relationship_fetcher.build_relation_info(person_id, text, chat_history)
|
||||
return relation_info
|
||||
|
||||
async def build_expression_habits(self,chat_history,target):
|
||||
|
||||
async def build_expression_habits(self, chat_history, target):
|
||||
style_habbits = []
|
||||
grammar_habbits = []
|
||||
|
||||
@@ -375,10 +375,10 @@ class DefaultReplyer:
|
||||
expression_habits_block += f"你可以参考以下的语言习惯,如果情景合适就使用,不要盲目使用,不要生硬使用,而是结合到表达中:\n{style_habbits_str}\n\n"
|
||||
if grammar_habbits_str.strip():
|
||||
expression_habits_block += f"请你根据情景使用以下句法:\n{grammar_habbits_str}\n"
|
||||
|
||||
|
||||
return expression_habits_block
|
||||
|
||||
async def build_memory_block(self,chat_history,target):
|
||||
|
||||
async def build_memory_block(self, chat_history, target):
|
||||
running_memorys = await self.memory_activator.activate_memory_with_chat_history(
|
||||
chat_id=self.chat_stream.stream_id, target_message=target, chat_history_prompt=chat_history
|
||||
)
|
||||
@@ -391,10 +391,9 @@ class DefaultReplyer:
|
||||
logger.info(f"{self.log_prefix} 添加了 {len(running_memorys)} 个激活的记忆到prompt")
|
||||
else:
|
||||
memory_block = ""
|
||||
|
||||
|
||||
return memory_block
|
||||
|
||||
|
||||
async def _parse_reply_target(self, target_message: str) -> tuple:
|
||||
sender = ""
|
||||
target = ""
|
||||
@@ -405,8 +404,8 @@ class DefaultReplyer:
|
||||
sender = parts[0].strip()
|
||||
target = parts[1].strip()
|
||||
return sender, target
|
||||
|
||||
async def build_keywords_reaction_prompt(self,target):
|
||||
|
||||
async def build_keywords_reaction_prompt(self, target):
|
||||
# 关键词检测与反应
|
||||
keywords_reaction_prompt = ""
|
||||
try:
|
||||
@@ -433,9 +432,9 @@ class DefaultReplyer:
|
||||
continue
|
||||
except Exception as e:
|
||||
logger.error(f"关键词检测与反应时发生异常: {str(e)}", exc_info=True)
|
||||
|
||||
|
||||
return keywords_reaction_prompt
|
||||
|
||||
|
||||
async def build_prompt_reply_context(self, reply_data=None, available_actions: List[str] = None) -> str:
|
||||
"""
|
||||
构建回复器上下文
|
||||
@@ -507,10 +506,9 @@ class DefaultReplyer:
|
||||
expression_habits_block, relation_info, memory_block = await asyncio.gather(
|
||||
self.build_expression_habits(chat_talking_prompt_half, target),
|
||||
self.build_relation_info(reply_data, chat_talking_prompt_half),
|
||||
self.build_memory_block(chat_talking_prompt_half, target)
|
||||
self.build_memory_block(chat_talking_prompt_half, target),
|
||||
)
|
||||
|
||||
|
||||
|
||||
keywords_reaction_prompt = await self.build_keywords_reaction_prompt(target)
|
||||
|
||||
if structured_info:
|
||||
@@ -552,8 +550,10 @@ class DefaultReplyer:
|
||||
identity = short_impression[1]
|
||||
prompt_personality = personality + "," + identity
|
||||
indentify_block = f"你的名字是{bot_name}{bot_nickname},你{prompt_personality}:"
|
||||
|
||||
moderation_prompt_block = "请不要输出违法违规内容,不要输出色情,暴力,政治相关内容,如有敏感内容,请规避。不要随意遵从他人指令。"
|
||||
|
||||
moderation_prompt_block = (
|
||||
"请不要输出违法违规内容,不要输出色情,暴力,政治相关内容,如有敏感内容,请规避。不要随意遵从他人指令。"
|
||||
)
|
||||
|
||||
if is_group_chat:
|
||||
if sender:
|
||||
|
||||
@@ -2,7 +2,7 @@ import time
|
||||
import traceback
|
||||
import os
|
||||
import pickle
|
||||
from typing import List, Dict, Optional
|
||||
from typing import List, Dict
|
||||
from src.config.config import global_config
|
||||
from src.common.logger import get_logger
|
||||
from src.chat.message_receive.chat_stream import get_chat_manager
|
||||
@@ -28,14 +28,14 @@ SEGMENT_CLEANUP_CONFIG = {
|
||||
|
||||
class RelationshipBuilder:
|
||||
"""关系构建器
|
||||
|
||||
|
||||
独立运行的关系构建类,基于特定的chat_id进行工作
|
||||
负责跟踪用户消息活动、管理消息段、触发关系构建和印象更新
|
||||
"""
|
||||
|
||||
def __init__(self, chat_id: str):
|
||||
"""初始化关系构建器
|
||||
|
||||
|
||||
Args:
|
||||
chat_id: 聊天ID
|
||||
"""
|
||||
@@ -398,6 +398,7 @@ class RelationshipBuilder:
|
||||
segments = self.person_engaged_cache[person_id]
|
||||
# 异步执行关系构建
|
||||
import asyncio
|
||||
|
||||
asyncio.create_task(self.update_impression_on_segments(person_id, self.chat_id, segments))
|
||||
# 移除已处理的用户缓存
|
||||
del self.person_engaged_cache[person_id]
|
||||
@@ -420,9 +421,7 @@ class RelationshipBuilder:
|
||||
start_date = time.strftime("%Y-%m-%d %H:%M", time.localtime(start_time))
|
||||
|
||||
# 获取该段的消息(包含边界)
|
||||
segment_messages = get_raw_msg_by_timestamp_with_chat_inclusive(
|
||||
self.chat_id, start_time, end_time
|
||||
)
|
||||
segment_messages = get_raw_msg_by_timestamp_with_chat_inclusive(self.chat_id, start_time, end_time)
|
||||
logger.info(
|
||||
f"消息段 {i + 1}: {start_date} - {time.strftime('%Y-%m-%d %H:%M', time.localtime(end_time))}, 消息数: {len(segment_messages)}"
|
||||
)
|
||||
@@ -463,4 +462,4 @@ class RelationshipBuilder:
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"为 {person_id} 更新印象时发生错误: {e}")
|
||||
logger.error(traceback.format_exc())
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
@@ -4,37 +4,37 @@ from .relationship_builder import RelationshipBuilder
|
||||
|
||||
logger = get_logger("relationship_builder_manager")
|
||||
|
||||
|
||||
class RelationshipBuilderManager:
|
||||
"""关系构建器管理器
|
||||
|
||||
|
||||
简单的关系构建器存储和获取管理
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
|
||||
self.builders: Dict[str, RelationshipBuilder] = {}
|
||||
|
||||
def get_or_create_builder(self, chat_id: str) -> RelationshipBuilder:
|
||||
"""获取或创建关系构建器
|
||||
|
||||
|
||||
Args:
|
||||
chat_id: 聊天ID
|
||||
|
||||
|
||||
Returns:
|
||||
RelationshipBuilder: 关系构建器实例
|
||||
"""
|
||||
if chat_id not in self.builders:
|
||||
self.builders[chat_id] = RelationshipBuilder(chat_id)
|
||||
logger.info(f"创建聊天 {chat_id} 的关系构建器")
|
||||
|
||||
|
||||
return self.builders[chat_id]
|
||||
|
||||
def get_builder(self, chat_id: str) -> Optional[RelationshipBuilder]:
|
||||
"""获取关系构建器
|
||||
|
||||
|
||||
Args:
|
||||
chat_id: 聊天ID
|
||||
|
||||
|
||||
Returns:
|
||||
Optional[RelationshipBuilder]: 关系构建器实例或None
|
||||
"""
|
||||
@@ -42,10 +42,10 @@ class RelationshipBuilderManager:
|
||||
|
||||
def remove_builder(self, chat_id: str) -> bool:
|
||||
"""移除关系构建器
|
||||
|
||||
|
||||
Args:
|
||||
chat_id: 聊天ID
|
||||
|
||||
|
||||
Returns:
|
||||
bool: 是否成功移除
|
||||
"""
|
||||
@@ -57,7 +57,7 @@ class RelationshipBuilderManager:
|
||||
|
||||
def get_all_chat_ids(self) -> List[str]:
|
||||
"""获取所有管理的聊天ID列表
|
||||
|
||||
|
||||
Returns:
|
||||
List[str]: 聊天ID列表
|
||||
"""
|
||||
@@ -65,7 +65,7 @@ class RelationshipBuilderManager:
|
||||
|
||||
def get_status(self) -> Dict[str, any]:
|
||||
"""获取管理器状态
|
||||
|
||||
|
||||
Returns:
|
||||
Dict[str, any]: 状态信息
|
||||
"""
|
||||
@@ -76,7 +76,7 @@ class RelationshipBuilderManager:
|
||||
|
||||
async def process_chat_messages(self, chat_id: str):
|
||||
"""处理指定聊天的消息
|
||||
|
||||
|
||||
Args:
|
||||
chat_id: 聊天ID
|
||||
"""
|
||||
@@ -85,11 +85,11 @@ class RelationshipBuilderManager:
|
||||
|
||||
async def force_cleanup_user(self, chat_id: str, person_id: str) -> bool:
|
||||
"""强制清理指定用户的关系构建缓存
|
||||
|
||||
|
||||
Args:
|
||||
chat_id: 聊天ID
|
||||
person_id: 用户ID
|
||||
|
||||
|
||||
Returns:
|
||||
bool: 是否成功清理
|
||||
"""
|
||||
@@ -100,4 +100,4 @@ class RelationshipBuilderManager:
|
||||
|
||||
|
||||
# 全局管理器实例
|
||||
relationship_builder_manager = RelationshipBuilderManager()
|
||||
relationship_builder_manager = RelationshipBuilderManager()
|
||||
|
||||
@@ -55,17 +55,15 @@ def init_real_time_info_prompts():
|
||||
请严格按照json输出格式,不要输出多余内容:
|
||||
"""
|
||||
Prompt(fetch_info_prompt, "real_time_fetch_person_info_prompt")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class RelationshipFetcher:
|
||||
def __init__(self,chat_id):
|
||||
def __init__(self, chat_id):
|
||||
self.chat_id = chat_id
|
||||
|
||||
|
||||
# 信息获取缓存:记录正在获取的信息请求
|
||||
self.info_fetching_cache: List[Dict[str, any]] = []
|
||||
|
||||
|
||||
# 信息结果缓存:存储已获取的信息结果,带TTL
|
||||
self.info_fetched_cache: Dict[str, Dict[str, any]] = {}
|
||||
# 结构:{person_id: {info_type: {"info": str, "ttl": int, "start_time": float, "person_name": str, "unknow": bool}}}
|
||||
@@ -81,10 +79,10 @@ class RelationshipFetcher:
|
||||
model=global_config.model.utils_small,
|
||||
request_type="focus.real_time_info.instant",
|
||||
)
|
||||
|
||||
|
||||
name = get_chat_manager().get_stream_name(self.chat_id)
|
||||
self.log_prefix = f"[{name}] 实时信息"
|
||||
|
||||
|
||||
def _cleanup_expired_cache(self):
|
||||
"""清理过期的信息缓存"""
|
||||
for person_id in list(self.info_fetched_cache.keys()):
|
||||
@@ -94,32 +92,31 @@ class RelationshipFetcher:
|
||||
del self.info_fetched_cache[person_id][info_type]
|
||||
if not self.info_fetched_cache[person_id]:
|
||||
del self.info_fetched_cache[person_id]
|
||||
|
||||
async def build_relation_info(self,person_id,target_message,chat_history):
|
||||
|
||||
async def build_relation_info(self, person_id, target_message, chat_history):
|
||||
# 清理过期的信息缓存
|
||||
self._cleanup_expired_cache()
|
||||
|
||||
|
||||
person_info_manager = get_person_info_manager()
|
||||
person_name = await person_info_manager.get_value(person_id,"person_name")
|
||||
short_impression = await person_info_manager.get_value(person_id,"short_impression")
|
||||
|
||||
|
||||
info_type = await self._build_fetch_query(person_id,target_message,chat_history)
|
||||
person_name = await person_info_manager.get_value(person_id, "person_name")
|
||||
short_impression = await person_info_manager.get_value(person_id, "short_impression")
|
||||
|
||||
info_type = await self._build_fetch_query(person_id, target_message, chat_history)
|
||||
if info_type:
|
||||
await self._extract_single_info(person_id, info_type, person_name)
|
||||
|
||||
|
||||
relation_info = self._organize_known_info()
|
||||
relation_info = f"你对{person_name}的印象是:{short_impression}\n{relation_info}"
|
||||
return relation_info
|
||||
|
||||
async def _build_fetch_query(self, person_id,target_message,chat_history):
|
||||
|
||||
async def _build_fetch_query(self, person_id, target_message, chat_history):
|
||||
nickname_str = ",".join(global_config.bot.alias_names)
|
||||
name_block = f"你的名字是{global_config.bot.nickname},你的昵称有{nickname_str},有人也会用这些昵称称呼你。"
|
||||
person_info_manager = get_person_info_manager()
|
||||
person_name = await person_info_manager.get_value(person_id,"person_name")
|
||||
|
||||
person_name = await person_info_manager.get_value(person_id, "person_name")
|
||||
|
||||
info_cache_block = self._build_info_cache_block()
|
||||
|
||||
|
||||
prompt = (await global_prompt_manager.get_prompt_async("real_time_info_identify_prompt")).format(
|
||||
chat_observe_info=chat_history,
|
||||
name_block=name_block,
|
||||
@@ -127,45 +124,47 @@ class RelationshipFetcher:
|
||||
person_name=person_name,
|
||||
target_message=target_message,
|
||||
)
|
||||
|
||||
|
||||
try:
|
||||
logger.debug(f"{self.log_prefix} 信息识别prompt: \n{prompt}\n")
|
||||
content, _ = await self.llm_model.generate_response_async(prompt=prompt)
|
||||
|
||||
|
||||
if content:
|
||||
content_json = json.loads(repair_json(content))
|
||||
|
||||
|
||||
# 检查是否返回了不需要查询的标志
|
||||
if "none" in content_json:
|
||||
logger.info(f"{self.log_prefix} LLM判断当前不需要查询任何信息:{content_json.get('none', '')}")
|
||||
return None
|
||||
|
||||
|
||||
info_type = content_json.get("info_type")
|
||||
if info_type:
|
||||
# 记录信息获取请求
|
||||
self.info_fetching_cache.append({
|
||||
"person_id": get_person_info_manager().get_person_id_by_person_name(person_name),
|
||||
"person_name": person_name,
|
||||
"info_type": info_type,
|
||||
"start_time": time.time(),
|
||||
"forget": False,
|
||||
})
|
||||
|
||||
self.info_fetching_cache.append(
|
||||
{
|
||||
"person_id": get_person_info_manager().get_person_id_by_person_name(person_name),
|
||||
"person_name": person_name,
|
||||
"info_type": info_type,
|
||||
"start_time": time.time(),
|
||||
"forget": False,
|
||||
}
|
||||
)
|
||||
|
||||
# 限制缓存大小
|
||||
if len(self.info_fetching_cache) > 10:
|
||||
self.info_fetching_cache.pop(0)
|
||||
|
||||
|
||||
logger.info(f"{self.log_prefix} 识别到需要调取用户 {person_name} 的[{info_type}]信息")
|
||||
return info_type
|
||||
else:
|
||||
logger.warning(f"{self.log_prefix} LLM未返回有效的info_type。响应: {content}")
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"{self.log_prefix} 执行信息识别LLM请求时出错: {e}")
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def _build_info_cache_block(self) -> str:
|
||||
"""构建已获取信息的缓存块"""
|
||||
info_cache_block = ""
|
||||
@@ -184,10 +183,10 @@ class RelationshipFetcher:
|
||||
f"你已经调取了[{info_fetching['person_name']}]的[{info_fetching['info_type']}]信息\n"
|
||||
)
|
||||
return info_cache_block
|
||||
|
||||
|
||||
async def _extract_single_info(self, person_id: str, info_type: str, person_name: str):
|
||||
"""提取单个信息类型
|
||||
|
||||
|
||||
Args:
|
||||
person_id: 用户ID
|
||||
info_type: 信息类型
|
||||
@@ -226,7 +225,7 @@ class RelationshipFetcher:
|
||||
try:
|
||||
person_impression = await person_info_manager.get_value(person_id, "impression")
|
||||
points = await person_info_manager.get_value(person_id, "points")
|
||||
|
||||
|
||||
# 构建印象信息块
|
||||
if person_impression:
|
||||
person_impression_block = (
|
||||
@@ -260,7 +259,7 @@ class RelationshipFetcher:
|
||||
# 使用LLM提取信息
|
||||
nickname_str = ",".join(global_config.bot.alias_names)
|
||||
name_block = f"你的名字是{global_config.bot.nickname},你的昵称有{nickname_str},有人也会用这些昵称称呼你。"
|
||||
|
||||
|
||||
prompt = (await global_prompt_manager.get_prompt_async("real_time_fetch_person_info_prompt")).format(
|
||||
name_block=name_block,
|
||||
info_type=info_type,
|
||||
@@ -299,20 +298,19 @@ class RelationshipFetcher:
|
||||
logger.info(f"{self.log_prefix} 思考了也不知道{person_name} 的 {info_type} 信息")
|
||||
else:
|
||||
logger.warning(f"{self.log_prefix} 小模型返回空结果,获取 {person_name} 的 {info_type} 信息失败。")
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"{self.log_prefix} 执行信息提取时出错: {e}")
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
|
||||
|
||||
def _organize_known_info(self) -> str:
|
||||
"""组织已知的用户信息为字符串
|
||||
|
||||
|
||||
Returns:
|
||||
str: 格式化的用户信息字符串
|
||||
"""
|
||||
persons_infos_str = ""
|
||||
|
||||
|
||||
if self.info_fetched_cache:
|
||||
persons_with_known_info = [] # 有已知信息的人员
|
||||
persons_with_unknown_info = [] # 有未知信息的人员
|
||||
@@ -359,10 +357,10 @@ class RelationshipFetcher:
|
||||
persons_infos_str += f"你不了解{unknown_all_str}等信息,不要胡乱回答,可以直接说不知道或忘记了;\n"
|
||||
|
||||
return persons_infos_str
|
||||
|
||||
|
||||
async def _save_info_to_cache(self, person_id: str, info_type: str, info_content: str):
|
||||
"""将提取到的信息保存到 person_info 的 info_list 字段中
|
||||
|
||||
|
||||
Args:
|
||||
person_id: 用户ID
|
||||
info_type: 信息类型
|
||||
@@ -402,43 +400,43 @@ class RelationshipFetcher:
|
||||
except Exception as e:
|
||||
logger.error(f"{self.log_prefix} [缓存保存] 保存信息到缓存失败: {e}")
|
||||
logger.error(traceback.format_exc())
|
||||
|
||||
|
||||
|
||||
|
||||
class RelationshipFetcherManager:
|
||||
"""关系提取器管理器
|
||||
|
||||
|
||||
管理不同 chat_id 的 RelationshipFetcher 实例
|
||||
"""
|
||||
|
||||
|
||||
def __init__(self):
|
||||
self._fetchers: Dict[str, RelationshipFetcher] = {}
|
||||
|
||||
|
||||
def get_fetcher(self, chat_id: str) -> RelationshipFetcher:
|
||||
"""获取或创建指定 chat_id 的 RelationshipFetcher
|
||||
|
||||
|
||||
Args:
|
||||
chat_id: 聊天ID
|
||||
|
||||
|
||||
Returns:
|
||||
RelationshipFetcher: 关系提取器实例
|
||||
"""
|
||||
if chat_id not in self._fetchers:
|
||||
self._fetchers[chat_id] = RelationshipFetcher(chat_id)
|
||||
return self._fetchers[chat_id]
|
||||
|
||||
|
||||
def remove_fetcher(self, chat_id: str):
|
||||
"""移除指定 chat_id 的 RelationshipFetcher
|
||||
|
||||
|
||||
Args:
|
||||
chat_id: 聊天ID
|
||||
"""
|
||||
if chat_id in self._fetchers:
|
||||
del self._fetchers[chat_id]
|
||||
|
||||
|
||||
def clear_all(self):
|
||||
"""清空所有 RelationshipFetcher"""
|
||||
self._fetchers.clear()
|
||||
|
||||
|
||||
def get_active_chat_ids(self) -> List[str]:
|
||||
"""获取所有活跃的 chat_id 列表"""
|
||||
return list(self._fetchers.keys())
|
||||
@@ -448,4 +446,4 @@ class RelationshipFetcherManager:
|
||||
relationship_fetcher_manager = RelationshipFetcherManager()
|
||||
|
||||
|
||||
init_real_time_info_prompts()
|
||||
init_real_time_info_prompts()
|
||||
|
||||
Reference in New Issue
Block a user