Skip to content

Commit

Permalink
feat(backend): support test llm
Browse files Browse the repository at this point in the history
  • Loading branch information
wd0517 committed Jul 24, 2024
1 parent b317809 commit fe52ad1
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions backend/app/api/admin_routes/llm.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from typing import List
from pydantic import BaseModel

from fastapi import APIRouter, Depends, HTTPException, status
from fastapi_pagination import Params, Page
from fastapi_pagination.ext.sqlmodel import paginate
from sqlmodel import select
from sqlmodel import select, update

from app.api.deps import SessionDep, CurrentSuperuserDep
from app.rag.llm_option import admin_llm_options, LLMOption
from app.rag.chat_config import get_llm
from app.models import (
ChatEngine,
LLM,
AdminLLM,
EmbeddingModel,
Expand Down Expand Up @@ -47,8 +50,34 @@ def create_llm(
return llm


class LLMTestResult(BaseModel):
success: bool
error: str = ""


@router.post("/admin/llms/test")
def test_llm(
db_llm: LLM,
user: CurrentSuperuserDep,
) -> LLMTestResult:
try:
llm = get_llm(
provider=db_llm.provider,
model=db_llm.model,
config=db_llm.config,
credentials=db_llm.credentials,
)
response = llm.complete("Who are you?")
success = True
error = ""
except Exception as e:
success = False
error = str(e)
return LLMTestResult(success=success, error=error)


@router.get("/admin/llms/{llm_id}")
def get_llm(
def get_llm_detail(
llm_id: int,
session: SessionDep,
user: CurrentSuperuserDep,
Expand All @@ -72,6 +101,16 @@ def delete_llm(
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="LLM not found"
)
session.exec(
update(ChatEngine)
.where(ChatEngine.llm_id == llm_id)
.values(llm_id=None)
)
session.exec(
update(ChatEngine)
.where(ChatEngine.fast_llm_id == llm_id)
.values(fast_llm_id=None)
)
session.delete(llm)
session.commit()
return llm

0 comments on commit fe52ad1

Please sign in to comment.