update model card
This commit is contained in:
@@ -6,6 +6,7 @@ from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
||||
from jose import jwt, JWTError
|
||||
from pydantic import BaseModel, Field
|
||||
from app.core.security import SECRET_KEY, ALGORITHM
|
||||
from litellm import completion
|
||||
|
||||
router = APIRouter()
|
||||
security = HTTPBearer()
|
||||
@@ -56,6 +57,13 @@ class LLMConfigUpdate(BaseModel):
|
||||
extra_headers: Optional[Dict[str, str]] = None
|
||||
is_active: Optional[bool] = None
|
||||
|
||||
class TestConnectionRequest(BaseModel):
|
||||
provider: str
|
||||
model: str
|
||||
api_key: Optional[str] = None
|
||||
api_base: Optional[str] = None
|
||||
extra_headers: Optional[Dict[str, str]] = None
|
||||
|
||||
|
||||
def get_current_user(credentials: HTTPAuthorizationCredentials = Depends(security)) -> CurrentUser:
|
||||
unauthorized = HTTPException(
|
||||
@@ -152,3 +160,54 @@ def delete_llm_config(config_id: str, _: CurrentUser = Depends(get_admin_user)):
|
||||
raise HTTPException(status_code=404, detail="LLM configuration not found")
|
||||
_save_data(data)
|
||||
return {"message": "LLM configuration deleted successfully"}
|
||||
|
||||
@router.post("/llm/test")
|
||||
def test_connection(request: TestConnectionRequest, _: CurrentUser = Depends(get_admin_user)):
|
||||
try:
|
||||
# Use litellm to test connection
|
||||
# litellm handles many providers
|
||||
kwargs = {
|
||||
"model": request.model,
|
||||
"messages": [{"role": "user", "content": "Hello"}],
|
||||
"max_tokens": 5
|
||||
}
|
||||
|
||||
if request.api_key:
|
||||
kwargs["api_key"] = request.api_key
|
||||
|
||||
if request.api_base:
|
||||
kwargs["api_base"] = request.api_base
|
||||
|
||||
if request.extra_headers:
|
||||
kwargs["extra_headers"] = request.extra_headers
|
||||
|
||||
# For OpenAI-compatible endpoints that are not standard OpenAI (like Local, vLLM etc)
|
||||
# usually user sets provider to "openai" and api_base to their custom URL.
|
||||
# litellm usually works well if we pass custom_llm_provider="openai" if provider is openai but custom url
|
||||
|
||||
# If provider is "local" or "openai", we generally use "openai" format
|
||||
if request.provider == "local":
|
||||
kwargs["custom_llm_provider"] = "openai"
|
||||
elif request.provider:
|
||||
kwargs["custom_llm_provider"] = request.provider
|
||||
|
||||
# If user explicitly selected provider in UI, we might want to respect that
|
||||
# But litellm completion main arg is 'model'.
|
||||
# If the UI 'model' input doesn't have prefix, we might need to add it or pass custom_llm_provider.
|
||||
|
||||
# Simple heuristic: if provider is set, try to pass it if litellm supports it or just rely on env vars/args
|
||||
# For this simple test, we just try to call it.
|
||||
|
||||
try:
|
||||
response = completion(**kwargs)
|
||||
except Exception as first_error:
|
||||
error_text = str(first_error)
|
||||
if request.provider and "Provider NOT provided" in error_text and "/" not in request.model:
|
||||
retry_kwargs = kwargs.copy()
|
||||
retry_kwargs["model"] = f"{request.provider}/{request.model}"
|
||||
response = completion(**retry_kwargs)
|
||||
else:
|
||||
raise first_error
|
||||
return {"success": True, "message": "Connection successful", "details": str(response)}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=f"Connection failed: {str(e)}")
|
||||
|
||||
Reference in New Issue
Block a user