Merge branch 'feat/improve-agent-prompt'

This commit is contained in:
penghanyuan
2026-03-01 10:12:23 +01:00
parent a69d5c625c
commit c1e2fb31a0
11 changed files with 644 additions and 65 deletions

View File

@@ -2,6 +2,7 @@
回忆录整理 Agent 提示词模板
"""
import json
from typing import Optional
# 章节分类映射
CHAPTER_CATEGORIES = {
@@ -177,42 +178,89 @@ def get_state_extraction_prompt(user_message: str, current_stage: str, stage_slo
"""
def get_creative_title_prompt(stage: str, emotion: str, slots: dict) -> str:
"""生成有创意的章节标题"""
def _build_age_hint(stage: str, birth_year: Optional[int] = None) -> str:
"""根据人生阶段和出生年份推算大致年龄区间"""
if not birth_year:
return ""
stage_age_ranges = {
"childhood": (0, 12),
"education": (6, 22),
"career": (18, 60),
"career_early": (18, 30),
"career_achievement": (25, 55),
"career_challenge": (20, 55),
"family": (20, 60),
"belief": (30, 70),
"beliefs": (30, 70),
"summary": (50, 80),
}
age_range = stage_age_ranges.get(stage)
if not age_range:
return ""
year_start = birth_year + age_range[0]
year_end = birth_year + age_range[1]
return f"大约 {year_start}-{year_end} 年({age_range[0]}-{age_range[1]} 岁)"
def get_creative_title_prompt(
stage: str,
emotion: str,
slots: dict,
user_profile: str = "",
birth_year: Optional[int] = None,
) -> str:
"""生成有创意的章节标题,包含年龄/时间信息"""
age_hint = _build_age_hint(stage, birth_year)
profile_section = f"\n用户基本信息:\n{user_profile}" if user_profile else ""
time_section = f"\n时间参考:{age_hint}" if age_hint else ""
return f"""{get_system_prompt()}
请根据阶段和情绪生成 1 个有创意的章节标题。
阶段:{stage}
情绪:{emotion}
可用信息:{slots}
可用信息:{slots}{profile_section}{time_section}
要求:
1. 标题 12-18 字以内
1. 标题格式:「时间标注 · 标题正文」
- 时间标注用年龄或年代表示,如"6-12岁""1980年代""二十出头"
- 标题正文 12-18 字以内
2. 情绪 + 人生阶段 + 意象
3. 示例风格:
- 《那个夏天,我第一次离开家
- 《在陌生城市站稳脚跟
- 《不是所有选择都被理解
- 《慢下来,人生开始发声》
- 《6-12岁 · 那条巷子尽头的蝉鸣
- 《18岁 · 第一次离开家的夏天
- 《25-35岁 · 在陌生城市站稳脚跟
- 《四十不惑 · 慢下来,人生开始发声》
- 《1990年代 · 不是所有选择都被理解》
只输出标题文字,不要加引号或其他内容
只输出标题文字,不要加引号或书名号
"""
def get_narrative_prompt(stage: str, slots: dict, new_content: str, existing_content: str = "") -> str:
def get_narrative_prompt(
stage: str,
slots: dict,
new_content: str,
existing_content: str = "",
user_profile: str = "",
birth_year: Optional[int] = None,
) -> str:
"""将新对话改写为叙述(只输出新内容的改写,不重复已有内容)"""
# 只取已有内容的末尾作为衔接上下文
context_tail = ""
if existing_content:
context_tail = existing_content[-300:] if len(existing_content) > 300 else existing_content
context_section = f"\n\n【衔接上下文(已有内容的末尾,仅供参考衔接,不要重复)】:\n{context_tail}" if context_tail else ""
profile_section = f"\n\n用户基本信息:\n{user_profile}" if user_profile else ""
age_hint = _build_age_hint(stage, birth_year)
time_section = f"\n时间参考:{age_hint}" if age_hint else ""
return f"""{get_system_prompt()}
请将以下新的对话内容改写为第一人称文学叙述。
阶段:{stage}
可用信息:{slots}
可用信息:{slots}{profile_section}{time_section}
新的对话内容:
{new_content}
@@ -225,6 +273,7 @@ def get_narrative_prompt(stage: str, slots: dict, new_content: str, existing_con
4. 如果有衔接上下文,确保新内容与之自然衔接(语气、时间线连贯)
5. 语气自然,有情绪
6. 在适合配图的地方插入图片占位符
7. 如果有用户的基本信息(出生地、成长地等),在叙述中自然融入地域文化和时代背景
## 图片占位符格式
在描述场景、人物、重要时刻的段落后,插入图片占位符,格式为: