feat: 现在使用工具调用来更新关系和心情而不是固定更新

This commit is contained in:
SengokuCola
2025-04-14 00:36:33 +08:00
parent d1bbda9e60
commit 7eba42f84a
16 changed files with 551 additions and 87 deletions

View File

@@ -21,6 +21,7 @@ from ...person_info.relationship_manager import relationship_manager
from ...chat.message_buffer import message_buffer
from src.plugins.respon_info_catcher.info_catcher import info_catcher_manager
from ...utils.timer_calculater import Timer
from src.do_tool.tool_use import ToolUser
# 定义日志配置
chat_config = LogConfig(
@@ -37,6 +38,7 @@ class ThinkFlowChat:
self.gpt = ResponseGenerator()
self.mood_manager = MoodManager.get_instance()
self.mood_manager.start_mood_update()
self.tool_user = ToolUser()
async def _create_thinking_message(self, message, chat, userinfo, messageinfo):
"""创建思考消息"""
@@ -110,14 +112,10 @@ class ThinkFlowChat:
"""处理表情包"""
if random() < global_config.emoji_chance:
emoji_raw = await emoji_manager.get_emoji_for_text(response)
# print("11111111111111")
# logger.info(emoji_raw)
if emoji_raw:
emoji_path, description = emoji_raw
emoji_cq = image_path_to_base64(emoji_path)
# logger.info(emoji_cq)
thinking_time_point = round(message.message_info.time, 2)
message_segment = Seg(type="emoji", data=emoji_cq)
@@ -136,19 +134,9 @@ class ThinkFlowChat:
is_emoji=True,
)
# logger.info("22222222222222")
message_manager.add_message(bot_message)
async def _update_using_response(self, message, response_set):
"""更新心流状态"""
stream_id = message.chat_stream.stream_id
chat_talking_prompt = ""
if stream_id:
chat_talking_prompt = get_recent_group_detailed_plain_text(
stream_id, limit=global_config.MAX_CONTEXT_SIZE, combine=True
)
await heartflow.get_subheartflow(stream_id).do_thinking_after_reply(response_set, chat_talking_prompt)
async def _update_relationship(self, message: MessageRecv, response_set):
"""更新关系情绪"""
@@ -224,13 +212,6 @@ class ThinkFlowChat:
logger.info("触发缓冲,已炸飞消息列")
return
# 计算回复意愿
# current_willing_old = willing_manager.get_willing(chat_stream=chat)
# # current_willing_new = (heartflow.get_subheartflow(chat.stream_id).current_state.willing - 5) / 4
# # current_willing = (current_willing_old + current_willing_new) / 2
# # 有点bug
# current_willing = current_willing_old
# 获取回复概率
is_willing = False
if reply_probability != 1:
@@ -266,7 +247,7 @@ class ThinkFlowChat:
except Exception as e:
logger.error(f"心流创建思考消息失败: {e}")
logger.debug(f"创建捕捉器thinking_id:{thinking_id}")
logger.trace(f"创建捕捉器thinking_id:{thinking_id}")
info_catcher = info_catcher_manager.get_info_catcher(thinking_id)
info_catcher.catch_decide_to_response(message)
@@ -279,7 +260,72 @@ class ThinkFlowChat:
logger.error(f"心流观察失败: {e}")
info_catcher.catch_after_observe(timing_results["观察"])
# 思考前使用工具
update_relationship = ""
try:
with Timer("思考前使用工具", timing_results):
tool_result = await self.tool_user.use_tool(message.processed_plain_text, message.message_info.user_info.user_nickname, chat)
# 如果工具被使用且获得了结果,将收集到的信息合并到思考中
collected_info = ""
if tool_result.get("used_tools", False):
# 如果有收集到的结构化信息,将其格式化后添加到当前思考中
if "structured_info" in tool_result:
info = tool_result["structured_info"]
# 处理记忆信息
if info["memory"]:
collected_info += "\n记忆相关信息:\n"
for mem in info["memory"]:
collected_info += f"- {mem['name']}: {mem['content']}\n"
# 处理日程信息
if info["schedule"]:
collected_info += "\n日程相关信息:\n"
for sch in info["schedule"]:
collected_info += f"- {sch['name']}: {sch['content']}\n"
# 处理知识信息
if info["knowledge"]:
collected_info += "\n知识相关信息:\n"
for know in info["knowledge"]:
collected_info += f"- {know['name']}: {know['content']}\n"
# 处理关系信息
if info["change_relationship"]:
collected_info += "\n关系相关信息:\n"
for rel in info["change_relationship"]:
collected_info += f"- {rel['name']}: {rel['content']}\n"
# print("11111111111111111111111111111")
update_relationship += rel["content"]
# print(f"11111111111111111111111111111{update_relationship}")
# 处理心情信息
if info["change_mood"]:
collected_info += "\n心情相关信息:\n"
for mood in info["change_mood"]:
collected_info += f"- {mood['name']}: {mood['content']}\n"
# 处理其他信息
if info["other"]:
collected_info += "\n其他相关信息:\n"
for other in info["other"]:
collected_info += f"- {other['name']}: {other['content']}\n"
except Exception as e:
logger.error(f"思考前工具调用失败: {e}")
logger.error(traceback.format_exc())
if update_relationship:
# ori_response = ",".join(response_set)
# print("22222222222222222222222222222")
stance, emotion = await self.gpt._get_emotion_tags_with_reason("你还没有回复", message.processed_plain_text,update_relationship)
await relationship_manager.calculate_update_relationship_value(
chat_stream=message.chat_stream, label=emotion, stance=stance
)
print("33333333333333333333333333333")
# 思考前脑内状态
try:
with Timer("思考前脑内状态", timing_results):
@@ -289,6 +335,7 @@ class ThinkFlowChat:
message_txt=message.processed_plain_text,
sender_name=message.message_info.user_info.user_nickname,
chat_stream=chat,
extra_info=collected_info
)
except Exception as e:
logger.error(f"心流思考前脑内状态失败: {e}")
@@ -323,19 +370,80 @@ class ThinkFlowChat:
except Exception as e:
logger.error(f"心流处理表情包失败: {e}")
# 更新心流
try:
with Timer("更新心流", timing_results):
await self._update_using_response(message, response_set)
except Exception as e:
logger.error(f"心流更新失败: {e}")
# 更新关系情绪
# 思考后使用工具
try:
with Timer("更新关系情绪", timing_results):
await self._update_relationship(message, response_set)
with Timer("思考后使用工具", timing_results):
tool_result = await self.tool_user.use_tool(message.processed_plain_text, message.message_info.user_info.user_nickname, chat)
# 如果工具被使用且获得了结果,将收集到的信息合并到思考中
collected_info = ""
if tool_result.get("used_tools", False):
# 如果有收集到的结构化信息,将其格式化后添加到当前思考中
if "structured_info" in tool_result:
info = tool_result["structured_info"]
# 处理记忆信息
if info["memory"]:
collected_info += "\n记忆相关信息:\n"
for mem in info["memory"]:
collected_info += f"- {mem['name']}: {mem['content']}\n"
# 处理日程信息
if info["schedule"]:
collected_info += "\n日程相关信息:\n"
for sch in info["schedule"]:
collected_info += f"- {sch['name']}: {sch['content']}\n"
# 处理知识信息
if info["knowledge"]:
collected_info += "\n知识相关信息:\n"
for know in info["knowledge"]:
collected_info += f"- {know['name']}: {know['content']}\n"
# 处理关系信息
if info["change_relationship"]:
collected_info += "\n关系相关信息:\n"
for rel in info["change_relationship"]:
collected_info += f"- {rel['name']}: {rel['content']}\n"
# 处理心情信息
if info["change_mood"]:
collected_info += "\n心情相关信息:\n"
for mood in info["change_mood"]:
collected_info += f"- {mood['name']}: {mood['content']}\n"
# 处理其他信息
if info["other"]:
collected_info += "\n其他相关信息:\n"
for other in info["other"]:
collected_info += f"- {other['name']}: {other['content']}\n"
except Exception as e:
logger.error(f"心流更新关系情绪失败: {e}")
logger.error(f"思考后工具调用失败: {e}")
logger.error(traceback.format_exc())
# 更新关系
if info["change_relationship"]:
ori_response = ",".join(response_set)
stance, emotion = await self.gpt._get_emotion_tags(ori_response, message.processed_plain_text,info["change_relationship"]["content"])
await relationship_manager.calculate_update_relationship_value(
chat_stream=message.chat_stream, label=emotion, stance=stance
)
try:
with Timer("思考后脑内状态更新", timing_results):
stream_id = message.chat_stream.stream_id
chat_talking_prompt = ""
if stream_id:
chat_talking_prompt = get_recent_group_detailed_plain_text(
stream_id, limit=global_config.MAX_CONTEXT_SIZE, combine=True
)
await heartflow.get_subheartflow(stream_id).do_thinking_after_reply(response_set, chat_talking_prompt,collected_info)
except Exception as e:
logger.error(f"心流思考后脑内状态更新失败: {e}")
# 回复后处理
await willing_manager.after_generate_reply_handle(message.message_info.message_id)

