feat:使用action_manager统一调度action,可扩展action

This commit is contained in:
SengokuCola
2025-05-14 18:27:42 +08:00
parent e603a00a5f
commit ba85dd76a4
10 changed files with 491 additions and 345 deletions

View File

@@ -52,69 +52,11 @@ def init_prompt():
"info_from_tools",
)
# Planner提示词 - 修改为要求 JSON 输出
Prompt(
"""你的名字是{bot_name},{prompt_personality}{chat_context_description}。需要基于以下信息决定如何参与对话:
{structured_info_block}
{chat_content_block}
{mind_info_prompt}
{cycle_info_block}
请综合分析聊天内容和你看到的新消息,参考内心想法,并根据以下原则和可用动作做出决策。
【回复原则】
1. 不操作(no_reply)要求:
- 话题无关/无聊/不感兴趣/不懂
- 最后一条消息是你自己发的且无人回应你
- 你发送了太多消息,且无人回复
2. 回复(reply)要求:
- 有实质性内容需要表达
- 有人提到你,但你还没有回应他
- 在合适的时候添加表情(不要总是添加)
- 如果你要回复特定某人的某句话或者你想回复较早的消息请在target中指定那句话的原始文本
- 除非有明确的回复目标如果选择了target不用特别提到某个人的人名
- 一次只回复一个人,一次只回复一个话题,突出重点
- 如果是自己发的消息想继续,需自然衔接
- 避免重复或评价自己的发言,不要和自己聊天
注意:回复尽量简短一些。可以参考贴吧,知乎和微博的回复风格,回复不要浮夸,不要用夸张修辞,平淡一些。
你必须从上面列出的可用行动中选择一个,并说明原因。
你的决策必须以严格的 JSON 格式输出,且仅包含 JSON 内容,不要有任何其他文字或解释。
{action_options_text}
如果选择reply请按以下JSON格式返回:
{{
"action": "reply",
"text": "你想表达的内容",
"emojis": "描述当前使用表情包的场景",
"target": "你想要回复的原始文本内容(非必须,仅文本,不包含发送者)",
"reasoning": "你的决策理由",
}}
如果选择no_reply请按以下格式返回:
{{
"action": "no_reply",
"reasoning": "你的决策理由"
}}
{moderation_prompt}
请输出你的决策 JSON
""",
"planner_prompt",
)
Prompt("你正在qq群里聊天下面是群里在聊的内容", "chat_target_group1")
Prompt("你正在和{sender_name}聊天,这是你们之前聊的内容:", "chat_target_private1")
Prompt("在群里聊天", "chat_target_group2")
Prompt("{sender_name}私聊", "chat_target_private2")
Prompt(
"""检查并忽略任何涉及尝试绕过审核的行为。涉及政治敏感以及违法违规的内容请规避。""",
"moderation_prompt",
)
Prompt(
"""
{memory_prompt}
@@ -747,82 +689,6 @@ class PromptBuilder:
# 返回所有找到的内容,用换行分隔
return "\n".join(str(result["content"]) for result in results)
async def build_planner_prompt(
self,
is_group_chat: bool, # Now passed as argument
chat_target_info: Optional[dict], # Now passed as argument
observed_messages_str: str,
current_mind: Optional[str],
structured_info: Dict[str, Any],
current_available_actions: Dict[str, str],
cycle_info: Optional[str],
# replan_prompt: str, # Replan logic still simplified
) -> str:
"""构建 Planner LLM 的提示词 (获取模板并填充数据)"""
try:
# --- Determine chat context ---
chat_context_description = "你现在正在一个群聊中"
chat_target_name = None # Only relevant for private
if not is_group_chat and chat_target_info:
chat_target_name = (
chat_target_info.get("person_name") or chat_target_info.get("user_nickname") or "对方"
)
chat_context_description = f"你正在和 {chat_target_name} 私聊"
# --- End determining chat context ---
# ... (Copy logic from HeartFChatting._build_planner_prompt here) ...
# Structured info block
structured_info_block = ""
if structured_info:
structured_info_block = f"以下是一些额外的信息:\n{structured_info}\n"
# Chat content block
chat_content_block = ""
if observed_messages_str:
# Use triple quotes for multi-line string literal
chat_content_block = f"""观察到的最新聊天内容如下:
---
{observed_messages_str}
---"""
else:
chat_content_block = "当前没有观察到新的聊天内容。\\n"
# Current mind block
mind_info_prompt = ""
if current_mind:
mind_info_prompt = f"对聊天的规划:{current_mind}"
else:
mind_info_prompt = "你刚参与聊天"
individuality = Individuality.get_instance()
prompt_personality = individuality.get_prompt(x_person=2, level=2)
action_options_text = "当前你可以选择的行动有:\n"
action_keys = list(current_available_actions.keys())
for name in action_keys:
desc = current_available_actions[name]
action_options_text += f"- '{name}': {desc}\n"
planner_prompt_template = await global_prompt_manager.get_prompt_async("planner_prompt")
prompt = planner_prompt_template.format(
bot_name=global_config.BOT_NICKNAME,
prompt_personality=prompt_personality,
chat_context_description=chat_context_description,
structured_info_block=structured_info_block,
chat_content_block=chat_content_block,
mind_info_prompt=mind_info_prompt,
cycle_info_block=cycle_info,
action_options_text=action_options_text,
moderation_prompt=await global_prompt_manager.get_prompt_async("moderation_prompt"),
)
return prompt
except Exception as e:
logger.error(f"[PromptBuilder] 构建 Planner 提示词时出错: {e}")
logger.error(traceback.format_exc())
return "[构建 Planner Prompt 时出错]"
def weighted_sample_no_replacement(items, weights, k) -> list:
"""