您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

file2document_app.py 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.es_conn 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. if not DocumentService.remove_document(doc, tenant_id):
  57. return get_data_error_result(
  58. retmsg="Database error (Document removal)!")
  59. File2DocumentService.delete_by_file_id(id)
  60. # insert
  61. for kb_id in kb_ids:
  62. e, kb = KnowledgebaseService.get_by_id(kb_id)
  63. if not e:
  64. return get_data_error_result(
  65. retmsg="Can't find this knowledgebase!")
  66. e, file = FileService.get_by_id(id)
  67. if not e:
  68. return get_data_error_result(
  69. retmsg="Can't find this file!")
  70. doc = DocumentService.insert({
  71. "id": get_uuid(),
  72. "kb_id": kb.id,
  73. "parser_id": kb.parser_id,
  74. "parser_config": kb.parser_config,
  75. "created_by": current_user.id,
  76. "type": file.type,
  77. "name": file.name,
  78. "location": file.location,
  79. "size": file.size
  80. })
  81. FileService.set_constant_parser(doc, file.name)
  82. file2document = File2DocumentService.insert({
  83. "id": get_uuid(),
  84. "file_id": id,
  85. "document_id": doc.id,
  86. })
  87. file2documents.append(file2document.to_json())
  88. return get_json_result(data=file2documents)
  89. except Exception as e:
  90. return server_error_response(e)
  91. @manager.route('/rm', methods=['POST'])
  92. @login_required
  93. @validate_request("file_ids")
  94. def rm():
  95. req = request.json
  96. file_ids = req["file_ids"]
  97. if not file_ids:
  98. return get_json_result(
  99. data=False, retmsg='Lack of "Files ID"', retcode=RetCode.ARGUMENT_ERROR)
  100. try:
  101. for file_id in file_ids:
  102. informs = File2DocumentService.get_by_file_id(file_id)
  103. if not informs:
  104. return get_data_error_result(retmsg="Inform not found!")
  105. for inform in informs:
  106. if not inform:
  107. return get_data_error_result(retmsg="Inform not found!")
  108. File2DocumentService.delete_by_file_id(file_id)
  109. doc_id = inform.document_id
  110. e, doc = DocumentService.get_by_id(doc_id)
  111. if not e:
  112. return get_data_error_result(retmsg="Document not found!")
  113. tenant_id = DocumentService.get_tenant_id(doc_id)
  114. if not tenant_id:
  115. return get_data_error_result(retmsg="Tenant not found!")
  116. if not DocumentService.remove_document(doc, tenant_id):
  117. return get_data_error_result(
  118. retmsg="Database error (Document removal)!")
  119. return get_json_result(data=True)
  120. except Exception as e:
  121. return server_error_response(e)