diff --git a/agency_swarm/agency/agency.py b/agency_swarm/agency/agency.py index 537572c3..94c6f23f 100644 --- a/agency_swarm/agency/agency.py +++ b/agency_swarm/agency/agency.py @@ -383,7 +383,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) if self.shared_files: diff --git a/agency_swarm/agents/agent.py b/agency_swarm/agents/agent.py index bf636423..2dd3b801 100644 --- a/agency_swarm/agents/agent.py +++ b/agency_swarm/agents/agent.py @@ -36,7 +36,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. @@ -94,6 +94,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..1776cad7 100644 --- a/tests/test_agency.py +++ b/tests/test_agency.py @@ -96,7 +96,24 @@ 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) + 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)