(fix:openai) image uplads, use lambda in process_files

This commit is contained in:
ManishMadan2882
2025-04-18 18:27:02 +05:30
parent 5aa51f5f36
commit c8efef8f04
3 changed files with 15 additions and 20 deletions

View File

@@ -1,7 +1,5 @@
import json
import base64
import os
import mimetypes
import logging
from application.core.settings import settings
@@ -79,6 +77,8 @@ class OpenAILLM(BaseLLM):
content_parts.append(item)
elif "type" in item and item["type"] == "file" and "file" in item:
content_parts.append(item)
elif "type" in item and item["type"] == "image_url" and "image_url" in item:
content_parts.append(item)
cleaned_messages.append({"role": role, "content": content_parts})
else:
raise ValueError(
@@ -223,7 +223,6 @@ class OpenAILLM(BaseLLM):
elif mime_type == 'application/pdf':
try:
file_id = self._upload_file_to_openai(attachment)
prepared_messages[user_message_index]["content"].append({
"type": "file",
"file": {"file_id": file_id}
@@ -282,17 +281,13 @@ class OpenAILLM(BaseLLM):
raise FileNotFoundError(f"File not found: {file_path}")
try:
# Use storage's process_file method to handle the file appropriately
def upload_to_openai(file_path, **kwargs):
with open(file_path, 'rb') as file:
logging.info(f"Uploading file to OpenAI: {file_path}")
response = self.client.files.create(
file=file,
purpose="assistants"
)
return response.id
file_id = self.storage.process_file(file_path, upload_to_openai)
file_id = self.storage.process_file(
file_path,
lambda local_path, **kwargs: self.client.files.create(
file=open(local_path, 'rb'),
purpose="assistants"
).id
)
from application.core.mongo_db import MongoDB
mongo = MongoDB.get_client()

View File

@@ -100,4 +100,4 @@ class LocalStorage(BaseStorage):
if not os.path.exists(full_path):
raise FileNotFoundError(f"File not found: {full_path}")
return processor_func(file_path=full_path, **kwargs)
return processor_func(local_path=full_path, **kwargs)

View File

@@ -98,23 +98,23 @@ class S3Storage(BaseStorage):
path: Path to the file
processor_func: Function that processes the file
**kwargs: Additional arguments to pass to the processor function
Returns:
The result of the processor function
"""
import tempfile
import logging
if not self.file_exists(path):
raise FileNotFoundError(f"File not found in S3: {path}")
with tempfile.NamedTemporaryFile(suffix=os.path.splitext(path)[1], delete=True) as temp_file:
try:
# Download the file from S3 to the temporary file
self.s3.download_fileobj(self.bucket_name, path, temp_file)
temp_file.flush()
result = processor_func(file_path=temp_file.name, **kwargs)
return result
return processor_func(local_path=temp_file.name, **kwargs)
except Exception as e:
logging.error(f"Error processing S3 file {path}: {e}", exc_info=True)
raise