feat:优化了auto切换聊天模式机制,修改取名prompt,不再处理temp

This commit is contained in:
SengokuCola
2025-05-27 21:45:03 +08:00
parent 7e59382603
commit 369de9d137
14 changed files with 237 additions and 70 deletions

View File

@@ -1,6 +1,7 @@
from src.chat.heart_flow.heartflow import heartflow
from src.chat.heart_flow.sub_heartflow import ChatState
from src.common.logger_manager import get_logger
import time
logger = get_logger("api")
@@ -30,6 +31,29 @@ async def get_subheartflow_cycle_info(subheartflow_id: str, history_len: int) ->
return None
async def get_normal_chat_replies(subheartflow_id: str, limit: int = 10) -> list:
"""获取子心流的NormalChat回复记录
Args:
subheartflow_id: 子心流ID
limit: 最大返回数量默认10条
Returns:
list: 回复记录列表,如果未找到则返回空列表
"""
replies = await heartflow.api_get_normal_chat_replies(subheartflow_id, limit)
logger.debug(f"子心流 {subheartflow_id} NormalChat回复记录: 获取到 {len(replies) if replies else 0}")
if replies:
# 格式化时间戳为可读时间
for reply in replies:
if "time" in reply:
reply["formatted_time"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(reply["time"]))
return replies
else:
logger.warning(f"子心流 {subheartflow_id} NormalChat回复记录未找到")
return []
async def get_all_states():
"""获取所有状态"""
all_states = await heartflow.api_get_all_states()

View File

@@ -79,7 +79,7 @@ class DefaultExpressor:
# TODO: API-Adapter修改标记
self.express_model = LLMRequest(
model=global_config.model.focus_expressor,
temperature=global_config.model.focus_expressor["temp"],
# temperature=global_config.model.focus_expressor["temp"],
max_tokens=256,
request_type="focus_expressor",
)

View File

@@ -219,6 +219,7 @@ class HeartFCMessageReceiver:
)
# 8. 关系处理
if global_config.relationship.give_name:
await _process_relationship(message)
except Exception as e:

View File

@@ -107,12 +107,12 @@ class BackgroundTaskManager:
f"私聊激活检查任务已启动 间隔:{PRIVATE_CHAT_ACTIVATION_CHECK_INTERVAL_SECONDS}s",
"_private_chat_activation_task",
),
(
self._run_into_focus_cycle,
"debug", # 设为debug避免过多日志
f"专注评估任务已启动 间隔:{INTEREST_EVAL_INTERVAL_SECONDS}s",
"_into_focus_task",
)
# (
# self._run_into_focus_cycle,
# "debug", # 设为debug避免过多日志
# f"专注评估任务已启动 间隔:{INTEREST_EVAL_INTERVAL_SECONDS}s",
# "_into_focus_task",
# )
])
else:
logger.info("聊天模式为 normal跳过启动清理任务、私聊激活任务和专注评估任务")
@@ -215,10 +215,10 @@ class BackgroundTaskManager:
logger.info(f"[清理任务] 清理完成, 共停止 {stopped_count}/{len(flows_to_stop)} 个子心流")
# --- 新增兴趣评估工作函数 ---
async def _perform_into_focus_work(self):
"""执行一轮子心流兴趣评估与提升检查。"""
# 直接调用 subheartflow_manager 的方法,并传递当前状态信息
await self.subheartflow_manager.sbhf_normal_into_focus()
# async def _perform_into_focus_work(self):
# """执行一轮子心流兴趣评估与提升检查。"""
# # 直接调用 subheartflow_manager 的方法,并传递当前状态信息
# await self.subheartflow_manager.sbhf_normal_into_focus()
async def _run_state_update_cycle(self, interval: int):
await _run_periodic_loop(task_name="State Update", interval=interval, task_func=self._perform_state_update_work)
@@ -229,12 +229,12 @@ class BackgroundTaskManager:
)
# --- 新增兴趣评估任务运行器 ---
async def _run_into_focus_cycle(self):
await _run_periodic_loop(
task_name="Into Focus",
interval=INTEREST_EVAL_INTERVAL_SECONDS,
task_func=self._perform_into_focus_work,
)
# async def _run_into_focus_cycle(self):
# await _run_periodic_loop(
# task_name="Into Focus",
# interval=INTEREST_EVAL_INTERVAL_SECONDS,
# task_func=self._perform_into_focus_work,
# )
# 新增私聊激活任务运行器
async def _run_private_chat_activation_cycle(self, interval: int):

View File

