You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kb_service.py 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #
  2. # Copyright 2019 The RAG Flow 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. import peewee
  17. from werkzeug.security import generate_password_hash, check_password_hash
  18. from web_server.db import TenantPermission
  19. from web_server.db.db_models import DB, UserTenant, Tenant
  20. from web_server.db.db_models import Knowledgebase
  21. from web_server.db.services.common_service import CommonService
  22. from web_server.utils import get_uuid, get_format_time
  23. from web_server.db.db_utils import StatusEnum
  24. class KnowledgebaseService(CommonService):
  25. model = Knowledgebase
  26. @classmethod
  27. @DB.connection_context()
  28. def get_by_tenant_ids(cls, joined_tenant_ids, user_id,
  29. page_number, items_per_page, orderby, desc):
  30. kbs = cls.model.select().where(
  31. ((cls.model.tenant_id.in_(joined_tenant_ids) & (cls.model.permission ==
  32. TenantPermission.TEAM.value)) | (cls.model.tenant_id == user_id))
  33. & (cls.model.status == StatusEnum.VALID.value)
  34. )
  35. if desc:
  36. kbs = kbs.order_by(cls.model.getter_by(orderby).desc())
  37. else:
  38. kbs = kbs.order_by(cls.model.getter_by(orderby).asc())
  39. kbs = kbs.paginate(page_number, items_per_page)
  40. return list(kbs.dicts())
  41. @classmethod
  42. @DB.connection_context()
  43. def get_detail(cls, kb_id):
  44. fields = [
  45. cls.model.id,
  46. Tenant.embd_id,
  47. cls.model.avatar,
  48. cls.model.name,
  49. cls.model.description,
  50. cls.model.permission,
  51. cls.model.doc_num,
  52. cls.model.token_num,
  53. cls.model.chunk_num,
  54. cls.model.parser_id]
  55. kbs = cls.model.select(*fields).join(Tenant, on=((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