Skip to content

Commit 020b44e

Browse files
authored
Merge pull request #191 from FireMMDC/tool-maker-tool-lookup
Translating create.py into a class, adding a function to create a sepcific assistant.
2 parents 37bd375 + fbf5cd4 commit 020b44e

File tree

3 files changed

+66
-42
lines changed

3 files changed

+66
-42
lines changed

agents/agent_builder/create.py

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,38 @@
33
from pathlib import Path
44
from shared.openai_config import get_openai_client
55

6-
7-
def create_assistants():
8-
agents_path = "agents"
9-
client = get_openai_client()
10-
11-
agents_path = os.path.join(
12-
Path(__file__).absolute().parent, agents_path
13-
)
14-
15-
# Check if the 'agents' folder is empty or doesn't exist
16-
if (
17-
not os.path.exists(agents_path)
18-
or not os.path.isdir(agents_path)
19-
or not os.listdir(agents_path)
20-
):
21-
raise ValueError('The "agents" folder is missing, not a directory, or empty.')
22-
23-
24-
existing_assistants = {}
25-
26-
27-
for assistant in client.beta.assistants.list(limit=100):
28-
existing_assistants[assistant.name] = assistant
29-
30-
31-
# Iterate over each folder inside the 'agents' folder
32-
for agent_name in os.listdir(agents_path):
6+
class AgentBuilder:
7+
8+
def __init__(self,client):
9+
self.client = client
10+
self.existing_assistants = {}
11+
self.agents_path = "agents"
12+
13+
def get_existing_assistants(self):
14+
if not self.existing_assistants:
15+
for assistant in self.client.beta.assistants.list(limit=100):
16+
self.existing_assistants[assistant.name] = assistant
17+
18+
def create_assistant(self, agent_name):
3319
current_file_path = Path(__file__).absolute().parent
34-
agent_folder = os.path.join(current_file_path, agents_path, agent_name)
20+
agent_folder = os.path.join(current_file_path, self.agents_path, agent_name)
21+
22+
if (
23+
not os.path.exists(agent_folder)
24+
or not os.path.isdir(agent_folder)
25+
or not os.listdir(agent_folder)
26+
):
27+
raise ValueError(f'{agent_folder} is missing, not a directory, or empty.')
28+
3529
print(agent_folder)
3630
existing_files = {}
3731
requested_files = []
3832
existing_agent = {}
39-
if agent_name in existing_assistants:
40-
existing_agent = existing_assistants[agent_name]
33+
self.get_existing_assistants()
34+
if agent_name in self.existing_assistants:
35+
existing_agent = self.existing_assistants[agent_name]
4136
for file_id in existing_agent.file_ids:
42-
existing_file = client.files.retrieve(file_id=file_id)
37+
existing_file = self.client.files.retrieve(file_id=file_id)
4338
existing_files[existing_file.filename] = existing_file
4439

4540

@@ -69,7 +64,7 @@ def create_assistants():
6964
file_path = os.path.join(files_folder, filename)
7065
with open(file_path, 'rb') as file_data:
7166
# Upload each file to OpenAI
72-
file_object = client.files.create(
67+
file_object = self.client.files.create(
7368
file=file_data, purpose='assistants'
7469
)
7570
files.append({"name": filename, "id": file_object.id})
@@ -122,7 +117,7 @@ def create_assistants():
122117
if len(update_params) != 0:
123118
print(f"Updating {agent_name}'s { ','.join(update_params.keys()) }")
124119
update_params['assistant_id'] = existing_agent.id
125-
assistant = client.beta.assistants.update(**update_params)
120+
assistant = self.client.beta.assistants.update(**update_params)
126121
else:
127122
print(f"{agent_name} is up to date")
128123
else:
@@ -141,8 +136,29 @@ def create_assistants():
141136
create_params['file_ids'] = list(map(lambda x: x['id'], files))
142137

143138
# Create the assistant using the uploaded file IDs if files exist
144-
assistant = client.beta.assistants.create(**create_params)
139+
assistant = self.client.beta.assistants.create(**create_params)
145140
print("***********************************************")
146141

142+
def create_assistants(self):
143+
agents_path = os.path.join(
144+
Path(__file__).absolute().parent, self.agents_path
145+
)
146+
147+
# Check if the 'agents' folder is empty or doesn't exist
148+
if (
149+
not os.path.exists(agents_path)
150+
or not os.path.isdir(agents_path)
151+
or not os.listdir(agents_path)
152+
):
153+
raise ValueError(f'The "{self.agents_path}" folder is missing, not a directory, or empty.')
154+
155+
self.get_existing_assistants()
156+
157+
# Iterate over each folder inside the 'agents' folder
158+
for agent_name in os.listdir(agents_path):
159+
self.create_assistant(agent_name)
160+
147161
if __name__ == '__main__':
148-
create_assistants()
162+
client = get_openai_client()
163+
agent_builder = AgentBuilder(client=client)
164+
agent_builder.create_assistants()

agents/tool_maker/assistant_manager.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
from pathlib import Path
33
import os
44
import json
5-
from agents.agent_builder.create import create_assistants
5+
from agents.agent_builder.create import AgentBuilder
66

77
class AssistantManager:
88

99
def __init__(self, client):
10-
create_assistants()
1110
self.client = client
1211
self.assistant = None
12+
self.agent_builder = AgentBuilder(client=self.client)
1313
Path(__file__).absolute().parent
1414
tools_path = os.path.join(
1515
Path(__file__).absolute().parent, "tool_creator_metadata.json"
@@ -19,24 +19,27 @@ def __init__(self, client):
1919

2020
def get_assistant(self):
2121
"""Retrieve or create an assistant for testing this functionality"""
22-
if not self.assistant_package["name"] in [
22+
name = self.assistant_package["creator"]["name"]
23+
self.agent_builder.create_assistant(name)
24+
if not name in [
2325
assistant.name for assistant in self.client.beta.assistants.list()
2426
]:
25-
raise ValueError(f'{self.assistant_package["name"]} needs to be created using create.py in /agents/agent_builder/')
27+
raise ValueError(f'{name} needs to be created using create.py in /agents/agent_builder/')
2628
else:
2729
assistant_dict = {
2830
assistant.name: assistant.id
2931
for assistant in self.client.beta.assistants.list()
3032
}
3133
assistant = self.client.beta.assistants.retrieve(
32-
assistant_id=assistant_dict[self.assistant_package["name"]]
34+
assistant_id=assistant_dict[name]
3335
)
3436
self.assistant = assistant
3537
return assistant
3638

3739
def get_coding_assistant(self):
3840
"""Retrieve or create an assistant for testing this functionality"""
39-
name = "temporary_function_writer"
41+
name = self.assistant_package["writer"]["name"]
42+
self.agent_builder.create_assistant(name)
4043
if not name in [
4144
assistant.name for assistant in self.client.beta.assistants.list()
4245
]:
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
{
2-
"name": "tool_creator"
2+
"creator": {
3+
"name": "tool_creator"
4+
},
5+
"writer": {
6+
"name": "temporary_function_writer"
7+
}
38
}

0 commit comments

Comments
 (0)