From d9082b12a8d14a6dee342f3fe721c221e0fcb09c Mon Sep 17 00:00:00 2001 From: bdim404 Date: Wed, 11 Mar 2026 17:32:54 +0800 Subject: [PATCH] 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"}