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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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, User
  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. fields = [
  26. cls.model.id,
  27. cls.model.avatar,
  28. cls.model.name,
  29. cls.model.language,
  30. cls.model.description,
  31. cls.model.permission,
  32. cls.model.doc_num,
  33. cls.model.token_num,
  34. cls.model.chunk_num,
  35. cls.model.parser_id,
  36. cls.model.embd_id,
  37. User.nickname,
  38. User.avatar.alias('tenant_avatar'),
  39. cls.model.update_time
  40. ]
  41. kbs = cls.model.select(*fields).join(User, on=(cls.model.tenant_id == User.id)).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 = kbs.paginate(page_number, items_per_page)
  52. return list(kbs.dicts())
  53. @classmethod
  54. @DB.connection_context()
  55. def get_by_tenant_ids_by_offset(cls, joined_tenant_ids, user_id, offset, count, orderby, desc):
  56. kbs = cls.model.select().where(
  57. ((cls.model.tenant_id.in_(joined_tenant_ids) & (cls.model.permission ==
  58. TenantPermission.TEAM.value)) | (
  59. cls.model.tenant_id == user_id))
  60. & (cls.model.status == StatusEnum.VALID.value)
  61. )
  62. if desc:
  63. kbs = kbs.order_by(cls.model.getter_by(orderby).desc())
  64. else:
  65. kbs = kbs.order_by(cls.model.getter_by(orderby).asc())
  66. kbs = list(kbs.dicts())
  67. kbs_length = len(kbs)
  68. if offset < 0 or offset > kbs_length:
  69. raise IndexError("Offset is out of the valid range.")
  70. if count == -1:
  71. return kbs[offset:]
  72. return kbs[offset:offset + count]
  73. @classmethod
  74. @DB.connection_context()
  75. def get_detail(cls, kb_id):
  76. fields = [
  77. cls.model.id,
  78. # Tenant.embd_id,
  79. cls.model.embd_id,
  80. cls.model.avatar,
  81. cls.model.name,
  82. cls.model.language,
  83. cls.model.description,
  84. cls.model.permission,
  85. cls.model.doc_num,
  86. cls.model.token_num,
  87. cls.model.chunk_num,
  88. cls.model.parser_id,
  89. cls.model.parser_config]
  90. kbs = cls.model.select(*fields).join(Tenant, on=(
  91. (Tenant.id == cls.model.tenant_id) & (Tenant.status == StatusEnum.VALID.value))).where(
  92. (cls.model.id == kb_id),
  93. (cls.model.status == StatusEnum.VALID.value)
  94. )
  95. if not kbs:
  96. return
  97. d = kbs[0].to_dict()
  98. # d["embd_id"] = kbs[0].tenant.embd_id
  99. return d
  100. @classmethod
  101. @DB.connection_context()
  102. def update_parser_config(cls, id, config):
  103. e, m = cls.get_by_id(id)
  104. if not e:
  105. raise LookupError(f"knowledgebase({id}) not found.")
  106. def dfs_update(old, new):
  107. for k, v in new.items():
  108. if k not in old:
  109. old[k] = v
  110. continue
  111. if isinstance(v, dict):
  112. assert isinstance(old[k], dict)
  113. dfs_update(old[k], v)
  114. elif isinstance(v, list):
  115. assert isinstance(old[k], list)
  116. old[k] = list(set(old[k] + v))
  117. else:
  118. old[k] = v
  119. dfs_update(m.parser_config, config)
  120. cls.update_by_id(id, {"parser_config": m.parser_config})
  121. @classmethod
  122. @DB.connection_context()
  123. def get_field_map(cls, ids):
  124. conf = {}
  125. for k in cls.get_by_ids(ids):
  126. if k.parser_config and "field_map" in k.parser_config:
  127. conf.update(k.parser_config["field_map"])
  128. return conf
  129. @classmethod
  130. @DB.connection_context()
  131. def get_by_name(cls, kb_name, tenant_id):
  132. kb = cls.model.select().where(
  133. (cls.model.name == kb_name)
  134. & (cls.model.tenant_id == tenant_id)
  135. & (cls.model.status == StatusEnum.VALID.value)
  136. )
  137. if kb:
  138. return True, kb[0]
  139. return False, None
  140. @classmethod
  141. @DB.connection_context()
  142. def get_all_ids(cls):
  143. return [m["id"] for m in cls.model.select(cls.model.id).dicts()]
  144. @classmethod
  145. @DB.connection_context()
  146. def get_list(cls, joined_tenant_ids, user_id,
  147. page_number, items_per_page, orderby, desc, id, name):
  148. kbs = cls.model.select()
  149. if id:
  150. kbs = kbs.where(cls.model.id == id)
  151. if name:
  152. kbs = kbs.where(cls.model.name == name)
  153. kbs = kbs.where(
  154. ((cls.model.tenant_id.in_(joined_tenant_ids) & (cls.model.permission ==
  155. TenantPermission.TEAM.value)) | (
  156. cls.model.tenant_id == user_id))
  157. & (cls.model.status == StatusEnum.VALID.value)
  158. )
  159. if desc:
  160. kbs = kbs.order_by(cls.model.getter_by(orderby).desc())
  161. else:
  162. kbs = kbs.order_by(cls.model.getter_by(orderby).asc())
  163. kbs = kbs.paginate(page_number, items_per_page)
  164. return list(kbs.dicts())