From c8efef8f04c08821657dc5427b7b21354d058ff7 Mon Sep 17 00:00:00 2001 From: ManishMadan2882 Date: Fri, 18 Apr 2025 18:27:02 +0530 Subject: [PATCH] (fix:openai) image uplads, use lambda in process_files --- application/llm/openai.py | 23 +++++++++-------------- application/storage/local.py | 2 +- application/storage/s3.py | 10 +++++----- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/application/llm/openai.py b/application/llm/openai.py index f36e87cb..87eb295b 100644 --- a/application/llm/openai.py +++ b/application/llm/openai.py @@ -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() diff --git a/application/storage/local.py b/application/storage/local.py index db11b63c..fb21f08d 100644 --- a/application/storage/local.py +++ b/application/storage/local.py @@ -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) diff --git a/application/storage/s3.py b/application/storage/s3.py index e8df210e..abc57c6d 100644 --- a/application/storage/s3.py +++ b/application/storage/s3.py @@ -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