Files
life-echo/api/app/core/text_normalize.py
Kevin 309a051038 feat: 回忆录证据血缘与内部评测可追溯,顺带对齐本地评测台与 CI
数据库与模型:新增多版迁移(章节证据快照、对话血缘、记忆事实/时间线 lineage 等),把「成稿 ↔ 对话/记忆」的溯源信息落到表结构里。
业务链路:会话与 WS、回忆录/故事流水线、记忆写入与 enrichment 等跟着接上线索与快照;新增章节证据快照与评测侧 EvalTraceService 等模块,方便组评审用的证据包。
内部评测:自动化 run 与手工 memoir 评审共用可追溯证据;rubric/ judge 相关脚本与文档有配套调整。
app-eval-web:Memoir/实验详情里能展开看证据摘要与 evidence_trace(含对话轮次 id);Vite 代理与 development.sh 注入的 API 端口与当前默认内部评测端口一致,避免改端口后页面连错服务。
工程杂项:GitHub Actions / 仓库说明有更新;各适配器与支付/配额/plan 等多处为小改动或跟随主改动的收尾;新增/扩充了?
2026-04-08 15:37:09 +08:00

73 lines
2.2 KiB
Python
Raw Permalink 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.
"""口述/聊天输入的确定性规则与可选 LLM 纠错(供 conversation 与 memoir 共用)。"""
from __future__ import annotations
import json
import re
from typing import Any
from app.core.json_utils import extract_json_payload
from app.core.langchain_llm import invoke_json_object
from app.core.logging import get_logger
logger = get_logger(__name__)
_MEI_KANSHANG_RE = re.compile(r"美(?=看上[我你他她它])")
def apply_oral_rules(text: str) -> str:
"""确定性规则;保守替换,仅覆盖高频误听误打模式。"""
s = text or ""
if not s:
return s
return _MEI_KANSHANG_RE.sub("", s)
def llm_normalize_text(
text: str,
llm: Any,
*,
max_input_chars: int,
max_tokens: int,
agent_name: str,
) -> str | None:
"""仅修正明显错字与同音字,不增事实;失败返回 None。"""
if not llm or not (text or "").strip():
return None
t = (text or "").strip()
if len(t) > max_input_chars:
logger.debug(
"event=llm_text_normalize_skip reason=input_too_long len={} max={}",
len(t),
max_input_chars,
)
return None
prompt = f"""你是口述转写纠错助手。只修正明显的同音错别字、别字与标点,使句子通顺可读。
禁止增加事实、不补充细节、不摘要、不改写句式风格;不得新增人名、地名、数字、事件。
若原文已通顺或无法确定错误,则照抄输入。
【用户口述】
{t}
**JSON 输出**:只输出一个合法 JSON 对象。
{{"normalized_text": "纠错后的完整文本(与输入等意,仅修错字与标点)"}}
只输出 JSON不要其它文字。"""
try:
raw = invoke_json_object(
llm,
prompt,
max_tokens=max_tokens,
agent=agent_name,
)
data = json.loads(extract_json_payload(raw))
if not isinstance(data, dict):
return None
out = (data.get("normalized_text") or "").strip()
if not out:
return None
return out
except Exception as e:
logger.warning("llm_normalize_text 失败 {}: {}", agent_name, e)
return None