diff --git a/agents/agent_builder/create.py b/agents/agent_builder/create.py index 8e4652c..4b5d7e8 100644 --- a/agents/agent_builder/create.py +++ b/agents/agent_builder/create.py @@ -3,43 +3,38 @@ from pathlib import Path from shared.openai_config import get_openai_client - -def create_assistants(): - agents_path = "agents" - client = get_openai_client() - - agents_path = os.path.join( - Path(__file__).absolute().parent, agents_path - ) - - # 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) + + 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 = [] 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 @@ -69,7 +64,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}) @@ -122,7 +117,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: @@ -141,8 +136,29 @@ 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("***********************************************") + def create_assistants(self): + agents_path = os.path.join( + Path(__file__).absolute().parent, self.agents_path + ) + + # 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(f'The "{self.agents_path}" 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(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