Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

knowledgebase_service.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_detail(cls, kb_id):
  40. fields = [
  41. cls.model.id,
  42. #Tenant.embd_id,
  43. cls.model.embd_id,
  44. cls.model.avatar,
  45. cls.model.name,
  46. cls.model.language,
  47. cls.model.description,
  48. cls.model.permission,
  49. cls.model.doc_num,
  50. cls.model.token_num,
  51. cls.model.chunk_num,
  52. cls.model.parser_id,
  53. cls.model.parser_config]
  54. kbs = cls.model.select(*fields).join(Tenant, on=(
  55. (Tenant.id == cls.model.tenant_id) & (Tenant.status == StatusEnum.VALID.value))).where(
  56. (cls.model.id == kb_id),
  57. (cls.model.status == StatusEnum.VALID.value)
  58. )
  59. if not kbs:
  60. return
  61. d = kbs[0].to_dict()
  62. #d["embd_id"] = kbs[0].tenant.embd_id
  63. return d
  64. @classmethod
  65. @DB.connection_context()
  66. def update_parser_config(cls, id, config):
  67. e, m = cls.get_by_id(id)
  68. if not e:
  69. raise LookupError(f"knowledgebase({id}) not found.")
  70. def dfs_update(old, new):
  71. for k, v in new.items():
  72. if k not in old:
  73. old[k] = v
  74. continue
  75. if isinstance(v, dict):
  76. assert isinstance(old[k], dict)
  77. dfs_update(old[k], v)
  78. elif isinstance(v, list):
  79. assert isinstance(old[k], list)
  80. old[k] = list(set(old[k] + v))
  81. else:
  82. old[k] = v
  83. dfs_update(m.parser_config, config)
  84. cls.update_by_id(id, {"parser_config": m.parser_config})
  85. @classmethod
  86. @DB.connection_context()
  87. def get_field_map(cls, ids):
  88. conf = {}
  89. for k in cls.get_by_ids(ids):
  90. if k.parser_config and "field_map" in k.parser_config:
  91. conf.update(k.parser_config["field_map"])
  92. return conf
  93. @classmethod
  94. @DB.connection_context()
  95. def get_by_name(cls, kb_name, tenant_id):
  96. kb = cls.model.select().where(
  97. (cls.model.name == kb_name)
  98. & (cls.model.tenant_id == tenant_id)
  99. & (cls.model.status == StatusEnum.VALID.value)
  100. )
  101. if kb:
  102. return True, kb[0]
  103. return False, None
  104. @classmethod
  105. @DB.connection_context()
  106. def get_all_ids(cls):
  107. return [m["id"] for m in cls.model.select(cls.model.id).dicts()]