From 542a8c7a1e279f9ccab4f4103e59551389cc9276 Mon Sep 17 00:00:00 2001 From: Arsenii Shatokhin Date: Sat, 13 Jan 2024 06:49:32 +0400 Subject: [PATCH] Fixed loading assistants from id #44 --- agency_swarm/agency/agency.py | 3 ++- agency_swarm/agents/agent.py | 8 +++++++- tests/test_agency.py | 20 +++++++++++++++++++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/agency_swarm/agency/agency.py b/agency_swarm/agency/agency.py index cf4b3cb5..b49249f2 100644 --- a/agency_swarm/agency/agency.py +++ b/agency_swarm/agency/agency.py @@ -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() diff --git a/agency_swarm/agents/agent.py b/agency_swarm/agents/agent.py index cf4c1b6c..7e8f41ec 100644 --- a/agency_swarm/agents/agent.py +++ b/agency_swarm/agents/agent.py @@ -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. @@ -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() diff --git a/tests/test_agency.py b/tests/test_agency.py index 06610a97..06c4ef2d 100644 --- a/tests/test_agency.py +++ b/tests/test_agency.py @@ -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)