您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

knowledgebase_service.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. if count == -1:
  55. return kbs[offset:]
  56. return kbs[offset:offset+count]
  57. @classmethod
  58. @DB.connection_context()
  59. def get_detail(cls, kb_id):
  60. fields = [
  61. cls.model.id,
  62. #Tenant.embd_id,
  63. cls.model.embd_id,
  64. cls.model.avatar,
  65. cls.model.name,
  66. cls.model.language,
  67. cls.model.description,
  68. cls.model.permission,
  69. cls.model.doc_num,
  70. cls.model.token_num,
  71. cls.model.chunk_num,
  72. cls.model.parser_id,
  73. cls.model.parser_config]
  74. kbs = cls.model.select(*fields).join(Tenant, on=(
  75. (Tenant.id == cls.model.tenant_id) & (Tenant.status == StatusEnum.VALID.value))).where(
  76. (cls.model.id == kb_id),
  77. (cls.model.status == StatusEnum.VALID.value)
  78. )
  79. if not kbs:
  80. return
  81. d = kbs[0].to_dict()
  82. #d["embd_id"] = kbs[0].tenant.embd_id
  83. return d
  84. @classmethod
  85. @DB.connection_context()
  86. def update_parser_config(cls, id, config):
  87. e, m = cls.get_by_id(id)
  88. if not e:
  89. raise LookupError(f"knowledgebase({id}) not found.")
  90. def dfs_update(old, new):
  91. for k, v in new.items():
  92. if k not in old:
  93. old[k] = v
  94. continue
  95. if isinstance(v, dict):
  96. assert isinstance(old[k], dict)
  97. dfs_update(old[k], v)
  98. elif isinstance(v, list):
  99. assert isinstance(old[k], list)
  100. old[k] = list(set(old[k] + v))
  101. else:
  102. old[k] = v
  103. dfs_update(m.parser_config, config)
  104. cls.update_by_id(id, {"parser_config": m.parser_config})
  105. @classmethod
  106. @DB.connection_context()
  107. def get_field_map(cls, ids):
  108. conf = {}
  109. for k in cls.get_by_ids(ids):
  110. if k.parser_config and "field_map" in k.parser_config:
  111. conf.update(k.parser_config["field_map"])
  112. return conf
  113. @classmethod
  114. @DB.connection_context()
  115. def get_by_name(cls, kb_name, tenant_id):
  116. kb = cls.model.select().where(
  117. (cls.model.name == kb_name)
  118. & (cls.model.tenant_id == tenant_id)
  119. & (cls.model.status == StatusEnum.VALID.value)
  120. )
  121. if kb:
  122. return True, kb[0]
  123. return False, None
  124. @classmethod
  125. @DB.connection_context()
  126. def get_all_ids(cls):
  127. return [m["id"] for m in cls.model.select(cls.model.id).dicts()]