From e862a1f04e520864feda1619c812c82fd8324e79 Mon Sep 17 00:00:00 2001 From: FireMMDC Date: Wed, 29 Nov 2023 22:06:50 -0500 Subject: [PATCH 1/3] Reworking create.py into a class, adding the ability to create a specific assistant, updating tool_maker/assistant_manager to use the new AgentBuilder class. Also tweaked the tool_creator_metadata and its usage within tool_maker/assistant_manger --- agents/agent_builder/create.py | 73 +++++++++++--------- agents/tool_maker/assistant_manager.py | 15 ++-- agents/tool_maker/tool_creator_metadata.json | 7 +- 3 files changed, 56 insertions(+), 39 deletions(-) diff --git a/agents/agent_builder/create.py b/agents/agent_builder/create.py index 595be56..396d924 100644 --- a/agents/agent_builder/create.py +++ b/agents/agent_builder/create.py @@ -3,39 +3,31 @@ from pathlib import Path from shared.openai_config import get_openai_client - -def create_assistants(): - agents_path = "agents" - client = get_openai_client() - - # Check if the 'agents' folder is empty or doesn't exist - if ( - not os.path.exists(agents_path) - or not os.path.isdir(agents_path) - or not os.listdir(agents_path) - ): - raise ValueError('The "agents" folder is missing, not a directory, or empty.') - - - existing_assistants = {} - - - for assistant in client.beta.assistants.list(limit=100): - existing_assistants[assistant.name] = assistant - - - # Iterate over each folder inside the 'agents' folder - for agent_name in os.listdir(agents_path): +class AgentBuilder: + + def __init__(self,client): + self.client = client + self.existing_assistants = {} + self.agents_path = "agents" + + def get_existing_assistants(self): + if not self.existing_assistants: + for assistant in self.client.beta.assistants.list(limit=100): + self.existing_assistants[assistant.name] = assistant + + def create_assistant(self, agent_name): current_file_path = Path(__file__).absolute().parent - agent_folder = os.path.join(current_file_path, agents_path, agent_name) + agent_folder = os.path.join(current_file_path, self.agents_path, agent_name) + #Could not create agent_name, they need to be defined in /agents/agent_builder/ print(agent_folder) existing_files = {} requested_files = [] existing_agent = {} - if agent_name in existing_assistants: - existing_agent = existing_assistants[agent_name] + self.get_existing_assistants() + if agent_name in self.existing_assistants: + existing_agent = self.existing_assistants[agent_name] for file_id in existing_agent.file_ids: - existing_file = client.files.retrieve(file_id=file_id) + existing_file = self.client.files.retrieve(file_id=file_id) existing_files[existing_file.filename] = existing_file @@ -65,7 +57,7 @@ def create_assistants(): file_path = os.path.join(files_folder, filename) with open(file_path, 'rb') as file_data: # Upload each file to OpenAI - file_object = client.files.create( + file_object = self.client.files.create( file=file_data, purpose='assistants' ) files.append({"name": filename, "id": file_object.id}) @@ -118,7 +110,7 @@ def create_assistants(): if len(update_params) != 0: print(f"Updating {agent_name}'s { ','.join(update_params.keys()) }") update_params['assistant_id'] = existing_agent.id - assistant = client.beta.assistants.update(**update_params) + assistant = self.client.beta.assistants.update(**update_params) else: print(f"{agent_name} is up to date") else: @@ -137,8 +129,25 @@ def create_assistants(): create_params['file_ids'] = list(map(lambda x: x['id'], files)) # Create the assistant using the uploaded file IDs if files exist - assistant = client.beta.assistants.create(**create_params) + assistant = self.client.beta.assistants.create(**create_params) print("***********************************************") -n + + def create_assistants(self): + # Check if the 'agents' folder is empty or doesn't exist + if ( + not os.path.exists(self.agents_path) + or not os.path.isdir(self.agents_path) + or not os.listdir(self.agents_path) + ): + raise ValueError('The "agents" folder is missing, not a directory, or empty.') + + self.get_existing_assistants() + + # Iterate over each folder inside the 'agents' folder + for agent_name in os.listdir(self.agents_path): + self.create_assistant(agent_name) + if __name__ == '__main__': - create_assistants() \ No newline at end of file + client = get_openai_client() + agent_builder = AgentBuilder(client=client) + agent_builder.create_assistants() \ No newline at end of file diff --git a/agents/tool_maker/assistant_manager.py b/agents/tool_maker/assistant_manager.py index 0e111fd..f92397d 100644 --- a/agents/tool_maker/assistant_manager.py +++ b/agents/tool_maker/assistant_manager.py @@ -2,14 +2,14 @@ from pathlib import Path import os import json -from agents.agent_builder.create import create_assistants +from agents.agent_builder.create import AgentBuilder class AssistantManager: def __init__(self, client): - create_assistants() self.client = client self.assistant = None + self.agent_builder = AgentBuilder(client=self.client) Path(__file__).absolute().parent tools_path = os.path.join( Path(__file__).absolute().parent, "tool_creator_metadata.json" @@ -19,24 +19,27 @@ def __init__(self, client): def get_assistant(self): """Retrieve or create an assistant for testing this functionality""" - if not self.assistant_package["name"] in [ + name = self.assistant_package["creator"]["name"] + self.agent_builder.create_assistant(name) + if not name in [ assistant.name for assistant in self.client.beta.assistants.list() ]: - raise ValueError(f'{self.assistant_package["name"]} needs to be created using create.py in /agents/agent_builder/') + raise ValueError(f'{name} needs to be created using create.py in /agents/agent_builder/') else: assistant_dict = { assistant.name: assistant.id for assistant in self.client.beta.assistants.list() } assistant = self.client.beta.assistants.retrieve( - assistant_id=assistant_dict[self.assistant_package["name"]] + assistant_id=assistant_dict[name] ) self.assistant = assistant return assistant def get_coding_assistant(self): """Retrieve or create an assistant for testing this functionality""" - name = "temporary_function_writer" + name = self.assistant_package["writer"]["name"] + self.agent_builder.create_assistant(name) if not name in [ assistant.name for assistant in self.client.beta.assistants.list() ]: diff --git a/agents/tool_maker/tool_creator_metadata.json b/agents/tool_maker/tool_creator_metadata.json index 5b0e99c..977fcaa 100644 --- a/agents/tool_maker/tool_creator_metadata.json +++ b/agents/tool_maker/tool_creator_metadata.json @@ -1,3 +1,8 @@ { - "name": "tool_creator" + "creator": { + "name": "tool_creator" + }, + "writer": { + "name": "temporary_function_writer" + } } \ No newline at end of file From 825ddaefd142b896ff6c1b7261217605b0158307 Mon Sep 17 00:00:00 2001 From: FireMMDC Date: Wed, 29 Nov 2023 22:26:53 -0500 Subject: [PATCH 2/3] Adding an error message, if a specific agent name does not exist in the repo --- agents/agent_builder/create.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/agents/agent_builder/create.py b/agents/agent_builder/create.py index 396d924..b91d6e1 100644 --- a/agents/agent_builder/create.py +++ b/agents/agent_builder/create.py @@ -18,7 +18,14 @@ def get_existing_assistants(self): def create_assistant(self, agent_name): current_file_path = Path(__file__).absolute().parent agent_folder = os.path.join(current_file_path, self.agents_path, agent_name) - #Could not create agent_name, they need to be defined in /agents/agent_builder/ + + if ( + not os.path.exists(agent_folder) + or not os.path.isdir(agent_folder) + or not os.listdir(agent_folder) + ): + raise ValueError(f'{agent_folder} is missing, not a directory, or empty.') + print(agent_folder) existing_files = {} requested_files = [] @@ -150,4 +157,5 @@ def create_assistants(self): if __name__ == '__main__': client = get_openai_client() agent_builder = AgentBuilder(client=client) - agent_builder.create_assistants() \ No newline at end of file + agent_builder.create_assistant("tom") + #agent_builder.create_assistants() \ No newline at end of file From 01f275f48c2f2e4ebfd08fa1ba2ad9352fc95bbf Mon Sep 17 00:00:00 2001 From: FireMMDC Date: Wed, 29 Nov 2023 22:28:55 -0500 Subject: [PATCH 3/3] Removing test code --- agents/agent_builder/create.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/agents/agent_builder/create.py b/agents/agent_builder/create.py index b91d6e1..d74555b 100644 --- a/agents/agent_builder/create.py +++ b/agents/agent_builder/create.py @@ -18,7 +18,7 @@ def get_existing_assistants(self): def create_assistant(self, agent_name): current_file_path = Path(__file__).absolute().parent agent_folder = os.path.join(current_file_path, self.agents_path, agent_name) - + if ( not os.path.exists(agent_folder) or not os.path.isdir(agent_folder) @@ -157,5 +157,4 @@ def create_assistants(self): if __name__ == '__main__': client = get_openai_client() agent_builder = AgentBuilder(client=client) - agent_builder.create_assistant("tom") - #agent_builder.create_assistants() \ No newline at end of file + agent_builder.create_assistants() \ No newline at end of file