Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

knowledgebase_service.py 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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, UserTenant,Document
  18. from api.db.services.common_service import CommonService
  19. class KnowledgebaseService(CommonService):
  20. model = Knowledgebase
  21. @classmethod
  22. @DB.connection_context()
  23. def list_documents_by_ids(cls,kb_ids):
  24. doc_ids=cls.model.select(Document.id.alias("document_id")).join(Document,on=(cls.model.id == Document.kb_id)).where(
  25. cls.model.id.in_(kb_ids)
  26. )
  27. doc_ids =list(doc_ids.dicts())
  28. doc_ids = [doc["document_id"] for doc in doc_ids]
  29. return doc_ids
  30. @classmethod
  31. @DB.connection_context()
  32. def get_by_tenant_ids(cls, joined_tenant_ids, user_id,
  33. page_number, items_per_page, orderby, desc):
  34. fields = [
  35. cls.model.id,
  36. cls.model.avatar,
  37. cls.model.name,
  38. cls.model.language,
  39. cls.model.description,
  40. cls.model.permission,
  41. cls.model.doc_num,
  42. cls.model.token_num,
  43. cls.model.chunk_num,
  44. cls.model.parser_id,
  45. cls.model.embd_id,
  46. User.nickname,
  47. User.avatar.alias('tenant_avatar'),
  48. cls.model.update_time
  49. ]
  50. kbs = cls.model.select(*fields).join(User, on=(cls.model.tenant_id == User.id)).where(
  51. ((cls.model.tenant_id.in_(joined_tenant_ids) & (cls.model.permission ==
  52. TenantPermission.TEAM.value)) | (
  53. cls.model.tenant_id == user_id))
  54. & (cls.model.status == StatusEnum.VALID.value)
  55. )
  56. if desc:
  57. kbs = kbs.order_by(cls.model.getter_by(orderby).desc())
  58. else:
  59. kbs = kbs.order_by(cls.model.getter_by(orderby).asc())
  60. kbs = kbs.paginate(page_number, items_per_page)
  61. return list(kbs.dicts())
  62. @classmethod
  63. @DB.connection_context()
  64. def get_detail(cls, kb_id):
  65. fields = [
  66. cls.model.id,
  67. # Tenant.embd_id,
  68. cls.model.embd_id,
  69. cls.model.avatar,
  70. cls.model.name,
  71. cls.model.language,
  72. cls.model.description,
  73. cls.model.permission,
  74. cls.model.doc_num,
  75. cls.model.token_num,
  76. cls.model.chunk_num,
  77. cls.model.parser_id,
  78. cls.model.parser_config]
  79. kbs = cls.model.select(*fields).join(Tenant, on=(
  80. (Tenant.id == cls.model.tenant_id) & (Tenant.status == StatusEnum.VALID.value))).where(
  81. (cls.model.id == kb_id),
  82. (cls.model.status == StatusEnum.VALID.value)
  83. )
  84. if not kbs:
  85. return
  86. d = kbs[0].to_dict()
  87. # d["embd_id"] = kbs[0].tenant.embd_id
  88. return d
  89. @classmethod
  90. @DB.connection_context()
  91. def update_parser_config(cls, id, config):
  92. e, m = cls.get_by_id(id)
  93. if not e:
  94. raise LookupError(f"knowledgebase({id}) not found.")
  95. def dfs_update(old, new):
  96. for k, v in new.items():
  97. if k not in old:
  98. old[k] = v
  99. continue
  100. if isinstance(v, dict):
  101. assert isinstance(old[k], dict)
  102. dfs_update(old[k], v)
  103. elif isinstance(v, list):
  104. assert isinstance(old[k], list)
  105. old[k] = list(set(old[k] + v))
  106. else:
  107. old[k] = v
  108. dfs_update(m.parser_config, config)
  109. cls.update_by_id(id, {"parser_config": m.parser_config})
  110. @classmethod
  111. @DB.connection_context()
  112. def get_field_map(cls, ids):
  113. conf = {}
  114. for k in cls.get_by_ids(ids):
  115. if k.parser_config and "field_map" in k.parser_config:
  116. conf.update(k.parser_config["field_map"])
  117. return conf
  118. @classmethod
  119. @DB.connection_context()
  120. def get_by_name(cls, kb_name, tenant_id):
  121. kb = cls.model.select().where(
  122. (cls.model.name == kb_name)
  123. & (cls.model.tenant_id == tenant_id)
  124. & (cls.model.status == StatusEnum.VALID.value)
  125. )
  126. if kb:
  127. return True, kb[0]
  128. return False, None
  129. @classmethod
  130. @DB.connection_context()
  131. def get_all_ids(cls):
  132. return [m["id"] for m in cls.model.select(cls.model.id).dicts()]
  133. @classmethod
  134. @DB.connection_context()
  135. def get_list(cls, joined_tenant_ids, user_id,
  136. page_number, items_per_page, orderby, desc, id, name):
  137. kbs = cls.model.select()
  138. if id:
  139. kbs = kbs.where(cls.model.id == id)
  140. if name:
  141. kbs = kbs.where(cls.model.name == name)
  142. kbs = kbs.where(
  143. ((cls.model.tenant_id.in_(joined_tenant_ids) & (cls.model.permission ==
  144. TenantPermission.TEAM.value)) | (
  145. cls.model.tenant_id == user_id))
  146. & (cls.model.status == StatusEnum.VALID.value)
  147. )
  148. if desc:
  149. kbs = kbs.order_by(cls.model.getter_by(orderby).desc())
  150. else:
  151. kbs = kbs.order_by(cls.model.getter_by(orderby).asc())
  152. kbs = kbs.paginate(page_number, items_per_page)
  153. return list(kbs.dicts())
  154. @classmethod
  155. @DB.connection_context()
  156. def accessible(cls, kb_id, user_id):
  157. docs = cls.model.select(
  158. cls.model.id).join(UserTenant, on=(UserTenant.tenant_id == Knowledgebase.tenant_id)
  159. ).where(cls.model.id == kb_id, UserTenant.user_id == user_id).paginate(0, 1)
  160. docs = docs.dicts()
  161. if not docs:
  162. return False
  163. return True
  164. @classmethod
  165. @DB.connection_context()
  166. def accessible4deletion(cls, kb_id, user_id):
  167. docs = cls.model.select(
  168. cls.model.id).where(cls.model.id == kb_id, cls.model.created_by == user_id).paginate(0, 1)
  169. docs = docs.dicts()
  170. if not docs:
  171. return False
  172. return True