访谈与阶段 - 新增 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 与对话/长度行为。
131 lines
4.6 KiB
Python
131 lines
4.6 KiB
Python
"""
|
||
FidelityCheckAgent:比较「用户口述」与叙事 JSON 输出,判定是否存在明显编造或越界。
|
||
续写合并(append)时传入 `existing_canonical_markdown`,将已有故事正文一并视为允许来源。
|
||
失败时由流水线回退(见 story_pipeline_sync):续写为「已有 + 口述」,新建为口述原文。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
import re
|
||
from typing import Any
|
||
|
||
from app.core.config import settings
|
||
from app.core.langchain_llm import invoke_json_object
|
||
from app.core.logging import get_logger
|
||
from app.core.json_utils import extract_json_payload
|
||
|
||
logger = get_logger(__name__)
|
||
|
||
# 生成稿中出现的四位年份,若口述中未出现同串,仅打日志(不误杀)
|
||
_YEAR_4_RE = re.compile(r"(?<!\d)(19|20)\d{2}(?!\d)")
|
||
|
||
|
||
def _log_suspicious_years_not_in_oral(oral_text: str, narrative_json: str) -> None:
|
||
oral = oral_text or ""
|
||
gen = narrative_json or ""
|
||
for m in _YEAR_4_RE.finditer(gen):
|
||
y = m.group(0)
|
||
if y not in oral:
|
||
logger.debug(
|
||
"event=fidelity_heuristic_year_not_in_oral year={} oral_len={} gen_len={}",
|
||
y,
|
||
len(oral),
|
||
len(gen),
|
||
)
|
||
|
||
|
||
class FidelityCheckAgent:
|
||
"""叙事忠实度检查(json_object);失败时上层应回退为口述原文。"""
|
||
|
||
def passes(
|
||
self,
|
||
*,
|
||
oral_text: str,
|
||
narrative_json: str,
|
||
llm: Any,
|
||
existing_canonical_markdown: str | None = None,
|
||
) -> bool:
|
||
if not llm or not settings.memoir_fidelity_check_enabled:
|
||
return True
|
||
oral = (oral_text or "").strip()
|
||
gen = (narrative_json or "").strip()
|
||
if not oral or not gen:
|
||
return True
|
||
existing = (existing_canonical_markdown or "").strip()
|
||
_log_suspicious_years_not_in_oral(oral, gen)
|
||
pass_rules = """## 以下行为是 pass(不算编造)
|
||
- 口语转书面语(删语气词、调语序、用成语替换口语)
|
||
- 过渡句与衔接句(「那段日子」「回想起来」等,不引入新实体)
|
||
- 基于口述已有情感的渲染与书面化(如口述说「难受」,改写为「心里像堵了一团棉花」,但不能新增具体场景细节)
|
||
- 合并同义重复表述
|
||
- 纠正明显的语音识别或同音错别字
|
||
|
||
## 以下行为是 fail(算编造)
|
||
- 新增口述中**没有**的具体人名、地名、时间、数字、对话原文
|
||
- 补全口述未说明的结果或结局(如「最终没考上」)
|
||
- 把系统摘录/档案里才有的信息写成用户亲口经历
|
||
- 虚构具体场景细节来「让文章更好看」"""
|
||
|
||
if existing:
|
||
prompt = f"""你是事实核对员。当前为**续写合并**:生成稿应保留「已有故事正文」中的事实并融入「本轮口述」中的新事实。
|
||
|
||
【用户本轮口述】
|
||
{oral[:8000]}
|
||
|
||
【已有故事正文】(已落库,出现于此处的内容**不算**编造)
|
||
{existing[:12000]}
|
||
|
||
【模型生成的叙事】
|
||
{gen[:16000]}
|
||
|
||
{pass_rules}
|
||
|
||
判断:生成稿是否出现**既不在本轮口述、也不在已有正文**的具体新实体或虚构细节?
|
||
若内容可归因于上述两个来源的合理书面化整理,pass=true。
|
||
|
||
**JSON 输出**:只输出一个合法 JSON 对象。
|
||
{{"pass": true, "reason": null}}
|
||
或
|
||
{{"pass": false, "reason": "一句话说明"}}
|
||
|
||
只输出 JSON,不要其它文字。"""
|
||
else:
|
||
prompt = f"""你是事实核对员。比较用户口述与模型生成的叙事。
|
||
|
||
【用户口述】
|
||
{oral[:8000]}
|
||
|
||
【模型生成的叙事】
|
||
{gen[:16000]}
|
||
|
||
{pass_rules}
|
||
|
||
判断:生成稿是否出现口述中**明显没有**的具体新实体或虚构细节?
|
||
若仅为口述的书面化整理(含文学性改写、情感渲染、过渡衔接),pass=true。
|
||
|
||
**JSON 输出**:只输出一个合法 JSON 对象。
|
||
{{"pass": true, "reason": null}}
|
||
或
|
||
{{"pass": false, "reason": "一句话说明"}}
|
||
|
||
只输出 JSON,不要其它文字。"""
|
||
try:
|
||
raw = invoke_json_object(
|
||
llm,
|
||
prompt,
|
||
max_tokens=settings.memoir_fidelity_check_max_tokens,
|
||
agent="FidelityCheckAgent.passes",
|
||
)
|
||
data = json.loads(extract_json_payload(raw))
|
||
ok = bool(data.get("pass", True))
|
||
if not ok:
|
||
logger.warning(
|
||
"event=fidelity_check_fail reason={}",
|
||
(data.get("reason") or "")[:200],
|
||
)
|
||
return ok
|
||
except Exception as e:
|
||
logger.warning("FidelityCheckAgent 解析失败,放行: {}", e)
|
||
return True
|