Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

knowledgebase_service.py 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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)) | (cls.model.tenant_id == user_id))
  28. & (cls.model.status == StatusEnum.VALID.value)
  29. )
  30. if desc:
  31. kbs = kbs.order_by(cls.model.getter_by(orderby).desc())
  32. else:
  33. kbs = kbs.order_by(cls.model.getter_by(orderby).asc())
  34. kbs = kbs.paginate(page_number, items_per_page)
  35. return list(kbs.dicts())
  36. @classmethod
  37. @DB.connection_context()
  38. def get_detail(cls, kb_id):
  39. fields = [
  40. cls.model.id,
  41. Tenant.embd_id,
  42. cls.model.avatar,
  43. cls.model.name,
  44. cls.model.language,
  45. cls.model.description,
  46. cls.model.permission,
  47. cls.model.doc_num,
  48. cls.model.token_num,
  49. cls.model.chunk_num,
  50. cls.model.parser_id,
  51. cls.model.parser_config]
  52. kbs = cls.model.select(*fields).join(Tenant, on=((Tenant.id == cls.model.tenant_id) & (Tenant.status == StatusEnum.VALID.value))).where(
  53. (cls.model.id == kb_id),
  54. (cls.model.status == StatusEnum.VALID.value)
  55. )
  56. if not kbs:
  57. return
  58. d = kbs[0].to_dict()
  59. d["embd_id"] = kbs[0].tenant.embd_id
  60. return d
  61. @classmethod
  62. @DB.connection_context()
  63. def update_parser_config(cls, id, config):
  64. e, m = cls.get_by_id(id)
  65. if not e:
  66. raise LookupError(f"knowledgebase({id}) not found.")
  67. def dfs_update(old, new):
  68. for k, v in new.items():
  69. if k not in old:
  70. old[k] = v
  71. continue
  72. if isinstance(v, dict):
  73. assert isinstance(old[k], dict)
  74. dfs_update(old[k], v)
  75. elif isinstance(v, list):
  76. assert isinstance(old[k], list)
  77. old[k] = list(set(old[k] + v))
  78. else:
  79. old[k] = v
  80. dfs_update(m.parser_config, config)
  81. cls.update_by_id(id, {"parser_config": m.parser_config})
  82. @classmethod
  83. @DB.connection_context()
  84. def get_field_map(cls, ids):
  85. conf = {}
  86. for k in cls.get_by_ids(ids):
  87. if k.parser_config and "field_map" in k.parser_config:
  88. conf.update(k.parser_config["field_map"])
  89. return conf