Skip to content

Commit

Permalink
Merge pull request #191 from FireMMDC/tool-maker-tool-lookup
Browse files Browse the repository at this point in the history
Translating create.py into a class, adding a function to create a sepcific assistant.
  • Loading branch information
OWigginsHay authored Dec 7, 2023
2 parents 37bd375 + fbf5cd4 commit 020b44e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 42 deletions.
86 changes: 51 additions & 35 deletions agents/agent_builder/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -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:
Expand All @@ -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()
client = get_openai_client()
agent_builder = AgentBuilder(client=client)
agent_builder.create_assistants()
15 changes: 9 additions & 6 deletions agents/tool_maker/assistant_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
]:
Expand Down
7 changes: 6 additions & 1 deletion agents/tool_maker/tool_creator_metadata.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"name": "tool_creator"
"creator": {
"name": "tool_creator"
},
"writer": {
"name": "temporary_function_writer"
}
}

0 comments on commit 020b44e

Please sign in to comment.