Files
life-echo/api/tests/test_interview_prompts.py
Kevin bb16d3a5c9 refactor(agents): 抽取阶段常量与对话上下文;快档 LLM;图片 prompt 可禁止回退
访谈与阶段
- 新增 app/agents/stage_constants.py:集中 CHAT_STAGES、章节分类/顺序、阶段到默认 memoir 类别等,与 MemoirState 默认槽位顺序对齐;减少散落在 prompts 内的重复常量。
- 新增 app/agents/chat/prompt_context.py:以 ChatPromptContext 汇总 guided 系统提示所需字段(阶段、槽位、轮次、人设、记忆证据、回复长度模式、背景声线、职业等),统一走 get_guided_conversation_prompt。
- 大幅收敛 app/agents/chat/prompts_conversation.py;调整 prompts.py、stage_prompts.py、stage_detection.py;同步 interview_agent、profile_agent、helpers 与 state_schema,使对话侧构造提示的方式一致、可测。

回忆录流水线
- memoir/prompts.py 删除已迁至 stage_constants / 独立模板的大段常量与图片占位相关逻辑;classification / extraction / fidelity / narrative agents 与 orchest(全量历史仍可用于计数,注入模型时按轮次与字符上限截断)、image_prompt_fallback_disabled。
- dependencies 增加 get_llm_provider_fast(LRU 缓存,可与默认共用密钥与 base_url)。

任务与编排
- memoir_tasks:prepare_batches 注入 llm_fast;开启独立快档模型时打结构化日志。
- chapter_cover_tasks、story_image_tasks:与图片 prompt / JSON 工具路径或策略变更对齐(import 与行为一致)。
- story_pipeline_sync 等小处同步。

其它核心
- langchain_llm、text_normalize 随上述调用链微调。

开发者体验
- .cursor/settings.json:启用 redis-development、postman 插件。

测试
- 新增 test_image_prompt_policy:覆盖「禁止回退」等图片 prompt 策略。
- 更新 test_interview_prompts、test_interview_reply_length、test_experience_regressions、test_json_and_memory_utils,匹配新常量位置、json_utils 与对话/长度行为。
2026-04-02 12:00:00 +08:00

198 lines
6.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""访谈提示词追问触发与性格Persona拼接回归。"""
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
from app.agents.chat.helpers import format_history_string
from app.agents.chat.personas import normalize_interview_persona
from app.agents.chat.prompts_conversation import (
get_guided_conversation_prompt,
get_opening_prompt,
)
def test_guided_prompt_contains_mandatory_followup_when_heuristic_matches():
p = get_guided_conversation_prompt(
current_stage="childhood",
empty_slots=["place", "people"],
filled_slots={},
user_message="厉害吧 那个女生叫娟娟",
conversation_turn_total=1,
same_topic_turns=1,
all_stages_coverage=None,
detected_user_stage="childhood",
user_profile_context="",
persona="default",
)
assert "本轮追问判定" in p
assert "本轮判定" in p
def test_guided_prompt_does_not_embed_raw_user_message_in_system_text():
p = get_guided_conversation_prompt(
current_stage="childhood",
empty_slots=["place"],
filled_slots={},
user_message="__USER_SECRET_PHRASE_XYZ__",
conversation_turn_total=0,
same_topic_turns=0,
all_stages_coverage=None,
detected_user_stage="childhood",
user_profile_context="",
persona="default",
)
assert "__USER_SECRET_PHRASE_XYZ__" not in p
def test_guided_prompt_persona_curious_guide():
p = get_guided_conversation_prompt(
current_stage="education",
empty_slots=["school"],
filled_slots={},
user_message="还行吧",
conversation_turn_total=0,
same_topic_turns=0,
all_stages_coverage=None,
detected_user_stage="education",
user_profile_context="",
persona="curious_guide",
)
assert "好奇引导" in p
def test_normalize_interview_persona_unknown_falls_back():
assert normalize_interview_persona("not_a_real_persona") == "default"
assert normalize_interview_persona("") == "default"
def test_guided_prompt_contains_memoir_orientation():
p = get_guided_conversation_prompt(
current_stage="childhood",
empty_slots=["place"],
filled_slots={},
user_message="后来我就去上班了",
conversation_turn_total=0,
same_topic_turns=0,
all_stages_coverage=None,
detected_user_stage="childhood",
user_profile_context="",
persona="default",
)
assert "对话方向" in p
assert "人生故事" in p
def test_guided_prompt_contains_memory_section_when_evidence():
p = get_guided_conversation_prompt(
current_stage="childhood",
empty_slots=["place"],
filled_slots={},
user_message="后来我就去上班了",
conversation_turn_total=0,
same_topic_turns=0,
all_stages_coverage=None,
detected_user_stage="childhood",
user_profile_context="",
persona="default",
memory_evidence_text="[摘要:rolling] 1990年生于上海。",
)
assert "相关记忆摘录" in p
assert "过往口述" in p
assert "1990年生于上海" in p
def test_guided_prompt_chit_chat_hint():
p = get_guided_conversation_prompt(
current_stage="childhood",
empty_slots=["place"],
filled_slots={},
user_message="今天天气真好哈哈",
conversation_turn_total=0,
same_topic_turns=0,
all_stages_coverage=None,
detected_user_stage="childhood",
user_profile_context="",
persona="default",
)
assert "偏闲聊" in p
def test_guided_prompt_reply_length_section_explicit_expanded():
p = get_guided_conversation_prompt(
current_stage="childhood",
empty_slots=["place"],
filled_slots={},
user_message="还行吧",
conversation_turn_total=0,
same_topic_turns=0,
all_stages_coverage=None,
detected_user_stage="childhood",
user_profile_context="",
persona="default",
reply_length_mode="expanded",
)
assert "本轮回复长度" in p
assert "当前档位expanded" in p
assert "expanded" in p
def test_guided_prompt_reply_length_explicit_brief():
"""档位由 Agent 的 ReplyPlan 传入prompt 不再自行推导。"""
p = get_guided_conversation_prompt(
current_stage="childhood",
empty_slots=["place"],
filled_slots={},
user_message="",
conversation_turn_total=0,
same_topic_turns=0,
all_stages_coverage=None,
detected_user_stage="childhood",
user_profile_context="",
persona="default",
reply_length_mode="brief",
)
assert "当前档位brief" in p
def test_guided_prompt_background_voice_military() -> None:
p = get_guided_conversation_prompt(
current_stage="childhood",
empty_slots=["place"],
filled_slots={},
user_message="后来我就去上班了",
conversation_turn_total=0,
same_topic_turns=0,
all_stages_coverage=None,
detected_user_stage="childhood",
user_profile_context="",
persona="default",
background_voice="military",
)
assert "背景语气:军队语境" in p
assert "先接住对方" in p
def test_opening_prompt_military_has_examples_note() -> None:
p = get_opening_prompt(
current_stage="childhood",
empty_slots_readable=["成长的地方"],
user_profile_context="",
persona="default",
background_voice="military",
)
assert "军队语境" in p
assert "(军队语境:简洁" in p or "军队语境" in p
def test_format_history_string_includes_system_for_debug_logs() -> None:
"""log_agent_payload 依赖本函数时需包含 System避免生产上丢失主 system prompt。"""
s = format_history_string(
[
SystemMessage(content="SYS_INSTRUCTIONS"),
HumanMessage(content="hi"),
AIMessage(content="hello"),
]
)
assert "System: SYS_INSTRUCTIONS" in s
assert "Human: hi" in s
assert "Assistant: hello" in s