@@ -1,6 +1,6 @@
from src.chat.heart_flow.sub_heartflow import SubHeartflow, ChatState
from src.common.logger_manager import get_logger
from typing import Any, Optional
from typing import Any, Optional, List
from src.chat.heart_flow.mai_state_manager import MaiStateInfo, MaiStateManager
from src.chat.heart_flow.subheartflow_manager import SubHeartflowManager
from src.chat.heart_flow.background_tasks import BackgroundTaskManager # Import BackgroundTaskManager
@@ -57,6 +57,23 @@ class Heartflow:
return heartfc_instance.get_cycle_history(last_n=history_len)
async def api_get_normal_chat_replies(self, subheartflow_id: str, limit: int = 10) -> Optional[List[dict]]:
"""获取子心流的NormalChat回复记录
Args:
subheartflow_id: 子心流ID
limit: 最大返回数量默认10条
Returns:
Optional[List[dict]]: 回复记录列表如果子心流不存在则返回None
"""
subheartflow = await self.subheartflow_manager.get_or_create_subheartflow(subheartflow_id)
if not subheartflow:
logger.warning(f"尝试获取不存在的子心流 {subheartflow_id} 的NormalChat回复记录")
return None
return subheartflow.get_normal_chat_recent_replies(limit)
async def heartflow_start_working(self):
"""启动后台任务"""
await self.background_task_manager.start_tasks()

View File

@@ -129,7 +129,12 @@ class SubHeartflow:
return False
# 在 rewind 为 True 或 NormalChat 实例尚未创建时,创建新实例
if rewind or not self.normal_chat_instance:
self.normal_chat_instance = NormalChat(chat_stream=chat_stream, interest_dict=self.get_interest_dict())
# 提供回调函数用于接收需要切换到focus模式的通知
self.normal_chat_instance = NormalChat(
chat_stream=chat_stream,
interest_dict=self.get_interest_dict(),
on_switch_to_focus_callback=self._handle_switch_to_focus_request
)
# 进行异步初始化
await self.normal_chat_instance.initialize()
@@ -144,6 +149,23 @@ class SubHeartflow:
self.normal_chat_instance = None # 启动/初始化失败,清理实例
return False
async def _handle_switch_to_focus_request(self) -> None:
"""
处理来自NormalChat的切换到focus模式的请求
Args:
stream_id: 请求切换的stream_id
"""
logger.info(f"{self.log_prefix} 收到NormalChat请求切换到focus模式")
# 切换到focus模式
current_state = self.chat_state.chat_status
if current_state == ChatState.NORMAL:
await self.change_chat_state(ChatState.FOCUSED)
logger.info(f"{self.log_prefix} 已根据NormalChat请求从NORMAL切换到FOCUSED状态")
else:
logger.warning(f"{self.log_prefix} 当前状态为{current_state.value}无法切换到FOCUSED状态")
async def _stop_heart_fc_chat(self):
"""停止并清理 HeartFChatting 实例"""
if self.heart_fc_instance:
@@ -289,6 +311,19 @@ class SubHeartflow:
def get_interest_dict(self) -> Dict[str, tuple[MessageRecv, float, bool]]:
return self.interest_chatting.interest_dict
def get_normal_chat_recent_replies(self, limit: int = 10) -> List[dict]:
"""获取NormalChat实例的最近回复记录
Args:
limit: 最大返回数量默认10条
Returns:
List[dict]: 最近的回复记录列表如果没有NormalChat实例则返回空列表
"""
if self.normal_chat_instance:
return self.normal_chat_instance.get_recent_replies(limit)
return []
def clear_interest_dict(self):
self.interest_chatting.interest_dict.clear()

View File

