feat:合并normal和focus的prompt构建
This commit is contained in:
@@ -23,7 +23,6 @@ from src.chat.heart_flow.observation.actions_observation import ActionObservatio
|
||||
from src.chat.focus_chat.info_processors.tool_processor import ToolProcessor
|
||||
from src.chat.focus_chat.memory_activator import MemoryActivator
|
||||
from src.chat.focus_chat.info_processors.base_processor import BaseProcessor
|
||||
from src.chat.focus_chat.info_processors.expression_selector_processor import ExpressionSelectorProcessor
|
||||
from src.chat.focus_chat.planners.planner_factory import PlannerFactory
|
||||
from src.chat.focus_chat.planners.modify_actions import ActionModifier
|
||||
from src.chat.focus_chat.planners.action_manager import ActionManager
|
||||
@@ -31,7 +30,6 @@ 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.expression_selection_info import ExpressionSelectionInfo
|
||||
from src.chat.focus_chat.info.structured_info import StructuredInfo
|
||||
|
||||
|
||||
@@ -59,7 +57,6 @@ PROCESSOR_CLASSES = {
|
||||
POST_PLANNING_PROCESSOR_CLASSES = {
|
||||
"ToolProcessor": (ToolProcessor, "tool_use_processor"),
|
||||
"PersonImpressionpProcessor": (PersonImpressionpProcessor, "person_impression_processor"),
|
||||
"ExpressionSelectorProcessor": (ExpressionSelectorProcessor, "expression_selector_processor"),
|
||||
}
|
||||
|
||||
logger = get_logger("hfc") # Logger Name Changed
|
||||
@@ -699,30 +696,6 @@ class HeartFChatting:
|
||||
task_start_times[task] = time.time()
|
||||
logger.info(f"{self.log_prefix} 启动后期处理器任务: {processor_name}")
|
||||
|
||||
# 添加记忆激活器任务
|
||||
async def run_memory_with_timeout_and_timing():
|
||||
start_time = time.time()
|
||||
try:
|
||||
result = await asyncio.wait_for(
|
||||
self.memory_activator.activate_memory(observations),
|
||||
timeout=MEMORY_ACTIVATION_TIMEOUT,
|
||||
)
|
||||
end_time = time.time()
|
||||
post_processor_time_costs["MemoryActivator"] = end_time - start_time
|
||||
logger.debug(f"{self.log_prefix} 记忆激活器耗时: {end_time - start_time:.3f}秒")
|
||||
return result
|
||||
except Exception as e:
|
||||
end_time = time.time()
|
||||
post_processor_time_costs["MemoryActivator"] = end_time - start_time
|
||||
logger.warning(f"{self.log_prefix} 记忆激活器执行异常,耗时: {end_time - start_time:.3f}秒")
|
||||
raise e
|
||||
|
||||
memory_task = asyncio.create_task(run_memory_with_timeout_and_timing())
|
||||
task_list.append(memory_task)
|
||||
task_to_name_map[memory_task] = ("memory", "MemoryActivator")
|
||||
task_start_times[memory_task] = time.time()
|
||||
logger.info(f"{self.log_prefix} 启动记忆激活器任务")
|
||||
|
||||
# 如果没有任何后期任务,直接返回
|
||||
if not task_list:
|
||||
logger.info(f"{self.log_prefix} 没有启用的后期处理器或记忆激活器")
|
||||
@@ -731,7 +704,6 @@ class HeartFChatting:
|
||||
# 等待所有任务完成
|
||||
pending_tasks = set(task_list)
|
||||
all_post_plan_info = []
|
||||
running_memorys = []
|
||||
|
||||
while pending_tasks:
|
||||
done, pending_tasks = await asyncio.wait(pending_tasks, return_when=asyncio.FIRST_COMPLETED)
|
||||
@@ -748,13 +720,6 @@ class HeartFChatting:
|
||||
all_post_plan_info.extend(result)
|
||||
else:
|
||||
logger.warning(f"{self.log_prefix} 后期处理器 {task_name} 返回了 None")
|
||||
elif task_type == "memory":
|
||||
logger.info(f"{self.log_prefix} 记忆激活器已完成!")
|
||||
if result is not None:
|
||||
running_memorys = result
|
||||
else:
|
||||
logger.warning(f"{self.log_prefix} 记忆激活器返回了 None")
|
||||
running_memorys = []
|
||||
|
||||
except asyncio.TimeoutError:
|
||||
# 对于超时任务,记录已用时间
|
||||
@@ -764,12 +729,6 @@ class HeartFChatting:
|
||||
logger.warning(
|
||||
f"{self.log_prefix} 后期处理器 {task_name} 超时(>{global_config.focus_chat.processor_max_time}s),已跳过,耗时: {elapsed_time:.3f}秒"
|
||||
)
|
||||
elif task_type == "memory":
|
||||
post_processor_time_costs["MemoryActivator"] = elapsed_time
|
||||
logger.warning(
|
||||
f"{self.log_prefix} 记忆激活器超时(>{MEMORY_ACTIVATION_TIMEOUT}s),已跳过,耗时: {elapsed_time:.3f}秒"
|
||||
)
|
||||
running_memorys = []
|
||||
except Exception as e:
|
||||
# 对于异常任务,记录已用时间
|
||||
elapsed_time = time.time() - task_start_times[task]
|
||||
@@ -779,49 +738,29 @@ class HeartFChatting:
|
||||
f"{self.log_prefix} 后期处理器 {task_name} 执行失败,耗时: {elapsed_time:.3f}秒. 错误: {e}",
|
||||
exc_info=True,
|
||||
)
|
||||
elif task_type == "memory":
|
||||
post_processor_time_costs["MemoryActivator"] = elapsed_time
|
||||
logger.error(
|
||||
f"{self.log_prefix} 记忆激活器执行失败,耗时: {elapsed_time:.3f}秒. 错误: {e}",
|
||||
exc_info=True,
|
||||
)
|
||||
running_memorys = []
|
||||
|
||||
# 将后期处理器的结果整合到 action_data 中
|
||||
updated_action_data = action_data.copy()
|
||||
|
||||
relation_info = ""
|
||||
selected_expressions = []
|
||||
structured_info = ""
|
||||
|
||||
for info in all_post_plan_info:
|
||||
if isinstance(info, RelationInfo):
|
||||
relation_info = info.get_processed_info()
|
||||
elif isinstance(info, ExpressionSelectionInfo):
|
||||
selected_expressions = info.get_expressions_for_action_data()
|
||||
elif isinstance(info, StructuredInfo):
|
||||
structured_info = info.get_processed_info()
|
||||
|
||||
if relation_info:
|
||||
updated_action_data["relation_info_block"] = relation_info
|
||||
updated_action_data["relation_info"] = relation_info
|
||||
|
||||
if selected_expressions:
|
||||
updated_action_data["selected_expressions"] = selected_expressions
|
||||
|
||||
if structured_info:
|
||||
updated_action_data["structured_info"] = structured_info
|
||||
|
||||
# 特殊处理running_memorys
|
||||
if running_memorys:
|
||||
memory_str = "以下是当前在聊天中,你回忆起的记忆:\n"
|
||||
for running_memory in running_memorys:
|
||||
memory_str += f"{running_memory['content']}\n"
|
||||
updated_action_data["memory_block"] = memory_str
|
||||
logger.info(f"{self.log_prefix} 添加了 {len(running_memorys)} 个激活的记忆到action_data")
|
||||
|
||||
if all_post_plan_info or running_memorys:
|
||||
if all_post_plan_info:
|
||||
logger.info(
|
||||
f"{self.log_prefix} 后期处理完成,产生了 {len(all_post_plan_info)} 个信息项和 {len(running_memorys)} 个记忆"
|
||||
f"{self.log_prefix} 后期处理完成,产生了 {len(all_post_plan_info)} 个信息项"
|
||||
)
|
||||
|
||||
# 输出详细统计信息
|
||||
@@ -908,7 +847,7 @@ class HeartFChatting:
|
||||
logger.debug(f"{self.log_prefix} 并行阶段完成,准备进入规划器,plan_info数量: {len(all_plan_info)}")
|
||||
|
||||
with Timer("规划器", cycle_timers):
|
||||
plan_result = await self.action_planner.plan(all_plan_info, [], loop_start_time)
|
||||
plan_result = await self.action_planner.plan(all_plan_info, self.observations, loop_start_time)
|
||||
|
||||
loop_plan_info = {
|
||||
"action_result": plan_result.get("action_result", {}),
|
||||
|
||||
@@ -1,107 +0,0 @@
|
||||
import time
|
||||
import random
|
||||
from typing import List
|
||||
from src.chat.heart_flow.observation.chatting_observation import ChattingObservation
|
||||
from src.chat.heart_flow.observation.observation import Observation
|
||||
from src.common.logger import get_logger
|
||||
from src.chat.message_receive.chat_stream import get_chat_manager
|
||||
from .base_processor import BaseProcessor
|
||||
from src.chat.focus_chat.info.info_base import InfoBase
|
||||
from src.chat.focus_chat.info.expression_selection_info import ExpressionSelectionInfo
|
||||
from src.chat.express.expression_selector import expression_selector
|
||||
|
||||
logger = get_logger("processor")
|
||||
|
||||
|
||||
class ExpressionSelectorProcessor(BaseProcessor):
|
||||
log_prefix = "表达选择器"
|
||||
|
||||
def __init__(self, subheartflow_id: str):
|
||||
super().__init__()
|
||||
|
||||
self.subheartflow_id = subheartflow_id
|
||||
self.last_selection_time = 0
|
||||
self.selection_interval = 10 # 40秒间隔
|
||||
self.cached_expressions = [] # 缓存上一次选择的表达方式
|
||||
|
||||
name = get_chat_manager().get_stream_name(self.subheartflow_id)
|
||||
self.log_prefix = f"[{name}] 表达选择器"
|
||||
|
||||
async def process_info(
|
||||
self,
|
||||
observations: List[Observation] = None,
|
||||
action_type: str = None,
|
||||
action_data: dict = None,
|
||||
**kwargs,
|
||||
) -> List[InfoBase]:
|
||||
"""处理信息对象
|
||||
|
||||
Args:
|
||||
observations: 观察对象列表
|
||||
|
||||
Returns:
|
||||
List[InfoBase]: 处理后的表达选择信息列表
|
||||
"""
|
||||
current_time = time.time()
|
||||
|
||||
# 检查频率限制
|
||||
if current_time - self.last_selection_time < self.selection_interval:
|
||||
logger.debug(f"{self.log_prefix} 距离上次选择不足{self.selection_interval}秒,使用缓存的表达方式")
|
||||
# 使用缓存的表达方式
|
||||
if self.cached_expressions:
|
||||
# 从缓存的15个中随机选5个
|
||||
final_expressions = random.sample(self.cached_expressions, min(5, len(self.cached_expressions)))
|
||||
|
||||
# 创建表达选择信息
|
||||
expression_info = ExpressionSelectionInfo()
|
||||
expression_info.set_selected_expressions(final_expressions)
|
||||
|
||||
logger.info(f"{self.log_prefix} 使用缓存选择了{len(final_expressions)}个表达方式")
|
||||
return [expression_info]
|
||||
else:
|
||||
logger.debug(f"{self.log_prefix} 没有缓存的表达方式,跳过选择")
|
||||
return []
|
||||
|
||||
# 获取聊天内容
|
||||
chat_info = ""
|
||||
if observations:
|
||||
for observation in observations:
|
||||
if isinstance(observation, ChattingObservation):
|
||||
# chat_info = observation.get_observe_info()
|
||||
chat_info = observation.talking_message_str_truncate_short
|
||||
break
|
||||
|
||||
if not chat_info:
|
||||
logger.debug(f"{self.log_prefix} 没有聊天内容,跳过表达方式选择")
|
||||
return []
|
||||
|
||||
try:
|
||||
if action_type == "reply":
|
||||
target_message = action_data.get("reply_to", "")
|
||||
else:
|
||||
target_message = ""
|
||||
|
||||
# LLM模式:调用LLM选择5-10个,然后随机选5个
|
||||
selected_expressions = await expression_selector.select_suitable_expressions_llm(
|
||||
self.subheartflow_id, chat_info, max_num=12, min_num=2, target_message=target_message
|
||||
)
|
||||
cache_size = len(selected_expressions) if selected_expressions else 0
|
||||
mode_desc = f"LLM模式(已缓存{cache_size}个)"
|
||||
|
||||
if selected_expressions:
|
||||
self.cached_expressions = selected_expressions
|
||||
self.last_selection_time = current_time
|
||||
|
||||
# 创建表达选择信息
|
||||
expression_info = ExpressionSelectionInfo()
|
||||
expression_info.set_selected_expressions(selected_expressions)
|
||||
|
||||
logger.info(f"{self.log_prefix} 为当前聊天选择了{len(selected_expressions)}个表达方式({mode_desc})")
|
||||
return [expression_info]
|
||||
else:
|
||||
logger.debug(f"{self.log_prefix} 未选择任何表达方式")
|
||||
return []
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"{self.log_prefix} 处理表达方式选择时出错: {e}")
|
||||
return []
|
||||
@@ -10,6 +10,7 @@ from typing import List, Dict
|
||||
import difflib
|
||||
import json
|
||||
from json_repair import repair_json
|
||||
from src.person_info.person_info import get_person_info_manager
|
||||
|
||||
|
||||
logger = get_logger("memory_activator")
|
||||
@@ -75,8 +76,8 @@ class MemoryActivator:
|
||||
)
|
||||
self.running_memory = []
|
||||
self.cached_keywords = set() # 用于缓存历史关键词
|
||||
|
||||
async def activate_memory(self, observations) -> List[Dict]:
|
||||
|
||||
async def activate_memory_with_chat_history(self, chat_id, target_message, chat_history_prompt) -> List[Dict]:
|
||||
"""
|
||||
激活记忆
|
||||
|
||||
@@ -90,14 +91,14 @@ class MemoryActivator:
|
||||
if not global_config.memory.enable_memory:
|
||||
return []
|
||||
|
||||
obs_info_text = ""
|
||||
for observation in observations:
|
||||
if isinstance(observation, ChattingObservation):
|
||||
obs_info_text += observation.talking_message_str_truncate_short
|
||||
elif isinstance(observation, StructureObservation):
|
||||
working_info = observation.get_observe_info()
|
||||
for working_info_item in working_info:
|
||||
obs_info_text += f"{working_info_item['type']}: {working_info_item['content']}\n"
|
||||
# obs_info_text = ""
|
||||
# for observation in observations:
|
||||
# if isinstance(observation, ChattingObservation):
|
||||
# obs_info_text += observation.talking_message_str_truncate_short
|
||||
# elif isinstance(observation, StructureObservation):
|
||||
# working_info = observation.get_observe_info()
|
||||
# for working_info_item in working_info:
|
||||
# obs_info_text += f"{working_info_item['type']}: {working_info_item['content']}\n"
|
||||
|
||||
# logger.info(f"回忆待检索内容:obs_info_text: {obs_info_text}")
|
||||
|
||||
@@ -106,7 +107,7 @@ class MemoryActivator:
|
||||
|
||||
prompt = await global_prompt_manager.format_prompt(
|
||||
"memory_activator_prompt",
|
||||
obs_info_text=obs_info_text,
|
||||
obs_info_text=chat_history_prompt,
|
||||
cached_keywords=cached_keywords_str,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user