diff --git a/qwen3-tts-backend/api/auth.py b/qwen3-tts-backend/api/auth.py index 9b059aa..8b56f0d 100644 --- a/qwen3-tts-backend/api/auth.py +++ b/qwen3-tts-backend/api/auth.py @@ -297,13 +297,29 @@ async def set_llm_config( db: Session = Depends(get_db) ): from core.security import encrypt_api_key - encrypted_key = encrypt_api_key(config.api_key.strip()) + from core.llm_service import LLMService + + api_key = config.api_key.strip() + base_url = config.base_url.strip() + model = config.model.strip() + + # Validate LLM config by sending a test request + llm = LLMService(base_url=base_url, api_key=api_key, model=model) + try: + await llm.chat("You are a test assistant.", "Reply with 'ok'.") + except Exception as e: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail=f"LLM API validation failed: {e}" + ) + + encrypted_key = encrypt_api_key(api_key) update_user_llm_config( db, user_id=current_user.id, llm_api_key=encrypted_key, - llm_base_url=config.base_url.strip(), - llm_model=config.model.strip(), + llm_base_url=base_url, + llm_model=model, ) return {"message": "LLM config updated successfully"} diff --git a/qwen3-tts-backend/db/database.py b/qwen3-tts-backend/db/database.py index 254e1e0..f81b2eb 100644 --- a/qwen3-tts-backend/db/database.py +++ b/qwen3-tts-backend/db/database.py @@ -1,14 +1,18 @@ from sqlalchemy import create_engine, event from sqlalchemy.orm import sessionmaker, declarative_base +from sqlalchemy.pool import NullPool from config import settings +_is_sqlite = "sqlite" in settings.DATABASE_URL + engine = create_engine( settings.DATABASE_URL, - connect_args={"check_same_thread": False} if "sqlite" in settings.DATABASE_URL else {} + connect_args={"check_same_thread": False} if _is_sqlite else {}, + poolclass=NullPool if _is_sqlite else None, ) -if "sqlite" in settings.DATABASE_URL: +if _is_sqlite: @event.listens_for(engine, "connect") def _set_wal(dbapi_conn, _): dbapi_conn.execute("PRAGMA journal_mode=WAL")