選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

knowledgebase_service.py 5.1KB

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