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.

file2document_app.py 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 elasticsearch_dsl import Q
  17. from api.db.db_models import File2Document
  18. from api.db.services.file2document_service import File2DocumentService
  19. from api.db.services.file_service import FileService
  20. from flask import request
  21. from flask_login import login_required, current_user
  22. from api.db.services.knowledgebase_service import KnowledgebaseService
  23. from api.utils.api_utils import server_error_response, get_data_error_result, validate_request
  24. from api.utils import get_uuid
  25. from api.db import FileType
  26. from api.db.services.document_service import DocumentService
  27. from api.settings import RetCode
  28. from api.utils.api_utils import get_json_result
  29. from rag.nlp import search
  30. from rag.utils import ELASTICSEARCH
  31. @manager.route('/convert', methods=['POST'])
  32. @login_required
  33. @validate_request("file_ids", "kb_ids")
  34. def convert():
  35. req = request.json
  36. kb_ids = req["kb_ids"]
  37. file_ids = req["file_ids"]
  38. file2documents = []
  39. try:
  40. for file_id in file_ids:
  41. e, file = FileService.get_by_id(file_id)
  42. file_ids_list = [file_id]
  43. if file.type == FileType.FOLDER.value:
  44. file_ids_list = FileService.get_all_innermost_file_ids(file_id, [])
  45. for id in file_ids_list:
  46. informs = File2DocumentService.get_by_file_id(id)
  47. # delete
  48. for inform in informs:
  49. doc_id = inform.document_id
  50. e, doc = DocumentService.get_by_id(doc_id)
  51. if not e:
  52. return get_data_error_result(retmsg="Document not found!")
  53. tenant_id = DocumentService.get_tenant_id(doc_id)
  54. if not tenant_id:
  55. return get_data_error_result(retmsg="Tenant not found!")
  56. ELASTICSEARCH.deleteByQuery(
  57. Q("match", doc_id=doc.id), idxnm=search.index_name(tenant_id))
  58. DocumentService.increment_chunk_num(
  59. doc.id, doc.kb_id, doc.token_num * -1, doc.chunk_num * -1, 0)
  60. if not DocumentService.delete(doc):
  61. return get_data_error_result(
  62. retmsg="Database error (Document removal)!")
  63. File2DocumentService.delete_by_file_id(id)
  64. # insert
  65. for kb_id in kb_ids:
  66. e, kb = KnowledgebaseService.get_by_id(kb_id)
  67. if not e:
  68. return get_data_error_result(
  69. retmsg="Can't find this knowledgebase!")
  70. e, file = FileService.get_by_id(id)
  71. if not e:
  72. return get_data_error_result(
  73. retmsg="Can't find this file!")
  74. doc = DocumentService.insert({
  75. "id": get_uuid(),
  76. "kb_id": kb.id,
  77. "parser_id": kb.parser_id,
  78. "parser_config": kb.parser_config,
  79. "created_by": current_user.id,
  80. "type": file.type,
  81. "name": file.name,
  82. "location": file.location,
  83. "size": file.size
  84. })
  85. file2document = File2DocumentService.insert({
  86. "id": get_uuid(),
  87. "file_id": id,
  88. "document_id": doc.id,
  89. })
  90. file2documents.append(file2document.to_json())
  91. return get_json_result(data=file2documents)
  92. except Exception as e:
  93. return server_error_response(e)
  94. @manager.route('/rm', methods=['POST'])
  95. @login_required
  96. @validate_request("file_ids")
  97. def rm():
  98. req = request.json
  99. file_ids = req["file_ids"]
  100. if not file_ids:
  101. return get_json_result(
  102. data=False, retmsg='Lack of "Files ID"', retcode=RetCode.ARGUMENT_ERROR)
  103. try:
  104. for file_id in file_ids:
  105. informs = File2DocumentService.get_by_file_id(file_id)
  106. if not informs:
  107. return get_data_error_result(retmsg="Inform not found!")
  108. for inform in informs:
  109. if not inform:
  110. return get_data_error_result(retmsg="Inform not found!")
  111. File2DocumentService.delete_by_file_id(file_id)
  112. doc_id = inform.document_id
  113. e, doc = DocumentService.get_by_id(doc_id)
  114. if not e:
  115. return get_data_error_result(retmsg="Document not found!")
  116. tenant_id = DocumentService.get_tenant_id(doc_id)
  117. if not tenant_id:
  118. return get_data_error_result(retmsg="Tenant not found!")
  119. ELASTICSEARCH.deleteByQuery(
  120. Q("match", doc_id=doc.id), idxnm=search.index_name(tenant_id))
  121. DocumentService.increment_chunk_num(
  122. doc.id, doc.kb_id, doc.token_num * -1, doc.chunk_num * -1, 0)
  123. if not DocumentService.delete(doc):
  124. return get_data_error_result(
  125. retmsg="Database error (Document removal)!")
  126. return get_json_result(data=True)
  127. except Exception as e:
  128. return server_error_response(e)