You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

llm_service.py 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from api.db.services.user_service import TenantService
  17. from rag.llm import EmbeddingModel, CvModel
  18. from api.db import LLMType
  19. from api.db.db_models import DB, UserTenant
  20. from api.db.db_models import LLMFactories, LLM, TenantLLM
  21. from api.db.services.common_service import CommonService
  22. from api.db import StatusEnum
  23. class LLMFactoriesService(CommonService):
  24. model = LLMFactories
  25. class LLMService(CommonService):
  26. model = LLM
  27. class TenantLLMService(CommonService):
  28. model = TenantLLM
  29. @classmethod
  30. @DB.connection_context()
  31. def get_api_key(cls, tenant_id, model_name):
  32. objs = cls.query(tenant_id=tenant_id, llm_name=model_name)
  33. if not objs: return
  34. return objs[0]
  35. @classmethod
  36. @DB.connection_context()
  37. def get_my_llms(cls, tenant_id):
  38. fields = [cls.model.llm_factory, LLMFactories.logo, LLMFactories.tags, cls.model.model_type, cls.model.llm_name]
  39. objs = cls.model.select(*fields).join(LLMFactories, on=(cls.model.llm_factory == LLMFactories.name)).where(
  40. cls.model.tenant_id == tenant_id).dicts()
  41. return list(objs)
  42. @classmethod
  43. @DB.connection_context()
  44. def model_instance(cls, tenant_id, llm_type):
  45. e,tenant = TenantService.get_by_id(tenant_id)
  46. if not e: raise LookupError("Tenant not found")
  47. if llm_type == LLMType.EMBEDDING.value: mdlnm = tenant.embd_id
  48. elif llm_type == LLMType.SPEECH2TEXT.value: mdlnm = tenant.asr_id
  49. elif llm_type == LLMType.IMAGE2TEXT.value: mdlnm = tenant.img2txt_id
  50. elif llm_type == LLMType.CHAT.value: mdlnm = tenant.llm_id
  51. else: assert False, "LLM type error"
  52. model_config = cls.get_api_key(tenant_id, mdlnm)
  53. if not model_config: raise LookupError("Model({}) not found".format(mdlnm))
  54. model_config = model_config[0].to_dict()
  55. if llm_type == LLMType.EMBEDDING.value:
  56. if model_config["llm_factory"] not in EmbeddingModel: return
  57. return EmbeddingModel[model_config["llm_factory"]](model_config["api_key"], model_config["llm_name"])
  58. if llm_type == LLMType.IMAGE2TEXT.value:
  59. if model_config["llm_factory"] not in CvModel: return
  60. return CvModel[model_config["llm_factory"]](model_config["api_key"], model_config["llm_name"])