2026-03-19 10:43:34 +08:00
|
|
|
|
"""
|
|
|
|
|
|
ImagePromptOrchestrator:图片提示词生成编排器。
|
|
|
|
|
|
根据调用方(封面/正文)选择 build_prompt 或 build_cover_prompt;
|
|
|
|
|
|
统一异常处理和回退;内部委托 PromptGenerationAgent。
|
|
|
|
|
|
"""
|
2026-03-19 14:36:14 +08:00
|
|
|
|
|
2026-03-19 10:43:34 +08:00
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
|
|
from typing import Any, Optional
|
|
|
|
|
|
|
|
|
|
|
|
from app.agents.image_prompt.prompt_agent import PromptGenerationAgent
|
2026-05-06 13:18:02 +08:00
|
|
|
|
from app.core.config import settings
|
|
|
|
|
|
from app.core.logging import get_logger
|
2026-04-08 15:37:09 +08:00
|
|
|
|
from app.features.memoir.memoir_images.settings import MemoirImageSettings
|
2026-03-19 10:43:34 +08:00
|
|
|
|
|
2026-05-06 13:18:02 +08:00
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
2026-03-19 10:43:34 +08:00
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
|
)
|
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。"""
|
2026-04-30 09:17:01 +08:00
|
|
|
|
from app.core.llm_gateway import LlmGateway, LlmUseCase
|
2026-04-02 12:00:00 +08:00
|
|
|
|
|
2026-05-06 13:18:02 +08:00
|
|
|
|
image_settings = MemoirImageSettings.from_env()
|
|
|
|
|
|
try:
|
|
|
|
|
|
llm = LlmGateway().langchain_llm_for(LlmUseCase("image_prompt"))
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
if settings.image_prompt_fallback_disabled:
|
|
|
|
|
|
raise
|
|
|
|
|
|
logger.warning(
|
|
|
|
|
|
"ImagePromptOrchestrator LLM 初始化失败,使用确定性 fallback: {}",
|
|
|
|
|
|
e,
|
|
|
|
|
|
)
|
|
|
|
|
|
llm = None
|
|
|
|
|
|
return ImagePromptOrchestrator(llm=llm, settings=image_settings)
|