Fix process/files/batch

This commit is contained in:
Gabriel Ecegi 2024-12-14 10:45:27 +01:00
parent f2e2b59c18
commit 440894f8d3
2 changed files with 18 additions and 18 deletions

View File

@ -1062,30 +1062,24 @@ def process_files_batch(
Process a batch of files and save them to the vector database.
"""
results: List[BatchProcessFilesResult] = []
errors: List[BatchProcessFilesResult] = []
errors: List[BatchProcessFilesResult] = []
collection_name = form_data.collection_name
# Prepare all documents first
all_docs: List[Document] = []
for file_request in form_data.files:
for file in form_data.files:
try:
file = Files.get_file_by_id(file_request.file_id)
if not file:
log.error(f"process_files_batch: File {file_request.file_id} not found")
raise ValueError(f"File {file_request.file_id} not found")
text_content = file_request.content
text_content = file.data.get("content", "")
docs: List[Document] = [
Document(
page_content=text_content.replace("<br/>", "\n"),
metadata={
**file.meta,
"name": file_request.filename,
"name": file.filename,
"created_by": file.user_id,
"file_id": file.id,
"source": file_request.filename,
"source": file.filename,
},
)
]
@ -1101,9 +1095,9 @@ def process_files_batch(
))
except Exception as e:
log.error(f"process_files_batch: Error processing file {file_request.file_id}: {str(e)}")
log.error(f"process_files_batch: Error processing file {file.id}: {str(e)}")
errors.append(BatchProcessFilesResult(
file_id=file_request.file_id,
file_id=file.id,
status="failed",
error=str(e)
))
@ -1139,7 +1133,6 @@ def process_files_batch(
errors=errors
)
class ProcessTextForm(BaseModel):
name: str
content: str

View File

@ -548,10 +548,17 @@ def add_files_to_knowledge_batch(
files.append(file)
# Process files
result = process_files_batch(BatchProcessFilesForm(
files=files,
collection_name=id
))
try:
result = process_files_batch(BatchProcessFilesForm(
files=files,
collection_name=id
))
except Exception as e:
log.error(f"add_files_to_knowledge_batch: Exception occurred: {e}", exc_info=True)
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=str(e)
)
# Add successful files to knowledge base
data = knowledge.data or {}