Files
life-echo/api/app/agents/image_prompt/orchestrator.py

82 lines
2.6 KiB
Python
Raw Normal View History

"""
ImagePromptOrchestrator图片提示词生成编排器
根据调用方封面/正文选择 build_prompt build_cover_prompt
统一异常处理和回退内部委托 PromptGenerationAgent
"""
2026-03-19 14:36:14 +08:00
from __future__ import annotations
from typing import Any, Optional
from app.features.memoir.memoir_images.settings import MemoirImageSettings
from app.agents.image_prompt.prompt_agent import PromptGenerationAgent
class ImagePromptOrchestrator:
"""
图片提示词编排器
区分封面 vs 正文配图统一调用 PromptGenerationAgent
异常与回退由 PromptGenerationAgent底层 MemoirImagePromptService处理
"""
def __init__(self, llm: Optional[Any], settings: MemoirImageSettings) -> None:
self._agent = PromptGenerationAgent(llm=llm, settings=settings)
def build_prompt(
self,
chapter_title: str,
chapter_category: str,
description: str,
context_excerpt: str,
) -> dict[str, str]:
"""
生成正文配图的 prompt
委托 PromptGenerationAgent已含 LLM 调用失败时的 fallback 逻辑
"""
return self._agent.build_prompt(
chapter_title=chapter_title,
chapter_category=chapter_category,
description=description,
context_excerpt=context_excerpt,
)
def build_cover_prompt(
self,
chapter_title: str,
chapter_category: str,
context_excerpt: str,
) -> dict[str, str]:
"""
生成章节封面的 prompt
委托 PromptGenerationAgent已含 LLM 调用失败时的 fallback 逻辑
"""
return self._agent.build_cover_prompt(
chapter_title=chapter_title,
chapter_category=chapter_category,
context_excerpt=context_excerpt,
)
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 与对话/长度行为。
2026-04-02 12:00:00 +08:00
def build_story_primary_prompt(
self,
story_title: str,
story_stage: str | None,
prompt_brief: str,
style_profile: str | None,
) -> dict[str, str]:
"""生成 story 主插图的 prompt。"""
return self._agent.build_story_primary_prompt(
story_title=story_title,
story_stage=story_stage,
prompt_brief=prompt_brief,
style_profile=style_profile,
)
def get_image_prompt_orchestrator() -> ImagePromptOrchestrator:
"""Celery / 后台任务入口:统一装配 LLM 与 MemoirImageSettings。"""
from app.core.dependencies import get_llm_provider
llm = getattr(get_llm_provider(), "langchain_llm", None)
return ImagePromptOrchestrator(llm=llm, settings=MemoirImageSettings.from_env())