File size: 4,243 Bytes
57cf043
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import logging

from fastapi import APIRouter, Response, UploadFile, Depends
from fastapi.responses import FileResponse

from schemas.llm_config import LLMConfig as LLMConfigScheme, LLMConfigCreateScheme
from components.dbo.models.llm_config import LLMConfig as LLMConfigSQL
from components.services.llm_config import LLMConfigService
import common.dependencies as DI
from schemas.llm_config import LLMConfig

router = APIRouter(prefix='/llm_config')
logger = logging.getLogger(__name__)


@router.get('/')
async def get_llm_config_list(llm_config_service: LLMConfigService = Depends(DI.get_llm_config_service)) -> list[LLMConfig]:
    logger.info("Handling GET request to /llm_config/{config_id}}")
    try:
        config = llm_config_service.get_list()
        return config
    except Exception as e:
        logger.error(f"Error retrieving llm config: {str(e)}")
        raise e
    
@router.get('/default')
async def get_llm_default_config(llm_config_service: 
                        LLMConfigService = Depends(DI.get_llm_config_service)
                        ) -> LLMConfig:
    logger.info("Handling GET request to /llm_config/default/")
    try:
        config = llm_config_service.get_default()
        logger.info(
            f"Successfully retrieved default llm config with ID {config.id}"
        )
        return config
    except Exception as e:
        logger.error(f"Error retrieving default llm config: {str(e)}")
        raise e


@router.get('/{config_id}')
async def get_llm_config(config_id: int, 
                        llm_config_service: LLMConfigService = Depends(DI.get_llm_config_service)
                        ) -> LLMConfig:
    logger.info("Handling GET request to /llm_config/{config_id}}")
    try:
        config = llm_config_service.get_by_id(config_id)
        logger.info(
            f"Successfully retrieved llm config with ID: {config_id}"
        )
        return config
    except Exception as e:
        logger.error(f"Error retrieving llm config: {str(e)}")
        raise e
    

@router.put('/default/{config_id}')
async def set_as_default_config(config_id: int, 
                        llm_config_service: LLMConfigService = Depends(DI.get_llm_config_service)):
    logger.info("Handling PUT request to /llm_config/default/{config_id}")
    try:
        llm_config_service.set_as_default(config_id)
        logger.info(
            f"Successfully setted default llm config with ID: {config_id}"
        )
        return Response(status_code=200)
    except Exception as e:
        logger.error(f"Error setting the default llm config: {str(e)}")
        raise e


@router.delete('/{config_id}')
async def delete_config(config_id: int, 
                        llm_config_service: LLMConfigService = Depends(DI.get_llm_config_service)):
    logger.info("Handling DELETE request to /llm_config/{config_id}")
    try:
        llm_config_service.delete(config_id)
        logger.info(
            f"Successfully deleted llm config: {config_id}"
        )
        return Response(status_code=200)
    except Exception as e:
        logger.error(f"Error deleting llm config: {str(e)}")
        raise e


@router.post('/')
async def create_config(data: LLMConfigCreateScheme, 
                       llm_config_service: LLMConfigService = Depends(DI.get_llm_config_service)):
    logger.info("Handling POST request to /llm_config")
    try:
        new_config = llm_config_service.create(data)
        logger.info(
            f"Successfully created llm config with ID: {new_config.id}"
        )
        return new_config
    except Exception as e:
        logger.error(f"Error creating llm config: {str(e)}")
        raise e


@router.put('/{config_id}')
async def update_config(config_id: int, file: LLMConfigScheme, 
                       llm_config_service: LLMConfigService = Depends(DI.get_llm_config_service)):
    logger.info("Handling PUT request to /llm_config/{config_id}")
    try:
        updated_config = llm_config_service.update(config_id, file)
        logger.info(
            f"Successfully updated llm config with ID: {config_id}"
        )
        return updated_config
    except Exception as e:
        logger.error(f"Error updating llm config: {str(e)}")
        raise e