Skip to content

Commit

Permalink
Adjusted logic for uploading message files
Browse files Browse the repository at this point in the history
  • Loading branch information
VRSEN committed Aug 27, 2024
1 parent 53a0e62 commit 2107467
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
8 changes: 3 additions & 5 deletions agency_swarm/agency/agency.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from agency_swarm.tools import BaseTool, CodeInterpreter, FileSearch
from agency_swarm.user import User
from agency_swarm.util.errors import RefusalError
from agency_swarm.util.files import determine_file_type, get_tools
from agency_swarm.util.files import get_tools, get_file_purpose
from agency_swarm.util.shared_state import SharedState
from agency_swarm.util.streaming import AgencyEventHandler

Expand Down Expand Up @@ -330,9 +330,7 @@ def handle_file_upload(file_list):
if file_list:
try:
for file_obj in file_list:
file_type = determine_file_type(file_obj.name)
purpose = "assistants" if file_type != "vision" else "vision"
tools = [{"type": "code_interpreter"}] if file_type == "assistants.code_interpreter" else [{"type": "file_search"}]
purpose = get_file_purpose(file_obj.name)

with open(file_obj.name, 'rb') as f:
# Upload the file to OpenAI
Expand All @@ -341,7 +339,7 @@ def handle_file_upload(file_list):
purpose=purpose
)

if file_type == "vision":
if purpose == "vision":
images.append({
"type": "image_file",
"image_file": {"file_id": file.id}
Expand Down
2 changes: 1 addition & 1 deletion agency_swarm/util/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .cli.create_agent_template import create_agent_template
from .cli.import_agent import import_agent
from .oai import set_openai_key, get_openai_client, set_openai_client
from .files import determine_file_type
from .files import get_tools, get_file_purpose
from .validators import llm_validator
15 changes: 9 additions & 6 deletions agency_swarm/util/files.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import mimetypes

image_types = [
"image/jpeg", "image/jpg", "image/png", "image/webp", "image/gif"
]

code_interpreter_types = [
"application/csv", "image/jpeg", "image/gif", "image/png",
"application/x-tar", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
Expand All @@ -17,18 +21,17 @@
"application/typescript"
]

def determine_file_type(file_path):
def get_file_purpose(file_path):
mime_type, _ = mimetypes.guess_type(file_path)
if mime_type:
if mime_type in code_interpreter_types:
return "assistants.code_interpreter"
elif mime_type.startswith('image/'):
if mime_type in image_types:
return "vision"
elif mime_type in dual_types:
return "assistants.file_search"
if mime_type in code_interpreter_types or mime_type in dual_types:
return "assistants"
raise ValueError(f"Unsupported file type: {mime_type}")

def get_tools(file_path):
"""Returns the tools for the given file path"""
mime_type, _ = mimetypes.guess_type(file_path)
if mime_type in code_interpreter_types:
return [{"type": "code_interpreter"}]
Expand Down

0 comments on commit 2107467

Please sign in to comment.