Chat 访谈 - 新增 persona 系统(default / warm_listener / curious_guide)与 background_voice 语气层 - 回复长度由 compute_reply_plan 统一决策(brief / standard / expanded),融合信息密度启发式 - 输入净稿(input_normalize):编排层可选 rules/llm 归一用户口语后再喂模型与记忆检索 - 记忆证据注入:按用户话检索 memory evidence 并注入 prompt Memoir 回忆录 - 口述归一(oral_normalize):segment 原文保留,story 管线取派生净稿作叙事输入 - segment 入队批次门闸:累计字数 + 最长等待秒数,减少零碎提交 - fidelity_check / prompts / narrative_agent 微调 - Alembic 0005:清理跨章节 story 外键 Infra - Dockerfile 加入 ffmpeg - pyproject.toml 新增依赖并同步 uv.lock - .env.example / .env.production 补全新配置项 Tests - 新增 test_background_voice、test_chat_input_normalize、test_experience_regressions - 扩展 test_interview_prompts、test_interview_reply_length、test_story_route_oral_invariant Made-with: Cursor
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.features.memoir.memoir_images.json_payload 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
|