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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. from peewee import Expression
  17. from web_server.db import TenantPermission, FileType
  18. from web_server.db.db_models import DB, Knowledgebase, Tenant
  19. from web_server.db.db_models import Document
  20. from web_server.db.services.common_service import CommonService
  21. from web_server.db.services.kb_service import KnowledgebaseService
  22. from web_server.db.db_utils 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. if desc:
  36. docs = docs.order_by(cls.model.getter_by(orderby).desc())
  37. else:
  38. docs = docs.order_by(cls.model.getter_by(orderby).asc())
  39. docs = docs.paginate(page_number, items_per_page)
  40. return list(docs.dicts())
  41. @classmethod
  42. @DB.connection_context()
  43. def insert(cls, doc):
  44. if not cls.save(**doc):
  45. raise RuntimeError("Database error (Document)!")
  46. e, doc = cls.get_by_id(doc["id"])
  47. if not e:
  48. raise RuntimeError("Database error (Document retrieval)!")
  49. e, kb = KnowledgebaseService.get_by_id(doc.kb_id)
  50. if not KnowledgebaseService.update_by_id(
  51. kb.id, {"doc_num": kb.doc_num + 1}):
  52. raise RuntimeError("Database error (Knowledgebase)!")
  53. return doc
  54. @classmethod
  55. @DB.connection_context()
  56. def get_newly_uploaded(cls, tm, mod, comm, items_per_page=64):
  57. fields = [cls.model.id, cls.model.kb_id, cls.model.parser_id, cls.model.name, cls.model.location, cls.model.size, Knowledgebase.tenant_id, Tenant.embd_id, Tenant.img2txt_id, cls.model.update_time]
  58. docs = cls.model.select(*fields) \
  59. .join(Knowledgebase, on=(cls.model.kb_id == Knowledgebase.id)) \
  60. .join(Tenant, on=(Knowledgebase.tenant_id == Tenant.id))\
  61. .where(
  62. cls.model.status == StatusEnum.VALID.value,
  63. ~(cls.model.type == FileType.VIRTUAL.value),
  64. cls.model.progress == 0,
  65. cls.model.update_time >= tm,
  66. (Expression(cls.model.create_time, "%%", comm) == mod))\
  67. .order_by(cls.model.update_time.asc())\
  68. .paginate(1, items_per_page)
  69. return list(docs.dicts())
  70. @classmethod
  71. @DB.connection_context()
  72. def increment_chunk_num(cls, doc_id, kb_id, token_num, chunk_num, duation):
  73. num = cls.model.update(token_num=cls.model.token_num + token_num,
  74. chunk_num=cls.model.chunk_num + chunk_num,
  75. process_duation=cls.model.process_duation+duation).where(
  76. cls.model.id == doc_id).execute()
  77. if num == 0:raise LookupError("Document not found which is supposed to be there")
  78. num = Knowledgebase.update(token_num=Knowledgebase.token_num+token_num, chunk_num=Knowledgebase.chunk_num+chunk_num).where(Knowledgebase.id==kb_id).execute()
  79. return num
  80. @classmethod
  81. @DB.connection_context()
  82. def get_tenant_id(cls, doc_id):
  83. 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)
  84. docs = docs.dicts()
  85. if not docs:return
  86. return docs[0]["tenant_id"]