Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

knowledgebase_service.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 StatusEnum, TenantPermission
  17. from api.db.db_models import Knowledgebase, DB, Tenant
  18. from api.db.services.common_service import CommonService
  19. class KnowledgebaseService(CommonService):
  20. model = Knowledgebase
  21. @classmethod
  22. @DB.connection_context()
  23. def get_by_tenant_ids(cls, joined_tenant_ids, user_id,
  24. page_number, items_per_page, orderby, desc):
  25. kbs = cls.model.select().where(
  26. ((cls.model.tenant_id.in_(joined_tenant_ids) & (cls.model.permission ==
  27. TenantPermission.TEAM.value)) | (
  28. cls.model.tenant_id == user_id))
  29. & (cls.model.status == StatusEnum.VALID.value)
  30. )
  31. if desc:
  32. kbs = kbs.order_by(cls.model.getter_by(orderby).desc())
  33. else:
  34. kbs = kbs.order_by(cls.model.getter_by(orderby).asc())
  35. kbs = kbs.paginate(page_number, items_per_page)
  36. return list(kbs.dicts())
  37. @classmethod
  38. @DB.connection_context()
  39. def get_by_tenant_ids_by_offset(cls, joined_tenant_ids, user_id, offset, count, orderby, desc):
  40. kbs = cls.model.select().where(
  41. ((cls.model.tenant_id.in_(joined_tenant_ids) & (cls.model.permission ==
  42. TenantPermission.TEAM.value)) | (
  43. cls.model.tenant_id == user_id))
  44. & (cls.model.status == StatusEnum.VALID.value)
  45. )
  46. if desc:
  47. kbs = kbs.order_by(cls.model.getter_by(orderby).desc())
  48. else:
  49. kbs = kbs.order_by(cls.model.getter_by(orderby).asc())
  50. kbs = list(kbs.dicts())
  51. kbs_length = len(kbs)
  52. if offset < 0 or offset > kbs_length:
  53. raise IndexError("Offset is out of the valid range.")
  54. return kbs[offset:offset+count]
  55. @classmethod
  56. @DB.connection_context()
  57. def get_detail(cls, kb_id):
  58. fields = [
  59. cls.model.id,
  60. #Tenant.embd_id,
  61. cls.model.embd_id,
  62. cls.model.avatar,
  63. cls.model.name,
  64. cls.model.language,
  65. cls.model.description,
  66. cls.model.permission,
  67. cls.model.doc_num,
  68. cls.model.token_num,
  69. cls.model.chunk_num,
  70. cls.model.parser_id,
  71. cls.model.parser_config]
  72. kbs = cls.model.select(*fields).join(Tenant, on=(
  73. (Tenant.id == cls.model.tenant_id) & (Tenant.status == StatusEnum.VALID.value))).where(
  74. (cls.model.id == kb_id),
  75. (cls.model.status == StatusEnum.VALID.value)
  76. )
  77. if not kbs:
  78. return
  79. d = kbs[0].to_dict()
  80. #d["embd_id"] = kbs[0].tenant.embd_id
  81. return d
  82. @classmethod
  83. @DB.connection_context()
  84. def update_parser_config(cls, id, config):
  85. e, m = cls.get_by_id(id)
  86. if not e:
  87. raise LookupError(f"knowledgebase({id}) not found.")
  88. def dfs_update(old, new):
  89. for k, v in new.items():
  90. if k not in old:
  91. old[k] = v
  92. continue
  93. if isinstance(v, dict):
  94. assert isinstance(old[k], dict)
  95. dfs_update(old[k], v)
  96. elif isinstance(v, list):
  97. assert isinstance(old[k], list)
  98. old[k] = list(set(old[k] + v))
  99. else:
  100. old[k] = v
  101. dfs_update(m.parser_config, config)
  102. cls.update_by_id(id, {"parser_config": m.parser_config})
  103. @classmethod
  104. @DB.connection_context()
  105. def get_field_map(cls, ids):
  106. conf = {}
  107. for k in cls.get_by_ids(ids):
  108. if k.parser_config and "field_map" in k.parser_config:
  109. conf.update(k.parser_config["field_map"])
  110. return conf
  111. @classmethod
  112. @DB.connection_context()
  113. def get_by_name(cls, kb_name, tenant_id):
  114. kb = cls.model.select().where(
  115. (cls.model.name == kb_name)
  116. & (cls.model.tenant_id == tenant_id)
  117. & (cls.model.status == StatusEnum.VALID.value)
  118. )
  119. if kb:
  120. return True, kb[0]
  121. return False, None
  122. @classmethod
  123. @DB.connection_context()
  124. def get_all_ids(cls):
  125. return [m["id"] for m in cls.model.select(cls.model.id).dicts()]