Skip to content

Commit 9b89e8f

Browse files
committed
refactor: enhance file parsing by saving source files and linking document IDs
1 parent 38c3dca commit 9b89e8f

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

apps/knowledge/serializers/document.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -863,17 +863,47 @@ def save_table(self, instance: Dict, with_valid=True):
863863
}).batch_save(document_list)
864864

865865
def parse_qa_file(self, file):
866+
# 保存源文件
867+
source_file_id = uuid.uuid7()
868+
source_file = File(
869+
id=source_file_id,
870+
file_name=file.name,
871+
source_type=FileSourceType.KNOWLEDGE,
872+
source_id=self.data.get('knowledge_id'),
873+
meta={}
874+
)
875+
source_file.save(file.read())
876+
file.seek(0)
877+
866878
get_buffer = FileBufferHandle().get_buffer
867879
for parse_qa_handle in parse_qa_handle_list:
868880
if parse_qa_handle.support(file, get_buffer):
869-
return parse_qa_handle.handle(file, get_buffer, self.save_image)
881+
documents = parse_qa_handle.handle(file, get_buffer, self.save_image)
882+
for doc in documents:
883+
doc['source_file_id'] = source_file_id
884+
return documents
870885
raise AppApiException(500, _('Unsupported file format'))
871886

872887
def parse_table_file(self, file):
888+
# 保存源文件
889+
source_file_id = uuid.uuid7()
890+
source_file = File(
891+
id=source_file_id,
892+
file_name=file.name,
893+
source_type=FileSourceType.KNOWLEDGE,
894+
source_id=self.data.get('knowledge_id'),
895+
meta={}
896+
)
897+
source_file.save(file.read())
898+
file.seek(0)
899+
873900
get_buffer = FileBufferHandle().get_buffer
874901
for parse_table_handle in parse_table_handle_list:
875902
if parse_table_handle.support(file, get_buffer):
876-
return parse_table_handle.handle(file, get_buffer, self.save_image)
903+
documents = parse_table_handle.handle(file, get_buffer, self.save_image)
904+
for doc in documents:
905+
doc['source_file_id'] = source_file_id
906+
return documents
877907
raise AppApiException(500, _('Unsupported file format'))
878908

879909
def save_image(self, image_list):

0 commit comments

Comments
 (0)