3
3
from pathlib import Path
4
4
from shared .openai_config import get_openai_client
5
5
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 ):
33
19
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
+
35
29
print (agent_folder )
36
30
existing_files = {}
37
31
requested_files = []
38
32
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 ]
41
36
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 )
43
38
existing_files [existing_file .filename ] = existing_file
44
39
45
40
@@ -69,7 +64,7 @@ def create_assistants():
69
64
file_path = os .path .join (files_folder , filename )
70
65
with open (file_path , 'rb' ) as file_data :
71
66
# Upload each file to OpenAI
72
- file_object = client .files .create (
67
+ file_object = self . client .files .create (
73
68
file = file_data , purpose = 'assistants'
74
69
)
75
70
files .append ({"name" : filename , "id" : file_object .id })
@@ -122,7 +117,7 @@ def create_assistants():
122
117
if len (update_params ) != 0 :
123
118
print (f"Updating { agent_name } 's { ',' .join (update_params .keys ()) } " )
124
119
update_params ['assistant_id' ] = existing_agent .id
125
- assistant = client .beta .assistants .update (** update_params )
120
+ assistant = self . client .beta .assistants .update (** update_params )
126
121
else :
127
122
print (f"{ agent_name } is up to date" )
128
123
else :
@@ -141,8 +136,29 @@ def create_assistants():
141
136
create_params ['file_ids' ] = list (map (lambda x : x ['id' ], files ))
142
137
143
138
# 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 )
145
140
print ("***********************************************" )
146
141
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
+
147
161
if __name__ == '__main__' :
148
- create_assistants ()
162
+ client = get_openai_client ()
163
+ agent_builder = AgentBuilder (client = client )
164
+ agent_builder .create_assistants ()
0 commit comments