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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 import TenantPermission
  17. from api.db.db_models import DB, Tenant
  18. from api.db.db_models import Knowledgebase
  19. from api.db.services.common_service import CommonService
  20. from api.db import StatusEnum
  21. class KnowledgebaseService(CommonService):
  22. model = Knowledgebase
  23. @classmethod
  24. @DB.connection_context()
  25. def get_by_tenant_ids(cls, joined_tenant_ids, user_id,
  26. page_number, items_per_page, orderby, desc):
  27. kbs = cls.model.select().where(
  28. ((cls.model.tenant_id.in_(joined_tenant_ids) & (cls.model.permission ==
  29. TenantPermission.TEAM.value)) | (cls.model.tenant_id == user_id))
  30. & (cls.model.status == StatusEnum.VALID.value)
  31. )
  32. if desc:
  33. kbs = kbs.order_by(cls.model.getter_by(orderby).desc())
  34. else:
  35. kbs = kbs.order_by(cls.model.getter_by(orderby).asc())
  36. kbs = kbs.paginate(page_number, items_per_page)
  37. return list(kbs.dicts())
  38. @classmethod
  39. @DB.connection_context()
  40. def get_detail(cls, kb_id):
  41. fields = [
  42. cls.model.id,
  43. Tenant.embd_id,
  44. cls.model.avatar,
  45. cls.model.name,
  46. cls.model.description,
  47. cls.model.permission,
  48. cls.model.doc_num,
  49. cls.model.token_num,
  50. cls.model.chunk_num,
  51. cls.model.parser_id]
  52. kbs = cls.model.select(*fields).join(Tenant, on=((Tenant.id == cls.model.tenant_id)&(Tenant.status== StatusEnum.VALID.value))).where(
  53. (cls.model.id == kb_id),
  54. (cls.model.status == StatusEnum.VALID.value)
  55. )
  56. if not kbs:
  57. return
  58. d = kbs[0].to_dict()
  59. d["embd_id"] = kbs[0].tenant.embd_id
  60. return d