Skip to content

Commit

Permalink
Fix compat issues when initializing assistants created with v1 api, r…
Browse files Browse the repository at this point in the history
…emove udpates when initializing assistant from id #128
  • Loading branch information
VRSEN committed May 28, 2024
1 parent 8254ecf commit 83859b7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
30 changes: 17 additions & 13 deletions agency_swarm/agency/agency.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from agency_swarm.user import User
from agency_swarm.util.files import determine_file_type
from agency_swarm.util.shared_state import SharedState
from openai.types.beta.threads.runs.tool_call import ToolCall

from agency_swarm.util.streaming import AgencyEventHandler

Expand Down Expand Up @@ -258,31 +259,28 @@ 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"}]

with open(file_obj.name, 'rb') as f:
# Upload the file to OpenAI
file = self.main_thread.client.files.create(
file=f,
purpose="assistants"
purpose=purpose
)

file_type = determine_file_type(file_obj.name)

if file_type == "assistants.code_interpreter":
attachments.append({
"file_id": file.id,
"tools": [{"type": "code_interpreter"}]
})
elif file_type == "vision":
if file_type == "vision":
images.append({
"type": "image_file",
"image_file": {"file_id": file.id}
})
else:
attachments.append({
"file_id": file.id,
"tools": [{"type": "file_search"}]
"tools": tools
})

message_file_names.append(file.filename)
print(f"Uploaded file ID: {file.id}")
return attachments
Expand Down Expand Up @@ -358,7 +356,10 @@ def on_text_delta(self, delta, snapshot):
chatbot_queue.put(delta.value)

@override
def on_tool_call_created(self, tool_call):
def on_tool_call_created(self, tool_call: ToolCall):
if isinstance(tool_call, dict):
tool_call = ToolCall(**tool_call)

# TODO: add support for code interpreter and retirieval tools
if tool_call.type == "function":
chatbot_queue.put("[new_message]")
Expand All @@ -367,7 +368,10 @@ def on_tool_call_created(self, tool_call):
chatbot_queue.put(self.message_output.get_formatted_header() + "\n")

@override
def on_tool_call_done(self, snapshot):
def on_tool_call_done(self, snapshot: ToolCall):
if isinstance(snapshot, dict):
snapshot = ToolCall(**snapshot)

self.message_output = None

# TODO: add support for code interpreter and retirieval tools
Expand Down
21 changes: 17 additions & 4 deletions agency_swarm/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,22 @@ def init_oai(self):
self.model = self.model or self.assistant.model
self.tool_resources = self.tool_resources or self.assistant.tool_resources.model_dump()

# update assistant if parameters are different
if not self._check_parameters(self.assistant.model_dump()):
self._update_assistant()
for tool in self.assistant.tools:
if tool.type == "function":
# function tools must be added manually
continue
elif tool.type == "file_search":
self.add_tool(FileSearch)
elif tool.type == "code_interpreter":
self.add_tool(CodeInterpreter)
elif tool.type == "retrieval":
self.add_tool(Retrieval)
else:
raise Exception("Invalid tool type.")

# # update assistant if parameters are different
# if not self._check_parameters(self.assistant.model_dump()):
# self._update_assistant()

return self

Expand All @@ -213,7 +226,7 @@ def init_oai(self):

# update assistant if parameters are different
if not self._check_parameters(self.assistant.model_dump()):
print("Updating assistant... " + self.name)
print("Updating agent... " + self.name)
self._update_assistant()

if self.assistant.tool_resources:
Expand Down

0 comments on commit 83859b7

Please sign in to comment.