Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #
  2. # Copyright 2019 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 rag.llm import EmbeddingModel, CvModel
  17. from api.db import LLMType
  18. from api.db.db_models import DB, UserTenant
  19. from api.db.db_models import LLMFactories, LLM, TenantLLM
  20. from api.db.services.common_service import CommonService
  21. from api.db import StatusEnum
  22. class LLMFactoriesService(CommonService):
  23. model = LLMFactories
  24. class LLMService(CommonService):
  25. model = LLM
  26. class TenantLLMService(CommonService):
  27. model = TenantLLM
  28. @classmethod
  29. @DB.connection_context()
  30. def get_api_key(cls, tenant_id, model_type):
  31. objs = cls.query(tenant_id=tenant_id, model_type=model_type)
  32. if objs and len(objs)>0 and objs[0].llm_name:
  33. return objs[0]
  34. fields = [LLM.llm_name, cls.model.llm_factory, cls.model.api_key]
  35. objs = cls.model.select(*fields).join(LLM, on=(LLM.fid == cls.model.llm_factory)).where(
  36. (cls.model.tenant_id == tenant_id),
  37. (cls.model.model_type == model_type),
  38. (LLM.status == StatusEnum.VALID)
  39. )
  40. if not objs:return
  41. return objs[0]
  42. @classmethod
  43. @DB.connection_context()
  44. def get_my_llms(cls, tenant_id):
  45. fields = [cls.model.llm_factory, LLMFactories.logo, LLMFactories.tags, cls.model.model_type, cls.model.llm_name]
  46. objs = cls.model.select(*fields).join(LLMFactories, on=(cls.model.llm_factory==LLMFactories.name)).where(cls.model.tenant_id==tenant_id).dicts()
  47. return list(objs)
  48. @classmethod
  49. @DB.connection_context()
  50. def model_instance(cls, tenant_id, llm_type):
  51. model_config = cls.get_api_key(tenant_id, model_type=LLMType.EMBEDDING)
  52. if not model_config:
  53. model_config = {"llm_factory": "local", "api_key": "", "llm_name": ""}
  54. else:
  55. model_config = model_config[0].to_dict()
  56. if llm_type == LLMType.EMBEDDING:
  57. if model_config["llm_factory"] not in EmbeddingModel: return
  58. return EmbeddingModel[model_config["llm_factory"]](model_config["api_key"], model_config["llm_name"])
  59. if llm_type == LLMType.IMAGE2TEXT:
  60. if model_config["llm_factory"] not in CvModel: return
  61. return CvModel[model_config.llm_factory](model_config["api_key"], model_config["llm_name"])