From d9082b12a8d14a6dee342f3fe721c221e0fcb09c Mon Sep 17 00:00:00 2001 From: bdim404 Date: Wed, 11 Mar 2026 17:32:54 +0800 Subject: [PATCH 1/2] feat: Validate LLM configuration by sending a test request during API key update. --- qwen3-tts-backend/api/auth.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) 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"} From f9a0e2bcc47cc15971fe4e7b1dfce9fde1c68e15 Mon Sep 17 00:00:00 2001 From: bdim404 Date: Wed, 11 Mar 2026 18:04:16 +0800 Subject: [PATCH 2/2] refactor: Simplify SQLite checks by introducing a variable for database type --- qwen3-tts-backend/db/database.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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")