访谈与阶段 - 新增 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 与对话/长度行为。
73 lines
2.2 KiB
Python
73 lines
2.2 KiB
Python
"""口述/聊天输入的确定性规则与可选 LLM 纠错(供 conversation 与 memoir 共用)。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
import re
|
||
from typing import Any
|
||
|
||
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__)
|
||
|
||
_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
|