File size: 3,001 Bytes
3079197
9bf75d4
3079197
 
 
 
 
 
 
 
 
 
 
 
 
6be3dd5
9bf75d4
 
 
 
 
3079197
 
 
 
 
 
 
 
 
 
 
 
3198faf
 
 
4a858d3
3198faf
 
 
 
 
 
 
4a858d3
3198faf
 
 
 
 
 
6be3dd5
 
 
 
 
 
 
 
 
 
 
4a858d3
6be3dd5
 
 
 
 
 
 
 
 
 
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
#
#  Copyright 2019 The InfiniFlow Authors. All Rights Reserved.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#
from rag.llm import EmbeddingModel, CvModel
from api.db import LLMType
from api.db.db_models import DB, UserTenant
from api.db.db_models import LLMFactories, LLM, TenantLLM
from api.db.services.common_service import CommonService
from api.db import StatusEnum


class LLMFactoriesService(CommonService):
    model = LLMFactories


class LLMService(CommonService):
    model = LLM


class TenantLLMService(CommonService):
    model = TenantLLM

    @classmethod
    @DB.connection_context()
    def get_api_key(cls, tenant_id, model_type, model_name=""):
        objs = cls.query(tenant_id=tenant_id, model_type=model_type)
        if objs and len(objs)>0 and objs[0].llm_name:
            return objs[0]

        fields = [LLM.llm_name, cls.model.llm_factory, cls.model.api_key]
        objs = cls.model.select(*fields).join(LLM, on=(LLM.fid == cls.model.llm_factory)).where(
            (cls.model.tenant_id == tenant_id),
            ((cls.model.model_type == model_type) | (cls.model.llm_name == model_name)),
            (LLM.status == StatusEnum.VALID)
        )

        if not objs:return
        return objs[0]

    @classmethod
    @DB.connection_context()
    def get_my_llms(cls, tenant_id):
        fields = [cls.model.llm_factory, LLMFactories.logo, LLMFactories.tags, cls.model.model_type, cls.model.llm_name]
        objs = cls.model.select(*fields).join(LLMFactories, on=(cls.model.llm_factory==LLMFactories.name)).where(cls.model.tenant_id==tenant_id).dicts()

        return list(objs)

    @classmethod
    @DB.connection_context()
    def model_instance(cls, tenant_id, llm_type):
        model_config = cls.get_api_key(tenant_id, model_type=LLMType.EMBEDDING.value)
        if not model_config:
            model_config = {"llm_factory": "local", "api_key": "", "llm_name": ""}
        else:
            model_config = model_config[0].to_dict()
        if llm_type == LLMType.EMBEDDING:
            if model_config["llm_factory"] not in EmbeddingModel: return
            return EmbeddingModel[model_config["llm_factory"]](model_config["api_key"], model_config["llm_name"])
        if llm_type == LLMType.IMAGE2TEXT:
            if model_config["llm_factory"] not in CvModel: return
            return CvModel[model_config.llm_factory](model_config["api_key"], model_config["llm_name"])