WIP: memory system improvements (in progress)
Interview/chat prompt layers, reply planner, style profiles, memory injection, interview meta store, and related tests. Work not finished. Made-with: Cursor
This commit is contained in:
@@ -3,7 +3,9 @@
|
||||
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
|
||||
|
||||
from app.agents.chat.interview_state_hints import (
|
||||
AUTOBIOGRAPHICAL_BOUNDARY_FALLBACK_ZH,
|
||||
DUPLICATE_QUESTION_GUARD_FALLBACK_ZH,
|
||||
apply_autobiographical_boundary_guard,
|
||||
apply_duplicate_question_guard,
|
||||
extract_scene_cues,
|
||||
segments_are_only_duplicate_guard_fallback,
|
||||
@@ -16,6 +18,7 @@ from app.agents.state_schema import (
|
||||
)
|
||||
from app.agents.chat.helpers import format_history_string
|
||||
from app.agents.chat.personas import normalize_interview_persona
|
||||
from app.agents.chat.output_rules import chat_output_rules
|
||||
from app.agents.chat.prompts_conversation import (
|
||||
get_guided_conversation_prompt,
|
||||
get_opening_prompt,
|
||||
@@ -217,8 +220,7 @@ def test_guided_prompt_contains_memory_section_when_evidence():
|
||||
persona="default",
|
||||
memory_evidence_text="[摘要:rolling] 1990年生于上海。",
|
||||
)
|
||||
assert "相关记忆摘录" in p
|
||||
assert "过往口述" in p
|
||||
assert "记忆线索" in p or "追问角度" in p
|
||||
assert "1990年生于上海" in p
|
||||
|
||||
|
||||
@@ -368,3 +370,92 @@ def test_format_history_string_omit_system_body() -> None:
|
||||
assert "System: <omitted total_len=16" in s
|
||||
assert "sha12=" in s
|
||||
assert "Human: hi" in s
|
||||
|
||||
|
||||
def test_guided_prompt_includes_identity_boundary_hard_rules() -> None:
|
||||
p = get_guided_conversation_prompt(
|
||||
current_stage="childhood",
|
||||
empty_slots=["place"],
|
||||
filled_slots={},
|
||||
detected_user_stage="childhood",
|
||||
user_profile_context="",
|
||||
persona="default",
|
||||
)
|
||||
assert "身份边界" in p
|
||||
assert "真实人生传记" in p
|
||||
assert "共同回忆" in p
|
||||
assert "泛指" in p
|
||||
|
||||
|
||||
def test_guided_prompt_blocks_using_user_context_as_assistant_identity() -> None:
|
||||
p = get_guided_conversation_prompt(
|
||||
current_stage="childhood",
|
||||
empty_slots=["place"],
|
||||
filled_slots={},
|
||||
detected_user_stage="childhood",
|
||||
user_profile_context="成长地:上海",
|
||||
persona="default",
|
||||
)
|
||||
assert "我是上海人" in p
|
||||
assert "不能把用户的成长地答成" in p
|
||||
assert "你刚提到上海" in p or "你之前说过那段童年" in p
|
||||
|
||||
|
||||
def test_chat_output_rules_bans_assistant_autobiography() -> None:
|
||||
rules = chat_output_rules()
|
||||
assert "禁止" in rules
|
||||
assert "声称助手本人" in rules or "助手本人" in rules
|
||||
assert "共同回忆" in rules
|
||||
assert "你是哪里人" in rules
|
||||
assert "你刚提到" in rules
|
||||
|
||||
|
||||
def test_autobiographical_boundary_guard_replaces_crush_claim() -> None:
|
||||
out, touched = apply_autobiographical_boundary_guard(
|
||||
["是啊,朱丽叶就是我当时暗恋的女生。"]
|
||||
)
|
||||
assert touched is True
|
||||
assert out == [AUTOBIOGRAPHICAL_BOUNDARY_FALLBACK_ZH]
|
||||
|
||||
|
||||
def test_autobiographical_boundary_guard_replaces_childhood_claim() -> None:
|
||||
out, touched = apply_autobiographical_boundary_guard(
|
||||
["我小时候也演过这个,还挺紧张的。"]
|
||||
)
|
||||
assert touched is True
|
||||
assert out == [AUTOBIOGRAPHICAL_BOUNDARY_FALLBACK_ZH]
|
||||
|
||||
|
||||
def test_autobiographical_boundary_guard_allows_generic_empathy() -> None:
|
||||
safe = [
|
||||
"我能想象那会儿站在台上,手心里全是汗。",
|
||||
"换作很多人可能也会记很久。",
|
||||
]
|
||||
out, touched = apply_autobiographical_boundary_guard(safe)
|
||||
assert touched is False
|
||||
assert out == safe
|
||||
|
||||
|
||||
def test_autobiographical_boundary_guard_mixed_segments() -> None:
|
||||
out, touched = apply_autobiographical_boundary_guard(
|
||||
["嗯,你刚才那段我接住了。", "我小时候也演过。"]
|
||||
)
|
||||
assert touched is True
|
||||
assert out[0] == "嗯,你刚才那段我接住了。"
|
||||
assert out[1] == AUTOBIOGRAPHICAL_BOUNDARY_FALLBACK_ZH
|
||||
|
||||
|
||||
def test_autobiographical_boundary_guard_catches_iyan_role_without_demo() -> None:
|
||||
out, touched = apply_autobiographical_boundary_guard(
|
||||
["那次话剧里我演罗密欧,对手戏挺难的。"]
|
||||
)
|
||||
assert touched is True
|
||||
assert out == [AUTOBIOGRAPHICAL_BOUNDARY_FALLBACK_ZH]
|
||||
|
||||
|
||||
def test_autobiographical_boundary_guard_allows_wo_yanshi_demo() -> None:
|
||||
out, touched = apply_autobiographical_boundary_guard(
|
||||
["我演示一下这个按钮怎么点。"]
|
||||
)
|
||||
assert touched is False
|
||||
assert out == ["我演示一下这个按钮怎么点。"]
|
||||
|
||||
Reference in New Issue
Block a user