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.

document_service.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 peewee import Expression
  17. from api.db import TenantPermission, FileType, TaskStatus
  18. from api.db.db_models import DB, Knowledgebase, Tenant
  19. from api.db.db_models import Document
  20. from api.db.services.common_service import CommonService
  21. from api.db.services.knowledgebase_service import KnowledgebaseService
  22. from api.db import StatusEnum
  23. class DocumentService(CommonService):
  24. model = Document
  25. @classmethod
  26. @DB.connection_context()
  27. def get_by_kb_id(cls, kb_id, page_number, items_per_page,
  28. orderby, desc, keywords):
  29. if keywords:
  30. docs = cls.model.select().where(
  31. cls.model.kb_id == kb_id,
  32. cls.model.name.like(f"%%{keywords}%%"))
  33. else:
  34. docs = cls.model.select().where(cls.model.kb_id == kb_id)
  35. count = docs.count()
  36. if desc:
  37. docs = docs.order_by(cls.model.getter_by(orderby).desc())
  38. else:
  39. docs = docs.order_by(cls.model.getter_by(orderby).asc())
  40. docs = docs.paginate(page_number, items_per_page)
  41. return list(docs.dicts()), count
  42. @classmethod
  43. @DB.connection_context()
  44. def insert(cls, doc):
  45. if not cls.save(**doc):
  46. raise RuntimeError("Database error (Document)!")
  47. e, doc = cls.get_by_id(doc["id"])
  48. if not e:
  49. raise RuntimeError("Database error (Document retrieval)!")
  50. e, kb = KnowledgebaseService.get_by_id(doc.kb_id)
  51. if not KnowledgebaseService.update_by_id(
  52. kb.id, {"doc_num": kb.doc_num + 1}):
  53. raise RuntimeError("Database error (Knowledgebase)!")
  54. return doc
  55. @classmethod
  56. @DB.connection_context()
  57. def get_newly_uploaded(cls, tm, mod=0, comm=1, items_per_page=64):
  58. fields = [cls.model.id, cls.model.kb_id, cls.model.parser_id, cls.model.name, cls.model.type, cls.model.location, cls.model.size, Knowledgebase.tenant_id, Tenant.embd_id, Tenant.img2txt_id, Tenant.asr_id, cls.model.update_time]
  59. docs = cls.model.select(*fields) \
  60. .join(Knowledgebase, on=(cls.model.kb_id == Knowledgebase.id)) \
  61. .join(Tenant, on=(Knowledgebase.tenant_id == Tenant.id))\
  62. .where(
  63. cls.model.status == StatusEnum.VALID.value,
  64. ~(cls.model.type == FileType.VIRTUAL.value),
  65. cls.model.progress == 0,
  66. cls.model.update_time >= tm,
  67. cls.model.run == TaskStatus.RUNNING.value,
  68. (Expression(cls.model.create_time, "%%", comm) == mod))\
  69. .order_by(cls.model.update_time.asc())\
  70. .paginate(1, items_per_page)
  71. return list(docs.dicts())
  72. @classmethod
  73. @DB.connection_context()
  74. def get_unfinished_docs(cls):
  75. fields = [cls.model.id, cls.model.process_begin_at]
  76. docs = cls.model.select(*fields) \
  77. .where(
  78. cls.model.status == StatusEnum.VALID.value,
  79. ~(cls.model.type == FileType.VIRTUAL.value),
  80. cls.model.progress < 1,
  81. cls.model.progress > 0)
  82. return list(docs.dicts())
  83. @classmethod
  84. @DB.connection_context()
  85. def increment_chunk_num(cls, doc_id, kb_id, token_num, chunk_num, duation):
  86. num = cls.model.update(token_num=cls.model.token_num + token_num,
  87. chunk_num=cls.model.chunk_num + chunk_num,
  88. process_duation=cls.model.process_duation+duation).where(
  89. cls.model.id == doc_id).execute()
  90. if num == 0:raise LookupError("Document not found which is supposed to be there")
  91. num = Knowledgebase.update(token_num=Knowledgebase.token_num+token_num, chunk_num=Knowledgebase.chunk_num+chunk_num).where(Knowledgebase.id==kb_id).execute()
  92. return num
  93. @classmethod
  94. @DB.connection_context()
  95. def get_tenant_id(cls, doc_id):
  96. docs = cls.model.select(Knowledgebase.tenant_id).join(Knowledgebase, on=(Knowledgebase.id == cls.model.kb_id)).where(cls.model.id == doc_id, Knowledgebase.status==StatusEnum.VALID.value)
  97. docs = docs.dicts()
  98. if not docs:return
  99. return docs[0]["tenant_id"]