feat: add synopsis generation endpoint and frontend integration
This commit is contained in:
@@ -24,6 +24,7 @@ from schemas.audiobook import (
|
||||
AudiobookGenerateRequest,
|
||||
AudiobookAnalyzeRequest,
|
||||
ScriptGenerationRequest,
|
||||
SynopsisGenerationRequest,
|
||||
)
|
||||
from core.config import settings
|
||||
|
||||
@@ -152,6 +153,47 @@ async def list_projects(
|
||||
return [_project_to_response(p) for p in projects]
|
||||
|
||||
|
||||
@router.post("/projects/generate-synopsis")
|
||||
async def generate_synopsis(
|
||||
data: SynopsisGenerationRequest,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
from db.crud import get_system_setting
|
||||
if not get_system_setting(db, "llm_api_key") or not get_system_setting(db, "llm_base_url") or not get_system_setting(db, "llm_model"):
|
||||
raise HTTPException(status_code=400, detail="LLM config not set. Please configure LLM API key first.")
|
||||
|
||||
from core.audiobook_service import _get_llm_service
|
||||
llm = _get_llm_service(db)
|
||||
|
||||
system_prompt = (
|
||||
"你是一位专业的小说策划师,擅长根据创作参数生成引人入胜的故事简介。"
|
||||
"请根据用户提供的类型、风格、主角、冲突等参数,生成一段200-400字的中文故事简介。"
|
||||
"简介需涵盖:世界观背景、主角基本情况、核心矛盾冲突、故事基调。"
|
||||
"直接输出简介正文,不要加任何前缀标题或说明文字。"
|
||||
)
|
||||
parts = [f"类型:{data.genre}"]
|
||||
if data.subgenre:
|
||||
parts.append(f"子类型:{data.subgenre}")
|
||||
if data.protagonist_type:
|
||||
parts.append(f"主角类型:{data.protagonist_type}")
|
||||
if data.tone:
|
||||
parts.append(f"故事基调:{data.tone}")
|
||||
if data.conflict_scale:
|
||||
parts.append(f"冲突规模:{data.conflict_scale}")
|
||||
parts.append(f"角色数量:约{data.num_characters}个主要角色")
|
||||
parts.append(f"故事体量:约{data.num_chapters}章")
|
||||
user_message = "\n".join(parts) + "\n\n请生成故事简介:"
|
||||
|
||||
try:
|
||||
synopsis = await llm.chat(system_prompt, user_message)
|
||||
except Exception as e:
|
||||
logger.error(f"Synopsis generation failed: {e}")
|
||||
raise HTTPException(status_code=500, detail=f"LLM generation failed: {str(e)}")
|
||||
|
||||
return {"synopsis": synopsis}
|
||||
|
||||
|
||||
@router.post("/projects/generate-script", response_model=AudiobookProjectResponse, status_code=status.HTTP_201_CREATED)
|
||||
async def create_ai_script_project(
|
||||
data: ScriptGenerationRequest,
|
||||
|
||||
Reference in New Issue
Block a user