@@ -186,41 +186,41 @@ class SubHeartflowManager:
f"{log_prefix} 完成,共处理 {processed_count} 个子心流,成功将 {changed_count} 个非 ABSENT 子心流的状态更改为 ABSENT。"
)
async def sbhf_normal_into_focus(self):
"""评估子心流兴趣度满足条件则提升到FOCUSED状态基于start_hfc_probability"""
try:
for sub_hf in list(self.subheartflows.values()):
flow_id = sub_hf.subheartflow_id
stream_name = chat_manager.get_stream_name(flow_id) or flow_id
# async def sbhf_normal_into_focus(self):
# """评估子心流兴趣度满足条件则提升到FOCUSED状态基于start_hfc_probability"""
# try:
# for sub_hf in list(self.subheartflows.values()):
# flow_id = sub_hf.subheartflow_id
# stream_name = chat_manager.get_stream_name(flow_id) or flow_id
# 跳过已经是FOCUSED状态的子心流
if sub_hf.chat_state.chat_status == ChatState.FOCUSED:
continue
# # 跳过已经是FOCUSED状态的子心流
# if sub_hf.chat_state.chat_status == ChatState.FOCUSED:
# continue
if sub_hf.interest_chatting.start_hfc_probability == 0:
continue
else:
logger.debug(
f"{stream_name},现在状态: {sub_hf.chat_state.chat_status.value},进入专注概率: {sub_hf.interest_chatting.start_hfc_probability}"
)
# if sub_hf.interest_chatting.start_hfc_probability == 0:
# continue
# else:
# logger.debug(
# f"{stream_name},现在状态: {sub_hf.chat_state.chat_status.value},进入专注概率: {sub_hf.interest_chatting.start_hfc_probability}"
# )
if random.random() >= sub_hf.interest_chatting.start_hfc_probability:
continue
# if random.random() >= sub_hf.interest_chatting.start_hfc_probability:
# continue
# 获取最新状态并执行提升
current_subflow = self.subheartflows.get(flow_id)
if not current_subflow:
continue
# # 获取最新状态并执行提升
# current_subflow = self.subheartflows.get(flow_id)
# if not current_subflow:
# continue
logger.info(
f"{stream_name} 触发 认真水群 (概率={current_subflow.interest_chatting.start_hfc_probability:.2f})"
)
# logger.info(
# f"{stream_name} 触发 认真水群 (概率={current_subflow.interest_chatting.start_hfc_probability:.2f})"
# )
# 执行状态提升
await current_subflow.change_chat_state(ChatState.FOCUSED)
# # 执行状态提升
# await current_subflow.change_chat_state(ChatState.FOCUSED)
except Exception as e:
logger.error(f"启动HFC 兴趣评估失败: {e}", exc_info=True)
# except Exception as e:
# logger.error(f"启动HFC 兴趣评估失败: {e}", exc_info=True)
async def sbhf_focus_into_normal(self, subflow_id: Any):
"""

View File

@@ -7,7 +7,7 @@ from src.chat.message_receive.chat_stream import chat_manager
from src.chat.message_receive.message import MessageRecv
from src.experimental.only_message_process import MessageProcessor
from src.experimental.PFC.pfc_manager import PFCManager
from src.chat.focus_chat.heartflow_message_revceiver import HeartFCMessageReceiver
from src.chat.focus_chat.heartflow_message_processor import HeartFCMessageReceiver
from src.chat.utils.prompt_builder import Prompt, global_prompt_manager
from src.config.config import global_config

View File

@@ -27,7 +27,7 @@ logger = get_logger("normal_chat")
class NormalChat:
def __init__(self, chat_stream: ChatStream, interest_dict: dict = None):
def __init__(self, chat_stream: ChatStream, interest_dict: dict = None, on_switch_to_focus_callback=None):
"""初始化 NormalChat 实例。只进行同步操作。"""
# Basic info from chat_stream (sync)
@@ -50,6 +50,17 @@ class NormalChat:
self._chat_task: Optional[asyncio.Task] = None
self._initialized = False # Track initialization status
# 记录最近的回复内容,每项包含: {time, user_message, response, is_mentioned, is_reference_reply}
self.recent_replies = []
self.max_replies_history = 20 # 最多保存最近20条回复记录
# 添加回调函数用于在满足条件时通知切换到focus_chat模式
self.on_switch_to_focus_callback = on_switch_to_focus_callback
# 最近回复检查相关
self._last_check_time = time.time()
self._check_interval = 10 # 每10秒检查一次是否需要切换到focus模式
async def initialize(self):
"""异步初始化,获取聊天类型和目标信息。"""
if self._initialized:
@@ -197,6 +208,12 @@ class NormalChat:
logger.info(f"[{self.stream_name}] 兴趣监控任务被取消或置空,退出")
break
# 定期检查是否需要切换到focus模式
current_time = time.time()
if current_time - self._last_check_time > self._check_interval:
await self._check_switch_to_focus()
self._last_check_time = current_time
items_to_process = list(self.interest_dict.items())
if not items_to_process:
continue
@@ -312,6 +329,28 @@ class NormalChat:
# 检查 first_bot_msg 是否为 None (例如思考消息已被移除的情况)
if first_bot_msg:
info_catcher.catch_after_response(timing_results["消息发送"], response_set, first_bot_msg)
# 记录回复信息到最近回复列表中
reply_info = {
"time": time.time(),
"user_message": message.processed_plain_text,
"user_info": {
"user_id": message.message_info.user_info.user_id,
"user_nickname": message.message_info.user_info.user_nickname
},
"response": response_set,
"is_mentioned": is_mentioned,
"is_reference_reply": message.reply is not None, # 判断是否为引用回复
"timing": {k: round(v, 2) for k, v in timing_results.items()}
}
self.recent_replies.append(reply_info)
# 保持最近回复历史在限定数量内
if len(self.recent_replies) > self.max_replies_history:
self.recent_replies = self.recent_replies[-self.max_replies_history:]
# 检查是否需要切换到focus模式
await self._check_switch_to_focus()
else:
logger.warning(f"[{self.stream_name}] 思考消息 {thinking_id} 在发送前丢失,无法记录 info_catcher")
@@ -520,3 +559,47 @@ class NormalChat:
except Exception as e:
logger.error(f"[{self.stream_name}] 清理思考消息时出错: {e}")
traceback.print_exc()
# 获取最近回复记录的方法
def get_recent_replies(self, limit: int = 10) -> List[dict]:
"""获取最近的回复记录
Args:
limit: 最大返回数量默认10条
Returns:
List[dict]: 最近的回复记录列表,每项包含:
time: 回复时间戳
user_message: 用户消息内容
user_info: 用户信息(user_id, user_nickname)
response: 回复内容
is_mentioned: 是否被提及(@)
is_reference_reply: 是否为引用回复
timing: 各阶段耗时
"""
# 返回最近的limit条记录按时间倒序排列
return sorted(self.recent_replies[-limit:], key=lambda x: x["time"], reverse=True)
async def _check_switch_to_focus(self) -> None:
"""检查是否满足切换到focus模式的条件"""
if not self.on_switch_to_focus_callback:
return # 如果没有设置回调函数,直接返回
current_time = time.time()
time_threshold = 120 / global_config.focus_chat.auto_focus_threshold
reply_threshold = 6 * global_config.focus_chat.auto_focus_threshold
one_minute_ago = current_time - time_threshold
# 统计1分钟内的回复数量
recent_reply_count = sum(1 for reply in self.recent_replies if reply["time"] > one_minute_ago)
# print(111111111111111333333333333333333333333331111111111111111111111111111111111)
# print(recent_reply_count)
# 如果1分钟内回复数量大于8触发切换到focus模式
if recent_reply_count > reply_threshold:
logger.info(f"[{self.stream_name}] 检测到1分钟内回复数量({recent_reply_count})大于{reply_threshold}触发切换到focus模式")
try:
# 调用回调函数通知上层切换到focus模式
await self.on_switch_to_focus_callback()
except Exception as e:
logger.error(f"[{self.stream_name}] 触发切换到focus模式时出错: {e}\n{traceback.format_exc()}")

View File

@@ -18,13 +18,13 @@ class NormalChatGenerator:
# TODO: API-Adapter修改标记
self.model_reasoning = LLMRequest(
model=global_config.model.normal_chat_1,
temperature=0.7,
# temperature=0.7,
max_tokens=3000,
request_type="normal_chat_1",
)
self.model_normal = LLMRequest(
model=global_config.model.normal_chat_2,
temperature=global_config.model.normal_chat_2["temp"],
# temperature=global_config.model.normal_chat_2["temp"],
max_tokens=256,
request_type="normal_chat_2",
)

View File

@@ -32,6 +32,7 @@ from src.config.official_configs import (
FocusChatProcessorConfig,
MessageReceiveConfig,
MaimMessageConfig,
RelationshipConfig,
)
install(extra_lines=3)
@@ -143,6 +144,7 @@ class Config(ConfigBase):
bot: BotConfig
personality: PersonalityConfig
identity: IdentityConfig
relationship: RelationshipConfig
chat: ChatConfig
message_receive: MessageReceiveConfig
normal_chat: NormalChatConfig

View File

@@ -44,6 +44,13 @@ class IdentityConfig(ConfigBase):
identity_detail: list[str] = field(default_factory=lambda: [])
"""身份特征"""
@dataclass
class RelationshipConfig(ConfigBase):
"""关系配置类"""
give_name: bool = False
"""是否给其他人取名"""
@dataclass
class ChatConfig(ConfigBase):
@@ -115,11 +122,8 @@ class NormalChatConfig(ConfigBase):
class FocusChatConfig(ConfigBase):
"""专注聊天配置类"""
reply_trigger_threshold: float = 3.0
"""心流聊天触发阈值,越低越容易触发"""
default_decay_rate_per_second: float = 0.98
"""默认衰减率,越大衰减越快"""
auto_focus_threshold: float = 1.0
"""自动切换到专注聊天的阈值,越低越容易进入专注聊天"""
observation_context_size: int = 12
"""可观察到的最长上下文大小,超过这个值的上下文会被压缩"""

View File

@@ -264,17 +264,17 @@ class PersonInfoManager:
prompt_personality = individuality.get_prompt(x_person=2, level=1)
bot_name = individuality.personality.bot_nickname
qv_name_prompt = f"你是{bot_name}{prompt_personality}"
qv_name_prompt += f"现在你想给一个用户取一个昵称,用户的qq昵称是{user_nickname}"
qv_name_prompt = f"你是{bot_name}一个伪装成人类的AI你不能让别人发现这一点"
qv_name_prompt += f"现在你想给一个用户取一个昵称用户的qq昵称是{user_nickname}"
qv_name_prompt += f"用户的qq群昵称名是{user_cardname}"
if user_avatar:
qv_name_prompt += f"用户的qq头像是{user_avatar}"
if old_name:
qv_name_prompt += f"你之前叫他{old_name},是因为{old_reason}"
qv_name_prompt += f"\n其他取名的要求是:{request},不要太浮夸"
qv_name_prompt += f"\n其他取名的要求是:{request},不要太浮夸,简短,"
qv_name_prompt += (
"\n请根据以上用户信息想想你叫他什么比较好不要太浮夸请最好使用用户的qq昵称可以稍作修改"
"\n请根据以上用户信息想想你叫他什么比较好不要太浮夸请最好使用用户的qq昵称可以稍作修改优先使用原文。优先使用用户的qq昵称或者群昵称原文。"
)
if existing_names_str:

View File

@@ -18,13 +18,11 @@ nickname = "麦麦"
alias_names = ["麦叠", "牢麦"] #仅在 专注聊天 有效
[personality]
personality_core = "用一句话或几句话描述人格的核心特点" # 建议20字以内谁再写3000字小作文敲谁脑袋
personality_core = "是一个积极向上的女大学生" # 建议20字以内谁再写3000字小作文敲谁脑袋
personality_sides = [
"用一句话或几句话描述人格的一些细节",
"用一句话或几句话描述人格的一些细节",
"用一句话或几句话描述人格的一些细节",
"用一句话或几句话描述人格的一些细节",
"用一句话或几句话描述人格的一些细节",
]# 条数任意不能为0
# 身份特点
@@ -38,6 +36,9 @@ identity_detail = [
# 可以描述外贸,性别,身高,职业,属性等等描述
# 条数任意不能为0
[relationship]
give_name = true # 麦麦是否给其他人取名,关闭后无法使用禁言功能
[chat] #麦麦的聊天通用设置
chat_mode = "normal" # 聊天模式 —— 普通模式normal专注模式focus在普通模式和专注模式之间自动切换
# chat_mode = "focus"
@@ -78,11 +79,11 @@ at_bot_inevitable_reply = false # @bot 必然回复
talk_frequency_down_groups = [] #降低回复频率的群号码
[focus_chat] #专注聊天
reply_trigger_threshold = 3.0 # 专注聊天触发阈值,越低越容易进入专注聊天
default_decay_rate_per_second = 0.98 # 默认衰减率,越大衰减越快,越高越难进入专注聊天
auto_focus_threshold = 1 # 自动切换到专注聊天阈值,越低越容易进入专注聊天
consecutive_no_reply_threshold = 3 # 连续不回复的阈值,越低越容易结束专注聊天
think_interval = 1 # 思考间隔 单位秒
think_interval = 3 # 思考间隔 单位秒,可以有效减少消耗
observation_context_size = 15 # 观察到的最长上下文大小,建议15太短太长都会导致脑袋尖尖
compressed_length = 5 # 不能大于chat.observation_context_size,心流上下文压缩的最短压缩长度超过心流观察到的上下文长度会压缩最短压缩长度为5
@@ -90,14 +91,14 @@ compress_length_limit = 5 #最多压缩份数,超过该数值的压缩上下
[focus_chat_processor] # 专注聊天处理器打开可以实现更多功能但是会增加token消耗
self_identify_processor = true # 是否启用自我识别处理器
tool_use_processor = true # 是否启用工具使用处理器
working_memory_processor = true # 是否启用工作记忆处理器
tool_use_processor = false # 是否启用工具使用处理器
working_memory_processor = false # 是否启用工作记忆处理器
[expression]
# 表达方式
expression_style = "描述麦麦说话的表达风格,表达习惯"
enable_expression_learning = true # 是否启用表达学习
learning_interval = 300 # 学习间隔 单位秒
learning_interval = 600 # 学习间隔 单位秒
[emoji]