refactor(agents): 抽取阶段常量与对话上下文;快档 LLM;图片 prompt 可禁止回退

访谈与阶段
- 新增 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 与对话/长度行为。
This commit is contained in:
Kevin
2026-04-02 12:00:00 +08:00
parent 43ef260ae2
commit bb16d3a5c9
42 changed files with 894 additions and 580 deletions

View File

@@ -0,0 +1,9 @@
"""Shared image style template for story/chapter image tasks (decoupled from narrative prompts)."""
IMAGE_PLACEHOLDER_TEMPLATE = (
"温暖怀旧风格,年代感复古色调,柔和光影,朴素温馨氛围,安静治愈,低饱和度,"
"质感柔和细腻,简约构图,充满岁月沉淀感与故事感,高清唯美插画封面,不要包含文字,"
"要适合老年人审美,画面要真实可信、让老年人产生共鸣与代入感,"
"场景环境、建筑风格、服饰器物必须严格符合所述时代背景和地域特色,"
"有朦胧怀旧的年代感。"
)

View File

@@ -1,23 +0,0 @@
import re
_MARKDOWN_JSON_FENCE_RE = re.compile(
r"^\s*```(?:json)?\s*(.*?)\s*```\s*$",
re.IGNORECASE | re.DOTALL,
)
def extract_json_payload(raw_response: str | None) -> str:
cleaned = (raw_response or "").strip()
fenced_match = _MARKDOWN_JSON_FENCE_RE.match(cleaned)
if fenced_match:
cleaned = fenced_match.group(1).strip()
if cleaned.startswith("{") and cleaned.endswith("}"):
return cleaned
start = cleaned.find("{")
end = cleaned.rfind("}")
if start != -1 and end != -1 and end > start:
return cleaned[start : end + 1].strip()
return cleaned

View File

