Skip to content

Commit

Permalink
Fixed loading assistants from id #44
Browse files Browse the repository at this point in the history
  • Loading branch information
VRSEN committed Jan 13, 2024
1 parent 889c5ee commit 542a8c7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
3 changes: 2 additions & 1 deletion agency_swarm/agency/agency.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,8 @@ def _init_agents(self):
There are no output parameters as this method is used for internal initialization purposes within the Agency class.
"""
for agent in self.agents:
agent.id = None
if "temp_id" in agent.id:
agent.id = None
agent.add_shared_instructions(self.shared_instructions)
agent.init_oai()

Expand Down
8 changes: 7 additions & 1 deletion agency_swarm/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(self, id: str = None, name: str = None, description: str = None, in
Initializes an Agent with specified attributes, tools, and OpenAI client.
Parameters:
id (str, optional): Unique identifier for the agent. Defaults to None.
id (str, optional): Loads the assistant from OpenAI assistant ID. Assistant will be created or loaded from settings if ID is not provided. Defaults to None.
name (str, optional): Name of the agent. Defaults to the class name if not provided.
description (str, optional): A brief description of the agent's purpose. Defaults to None.
instructions (str, optional): Path to a file containing specific instructions for the agent. Defaults to an empty string.
Expand Down Expand Up @@ -89,6 +89,12 @@ def init_oai(self):
# load assistant from id
if self.id:
self.assistant = self.client.beta.assistants.retrieve(self.id)
self.instructions = self.assistant.instructions
self.name = self.assistant.name
self.description = self.assistant.description
self.file_ids = self.assistant.file_ids
self.metadata = self.assistant.metadata
self.model = self.assistant.model
# update assistant if parameters are different
if not self._check_parameters(self.assistant.model_dump()):
self._update_assistant()
Expand Down
20 changes: 19 additions & 1 deletion tests/test_agency.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,25 @@ def test_2_load_agent(self):

self.check_agent_settings(agent3)

def test_3_agent_communication(self):
def test_3_load_agent_id(self):
"""it should load existing assistant from id"""
from test_agents import TestAgent1
agent3 = Agent(id=self.__class__.agent1.id, instructions=self.__class__.agent1.instructions)
agent3.add_shared_instructions(self.__class__.agency.shared_instructions)
agent3.tools = self.__class__.agent1.tools
agent3 = agent3.init_oai()

print("agent3", agent3.assistant.model_dump())
print("agent1", self.__class__.agent1.assistant.model_dump())

self.assertTrue(self.__class__.agent1.id == agent3.id)

# check that assistant settings match
self.assertTrue(agent3._check_parameters(self.__class__.agent1.assistant.model_dump()))

self.check_agent_settings(agent3)

def test_4_agent_communication(self):
"""it should communicate between agents"""
print("TestAgent1 tools", self.__class__.agent1.tools)
message = self.__class__.agency.get_completion("Please tell TestAgent1 to say test to TestAgent2.", yield_messages=False)
Expand Down

0 comments on commit 542a8c7

Please sign in to comment.