feat:将action记录存到数据库,并显示到prompt中

This commit is contained in:
SengokuCola
2025-06-04 18:14:16 +08:00
parent bb01a15468
commit 8c713a436c
16 changed files with 332 additions and 209 deletions

View File

@@ -24,6 +24,8 @@ from src.chat.normal_chat.normal_chat_planner import NormalChatPlanner
from src.chat.normal_chat.normal_chat_action_modifier import NormalChatActionModifier
from src.chat.normal_chat.normal_chat_expressor import NormalChatExpressor
from src.chat.focus_chat.replyer.default_replyer import DefaultReplyer
from src.common.database.database_model import ActionRecords
logger = get_logger("normal_chat")
@@ -442,7 +444,8 @@ class NormalChat:
logger.warning(f"[{self.stream_name}] 没有设置切换到focus聊天模式的回调函数无法执行切换")
return
else:
await self._check_switch_to_focus()
# await self._check_switch_to_focus()
pass
info_catcher.done_catch()
@@ -656,13 +659,16 @@ class NormalChat:
if action_handler:
# 执行动作
result = await action_handler.handle_action()
success = False
if result and isinstance(result, tuple) and len(result) >= 2:
# handle_action返回 (success: bool, message: str)
success, _ = result[0], result[1]
return success
success = result[0]
elif result:
# 如果返回了其他结果,假设成功
return True
success = True
return success
except Exception as e:
logger.error(f"[{self.stream_name}] 执行动作 {action_type} 失败: {e}")

View File

@@ -1,4 +1,5 @@
import json
from re import A
from typing import Dict, Any
from rich.traceback import install
from src.llm_models.utils_model import LLMRequest
@@ -115,13 +116,6 @@ class NormalChatPlanner:
}
# 构建normal_chat的上下文 (使用与normal_chat相同的prompt构建方法)
# chat_context = await prompt_builder.build_prompt_normal(
# enable_planner=True,
# message_txt=message.processed_plain_text,
# sender_name=sender_name,
# chat_stream=message.chat_stream,
# )
message_list_before_now = get_raw_msg_before_timestamp_with_chat(
chat_id=message.chat_stream.stream_id,
timestamp=time.time(),
@@ -134,6 +128,7 @@ class NormalChatPlanner:
merge_messages=False,
timestamp_mode="relative",
read_mark=0.0,
show_actions=True,
)
# 构建planner的prompt
@@ -155,7 +150,6 @@ class NormalChatPlanner:
try:
content, reasoning_content, model_name = await self.planner_llm.generate_response(prompt)
logger.info(f"{self.log_prefix}规划器原始提示词: {prompt}")
logger.info(f"{self.log_prefix}规划器原始响应: {content}")
@@ -206,7 +200,21 @@ class NormalChatPlanner:
f"{self.log_prefix}规划后恢复到默认动作集, 当前可用: {list(self.action_manager.get_using_actions().keys())}"
)
action_result = {"action_type": action, "action_data": action_data, "reasoning": reasoning}
# 构建 action 记录
action_record = {
"action_type": action,
"action_data": action_data,
"reasoning": reasoning,
"timestamp": time.time(),
"model_name": model_name if 'model_name' in locals() else None
}
action_result = {
"action_type": action,
"action_data": action_data,
"reasoning": reasoning,
"action_record": json.dumps(action_record, ensure_ascii=False)
}
plan_result = {
"action_result": action_result,
@@ -228,17 +236,25 @@ class NormalChatPlanner:
action_options_text = ""
# 添加特殊的change_to_focus_chat动作
action_options_text += "action_name: change_to_focus_chat\n"
action_options_text += "动作:change_to_focus_chat\n"
action_options_text += (
" 描述当聊天变得热烈、自己回复条数很多或需要深入交流时使用正常回复消息并切换到focus_chat模式\n"
"该动作的描述当聊天变得热烈、自己回复条数很多或需要深入交流时使用正常回复消息并切换到focus_chat模式\n"
)
action_options_text += " 参数:\n"
action_options_text += " 动作要求:\n"
action_options_text += " - 聊天上下文中自己的回复条数较多超过3-4条\n"
action_options_text += " - 对话进行得非常热烈活跃\n"
action_options_text += " - 用户表现出深入交流的意图\n"
action_options_text += " - 话题需要更专注和深入的讨论\n\n"
action_options_text += "使用该动作的场景:\n"
action_options_text += "- 聊天上下文中自己的回复条数较多超过3-4条\n"
action_options_text += "- 对话进行得非常热烈活跃\n"
action_options_text += "- 用户表现出深入交流的意图\n"
action_options_text += "- 话题需要更专注和深入的讨论\n\n"
action_options_text += "输出要求:\n"
action_options_text += "{{"
action_options_text += " \"action\": \"change_to_focus_chat\""
action_options_text += "}}\n\n"
for action_name, action_info in current_available_actions.items():
action_description = action_info.get("description", "")
action_parameters = action_info.get("parameters", {})

View File

@@ -184,6 +184,7 @@ class PromptBuilder:
merge_messages=False,
timestamp_mode="relative",
read_mark=0.0,
show_actions=True,
)
# 关键词检测与反应