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 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #
  2. # Copyright 2019 The FATE 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 web_server.db.db_models import DB, UserTenant
  19. from web_server.db.db_models import LLMFactories, LLM, TenantLLM
  20. from web_server.db.services.common_service import CommonService
  21. from web_server.utils import get_uuid, get_format_time
  22. from web_server.db.db_utils 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_type):
  32. objs = cls.query(tenant_id=tenant_id, model_type=model_type)
  33. if objs and len(objs)>0 and objs[0].llm_name:
  34. return objs[0]
  35. fields = [LLM.llm_name, cls.model.llm_factory, cls.model.api_key]
  36. objs = cls.model.select(*fields).join(LLM, on=(LLM.fid == cls.model.llm_factory)).where(
  37. (cls.model.tenant_id == tenant_id),
  38. (cls.model.model_type == model_type),
  39. (LLM.status == StatusEnum.VALID)
  40. )
  41. if not objs:return
  42. return objs[0]