feat: refactor LLM model retrieval to use system settings and improve error handling for LLM configuration

This commit is contained in:
2026-03-13 16:07:00 +08:00
parent cdb9d2ebb8
commit 786254cb81
2 changed files with 26 additions and 21 deletions

View File

@@ -117,7 +117,7 @@ async def create_project(
title=data.title,
source_type=data.source_type,
source_text=data.source_text,
llm_model=current_user.llm_model,
llm_model=crud.get_system_setting(db, "llm_model"),
)
return _project_to_response(project)
@@ -150,7 +150,7 @@ async def upload_epub_project(
title=title,
source_type="epub",
source_path=str(file_path),
llm_model=current_user.llm_model,
llm_model=crud.get_system_setting(db, "llm_model"),
)
return _project_to_response(project)
@@ -319,8 +319,16 @@ async def continue_script(
raise HTTPException(status_code=400, detail=f"Project must be in 'ready' or 'done' state, current: {project.status}")
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.")
cfg = project.script_config or {}
if cfg.get("nsfw_mode"):
from db.crud import can_user_use_nsfw
if not can_user_use_nsfw(current_user):
raise HTTPException(status_code=403, detail="NSFW access not granted")
if not get_system_setting(db, "grok_api_key") or not get_system_setting(db, "grok_base_url"):
raise HTTPException(status_code=400, detail="Grok config not set. Please configure Grok API key first.")
else:
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 continue_ai_script_chapters
from core.database import SessionLocal
@@ -450,7 +458,8 @@ async def analyze_project(
if project.status in ("analyzing", "generating", "parsing"):
raise HTTPException(status_code=400, detail=f"Project is currently {project.status}, please wait")
if not current_user.llm_api_key or not current_user.llm_base_url or not current_user.llm_model:
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 analyze_project as _analyze
@@ -585,7 +594,8 @@ async def parse_chapter(
if chapter.status == "parsing":
raise HTTPException(status_code=400, detail="Chapter is already being parsed")
if not current_user.llm_api_key or not current_user.llm_base_url or not current_user.llm_model:
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")
from core.audiobook_service import parse_one_chapter
@@ -616,7 +626,8 @@ async def parse_all_chapters_endpoint(
if project.status not in ("ready", "generating", "done", "error"):
raise HTTPException(status_code=400, detail=f"Project must be in 'ready' state, current: {project.status}")
if not current_user.llm_api_key or not current_user.llm_base_url or not current_user.llm_model:
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")
from core.audiobook_service import parse_all_chapters
@@ -648,7 +659,8 @@ async def process_all_endpoint(
if project.status not in ("ready", "generating", "done", "error"):
raise HTTPException(status_code=400, detail=f"Project must be in 'ready' state, current: {project.status}")
if not current_user.llm_api_key or not current_user.llm_base_url or not current_user.llm_model:
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")
from core.audiobook_service import process_all
@@ -972,9 +984,8 @@ async def download_project(
Path(settings.OUTPUT_DIR) / "audiobook" / str(project_id) / "full.wav"
)
if not Path(output_path).exists():
from core.audiobook_service import merge_audio_files
merge_audio_files(audio_paths, output_path)
from core.audiobook_service import merge_audio_files
merge_audio_files(audio_paths, output_path)
filename = f"chapter_{chapter}.wav" if chapter is not None else f"{project.title}.wav"
return FileResponse(output_path, media_type="audio/wav", filename=filename)