View File

@@ -225,6 +225,58 @@ class ResponseGenerator:
except Exception as e:
logger.debug(f"获取情感标签时出错: {e}")
return "中立", "平静" # 出错时返回默认值
async def _get_emotion_tags_with_reason(self, content: str, processed_plain_text: str, reason: str):
"""提取情感标签,结合立场和情绪"""
try:
# 构建提示词,结合回复内容、被回复的内容以及立场分析
prompt = f"""
请严格根据以下对话内容,完成以下任务:
1. 判断回复者对被回复者观点的直接立场:
- "支持":明确同意或强化被回复者观点
- "反对":明确反驳或否定被回复者观点
- "中立":不表达明确立场或无关回应
2. 从"开心,愤怒,悲伤,惊讶,平静,害羞,恐惧,厌恶,困惑"中选出最匹配的1个情感标签
3. 按照"立场-情绪"的格式直接输出结果,例如:"反对-愤怒"
4. 考虑回复者的人格设定为{global_config.personality_core}
对话示例:
被回复「A就是笨」
回复「A明明很聪明」 → 反对-愤怒
当前对话:
被回复:「{processed_plain_text}
回复:「{content}
原因:「{reason}
输出要求:
- 只需输出"立场-情绪"结果,不要解释
- 严格基于文字直接表达的对立关系判断
"""
# 调用模型生成结果
result, _, _ = await self.model_sum.generate_response(prompt)
result = result.strip()
# 解析模型输出的结果
if "-" in result:
stance, emotion = result.split("-", 1)
valid_stances = ["支持", "反对", "中立"]
valid_emotions = ["开心", "愤怒", "悲伤", "惊讶", "害羞", "平静", "恐惧", "厌恶", "困惑"]
if stance in valid_stances and emotion in valid_emotions:
return stance, emotion # 返回有效的立场-情绪组合
else:
logger.debug(f"无效立场-情感组合:{result}")
return "中立", "平静" # 默认返回中立-平静
else:
logger.debug(f"立场-情感格式错误:{result}")
return "中立", "平静" # 格式错误时返回默认值
except Exception as e:
logger.debug(f"获取情感标签时出错: {e}")
return "中立", "平静" # 出错时返回默认值
async def _process_response(self, content: str) -> List[str]:
"""处理响应内容,返回处理后的内容和情感标签"""