Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

llm_service.py 3.0KB

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