@@ -4,7 +4,7 @@ from typing import Any
from app.features.memoir.asset_resolver import strip_image_placeholders
from .json_payload import extract_json_payload
from app.core.json_utils import extract_json_payload
from .schema import IMAGE_STATUS_PENDING
PLACEHOLDER_RE = re.compile(

View File

@@ -2,10 +2,11 @@ import json
import re
from typing import Any, Optional
from app.core.config import settings
from app.core.langchain_llm import invoke_json_object
from app.core.logging import get_logger
from .json_payload import extract_json_payload
from app.core.json_utils import extract_json_payload
from .settings import MemoirImageSettings
logger = get_logger(__name__)
@@ -83,12 +84,19 @@ class MemoirImagePromptService:
"prompt_context": prompt_context,
}
except Exception as exc:
if settings.image_prompt_fallback_disabled:
raise
logger.warning(
"图片 prompt 生成回退到默认模板: chapter_category={}, title={}, error={}",
chapter_category,
chapter_title,
exc,
)
elif settings.image_prompt_fallback_disabled:
raise RuntimeError(
"MemoirImagePromptService.build_prompt requires LLM when "
"image_prompt_fallback_disabled is True"
)
return {
"prompt": _ensure_style_in_prompt(
@@ -112,6 +120,13 @@ class MemoirImagePromptService:
context_excerpt: str,
) -> dict[str, str]:
"""生成章节封面图的 image-generation prompt。"""
excerpt = (context_excerpt or "").strip()
if settings.image_prompt_fallback_disabled and not excerpt:
raise RuntimeError(
"Chapter cover prompt requires non-empty context_excerpt when "
"image_prompt_fallback_disabled is True"
)
style = self.CATEGORY_STYLE_MAP.get(
chapter_category, self.settings.default_style
)
@@ -120,7 +135,7 @@ class MemoirImagePromptService:
llm_input = {
"chapter_title": chapter_title,
"chapter_category": chapter_category,
"context_excerpt": context_excerpt,
"context_excerpt": excerpt,
"default_style": style,
"default_size": self.settings.default_size,
}
@@ -150,18 +165,25 @@ class MemoirImagePromptService:
"prompt_context": prompt_context,
}
except Exception as exc:
if settings.image_prompt_fallback_disabled:
raise
logger.warning(
"封面 prompt 生成回退到默认模板: chapter_category={}, title={}, error={}",
chapter_category,
chapter_title,
exc,
)
elif settings.image_prompt_fallback_disabled:
raise RuntimeError(
"MemoirImagePromptService.build_cover_prompt requires LLM when "
"image_prompt_fallback_disabled is True"
)
return {
"prompt": _ensure_style_in_prompt(
self._build_cover_fallback_prompt(
chapter_category=chapter_category,
context_excerpt=context_excerpt,
context_excerpt=excerpt,
style=style,
),
style,
@@ -171,6 +193,105 @@ class MemoirImagePromptService:
"prompt_context": prompt_context,
}
def build_story_primary_prompt(
self,
story_title: str,
story_stage: str | None,
prompt_brief: str,
style_profile: str | None,
) -> dict[str, str]:
"""生成 story 主插图的 image-generation promptLLM / fallback 策略与章节配图一致)。
`story_stage` 与 `Story.stage` 一致:通常为章节 category如 career_early也可能为
访谈五阶段名childhood/career/…);二者都参与默认画风推断。
"""
from app.agents.stage_constants import STAGE_TO_DEFAULT_CATEGORY
brief = (prompt_brief or "").strip()
if settings.image_prompt_fallback_disabled and not brief:
raise RuntimeError(
"Story image prompt requires non-empty prompt_brief when "
"image_prompt_fallback_disabled is True"
)
stage_key = (story_stage or "").strip()
if stage_key in self.CATEGORY_STYLE_MAP:
cat = stage_key
else:
cat = STAGE_TO_DEFAULT_CATEGORY.get(stage_key, "summary")
explicit_style = (style_profile or "").strip()
style = explicit_style or self.CATEGORY_STYLE_MAP.get(
cat, self.settings.default_style
)
prompt_context = f"story:{stage_key}:{story_title}"
llm_input = {
"story_title": story_title,
"story_stage": stage_key,
"prompt_brief": brief,
"default_style": style,
"default_size": self.settings.default_size,
}
if self.llm:
try:
prompt_text = (
"Return JSON only with keys prompt, style, size. "
"Convert into an image-generation prompt for the PRIMARY hero illustration "
"of a personal memoir story (one focal scene, emotionally resonant). "
"The API uses response_format=json_object.\n"
+ json.dumps(llm_input, ensure_ascii=False)
)
raw_response = invoke_json_object(
self.llm,
prompt_text,
max_tokens=512,
agent="MemoirImagePromptService.build_story_primary_prompt",
)
parsed = json.loads(extract_json_payload(raw_response))
return {
"prompt": _ensure_style_in_prompt(
parsed["prompt"], parsed.get("style", style)
),
"style": parsed.get("style", style),
"size": parsed.get("size", self.settings.default_size),
"prompt_context": prompt_context,
}
except Exception as exc:
if settings.image_prompt_fallback_disabled:
raise
logger.warning(
"story 主图 prompt 生成回退到默认模板: stage={}, title={}, error={}",
story_stage,
story_title,
exc,
)
elif settings.image_prompt_fallback_disabled:
raise RuntimeError(
"MemoirImagePromptService.build_story_primary_prompt requires LLM when "
"image_prompt_fallback_disabled is True"
)
from .image_placeholder_template import IMAGE_PLACEHOLDER_TEMPLATE
if brief:
body = brief
else:
joined = "".join(
filter(
None,
[(story_title or "").strip(), stage_key or None],
)
)
body = joined or "人生故事"
prompt_line = f"{IMAGE_PLACEHOLDER_TEMPLATE}{body}"
return {
"prompt": _ensure_style_in_prompt(prompt_line, style),
"style": style,
"size": self.settings.default_size,
"prompt_context": prompt_context,
}
def _build_cover_fallback_prompt(
self,
chapter_category: str,