@@ -42,46 +42,6 @@ def grep(self, pattern: str, path: str, recursive: bool) -> str:
42
42
return result .stderr .decode ("utf-8" )
43
43
44
44
45
- class EventHandler (AssistantEventHandler ):
46
- def __init__ (self , otto ):
47
- self .otto = otto
48
- super ().__init__ ()
49
-
50
- @override
51
- def on_event (self , event ):
52
- # Retrieve events that are denoted with 'requires_action'
53
- # since these will have our tool_calls
54
- if event .event == "thread.run.requires_action" :
55
- run_id = event .data .id # Retrieve the run ID from the event data
56
- self .handle_requires_action (event .data , run_id )
57
-
58
- def handle_requires_action (self , data , run_id ):
59
- tool_outputs = []
60
-
61
- for tool in data .required_action .submit_tool_outputs .tool_calls :
62
- otto_func = getattr (self .otto , tool .function .name )
63
- kwargs = json .loads (tool .function .arguments )
64
- print (bold (f"Running tool: { tool .function .name } with arguments: { kwargs } " ))
65
- tool_outputs .append (
66
- {"tool_call_id" : tool .id , "output" : otto_func (** kwargs )}
67
- )
68
-
69
- # Submit all tool_outputs at the same time
70
- self .submit_tool_outputs (tool_outputs , run_id )
71
-
72
- def submit_tool_outputs (self , tool_outputs , run_id ):
73
- # Use the submit_tool_outputs_stream helper
74
- with client .beta .threads .runs .submit_tool_outputs_stream (
75
- thread_id = self .current_run .thread_id ,
76
- run_id = self .current_run .id ,
77
- tool_outputs = tool_outputs ,
78
- event_handler = EventHandler (self .otto ),
79
- ) as stream :
80
- for text in stream .text_deltas :
81
- print (text , end = "" , flush = True )
82
- print ()
83
-
84
-
85
45
if __name__ == "__main__" :
86
46
load_dotenv ()
87
47
otto = Ottobot (live_mode = "--live" in sys .argv )
@@ -98,133 +58,152 @@ def submit_tool_outputs(self, tool_outputs, run_id):
98
58
99
59
os .chdir (repo_root ) # Move to the repo root so everything runs from there
100
60
101
- assistant = client .beta .assistants .create (
102
- instructions = instructions ,
103
- model = "gpt-4o" , # try mini?
104
- tools = [
105
- {
106
- "type" : "function" ,
107
- "function" : {
108
- "name" : "ls" ,
109
- "description" : "List files in a directory" ,
110
- "parameters" : {
111
- "type" : "object" ,
112
- "properties" : {
113
- "path" : {
114
- "type" : "string" ,
115
- "description" : "The path to the directory to list" ,
116
- }
117
- },
118
- "required" : ["path" ],
61
+ tools = [
62
+ {
63
+ "type" : "function" ,
64
+ "function" : {
65
+ "name" : "ls" ,
66
+ "description" : "List files in a directory" ,
67
+ "parameters" : {
68
+ "type" : "object" ,
69
+ "properties" : {
70
+ "path" : {
71
+ "type" : "string" ,
72
+ "description" : "The path to the directory to list" ,
73
+ }
119
74
},
75
+ "required" : ["path" ],
120
76
},
121
77
},
122
- {
123
- "type" : "function" ,
124
- "function" : {
125
- "name" : "tree" ,
126
- "description" : "List files in a directory in a tree format" ,
127
- "parameters" : {
128
- "type" : "object" ,
129
- "properties" : {
130
- "path" : {
131
- "type" : "string" ,
132
- "description" : "The path to the directory to list" ,
133
- }
134
- },
135
- "required" : ["path" ],
78
+ },
79
+ {
80
+ "type" : "function" ,
81
+ "function" : {
82
+ "name" : "tree" ,
83
+ "description" : "List files in a directory in a tree format" ,
84
+ "parameters" : {
85
+ "type" : "object" ,
86
+ "properties" : {
87
+ "path" : {
88
+ "type" : "string" ,
89
+ "description" : "The path to the directory to list" ,
90
+ }
136
91
},
92
+ "required" : ["path" ],
137
93
},
138
94
},
139
- {
140
- "type" : "function" ,
141
- "function" : {
142
- "name" : "cat" ,
143
- "description" : "Print the contents of a file" ,
144
- "parameters" : {
145
- "type" : "object" ,
146
- "properties" : {
147
- "file" : {
148
- "type" : "string" ,
149
- "description" : "The path to the file to print" ,
150
- }
151
- },
152
- "required" : ["file" ],
95
+ },
96
+ {
97
+ "type" : "function" ,
98
+ "function" : {
99
+ "name" : "cat" ,
100
+ "description" : "Print the contents of a file" ,
101
+ "parameters" : {
102
+ "type" : "object" ,
103
+ "properties" : {
104
+ "file" : {
105
+ "type" : "string" ,
106
+ "description" : "The path to the file to print" ,
107
+ }
153
108
},
109
+ "required" : ["file" ],
154
110
},
155
111
},
156
- {
157
- "type" : "function" ,
158
- "function " : {
159
- "name " : "grep" ,
160
- "description " : "Search for a pattern in a file " ,
161
- "parameters" : {
162
- "type " : "object" ,
163
- "properties " : {
164
- "pattern " : {
165
- "type " : "string" ,
166
- "description " : "The pattern to search for " ,
167
- } ,
168
- "path" : {
169
- "type " : "string" ,
170
- "description " : "The path to search in " ,
171
- } ,
172
- "recursive" : {
173
- "type " : "boolean" ,
174
- "description " : "Whether to search recursively " ,
175
- } ,
112
+ },
113
+ {
114
+ "type " : "function" ,
115
+ "function " : {
116
+ "name " : "grep " ,
117
+ "description" : "Search for a pattern in a file" ,
118
+ "parameters " : {
119
+ "type " : "object" ,
120
+ "properties " : {
121
+ "pattern " : {
122
+ "type " : "string " ,
123
+ "description" : "The pattern to search for" ,
124
+ },
125
+ "path " : {
126
+ "type " : "string " ,
127
+ "description" : "The path to search in" ,
128
+ },
129
+ "recursive " : {
130
+ "type " : "boolean " ,
131
+ "description" : "Whether to search recursively" ,
176
132
},
177
- "required" : ["pattern" , "path" , "recursive" ],
178
133
},
134
+ "required" : ["pattern" , "path" , "recursive" ],
179
135
},
180
136
},
181
- # {
182
- # "type": "function",
183
- # "function ": {
184
- # "name ": "find",
185
- # "description ": "Search for a file in a directory ",
186
- # "parameters ": {
187
- # "type ": "object",
188
- # "properties ": {
189
- # "name ": {
190
- # "type ": "string",
191
- # "description ": "The name of the file to search for"
192
- # },
193
- # "path": {
194
- # "type ": "string",
195
- # "description ": "The path to the directory to search in"
196
- # }
197
- # },
198
- # "required": ["name", "path"]
199
- # }
200
- # }
201
- # },
202
- # {
203
- # "type": "function",
204
- # "function ": {
205
- # "name ": "patch",
206
- # "description ": "Generate a git patch for a file ",
207
- # "parameters ": {
208
- # "type ": "object",
209
- # "properties ": {
210
- # "file ": {
211
- # "type ": "string",
212
- # "
213
- ],
214
- )
137
+ },
138
+ # {
139
+ # "type ": "function",
140
+ # "function ": {
141
+ # "name ": "find ",
142
+ # "description ": "Search for a file in a directory",
143
+ # "parameters ": {
144
+ # "type ": "object",
145
+ # "properties ": {
146
+ # "name ": {
147
+ # "type ": "string",
148
+ # "description": "The name of the file to search for"
149
+ # },
150
+ # "path ": {
151
+ # "type ": "string",
152
+ # "description": "The path to the directory to search in"
153
+ # }
154
+ # },
155
+ # "required": ["name", "path"]
156
+ # }
157
+ # }
158
+ # },
159
+ # {
160
+ # "type ": "function",
161
+ # "function ": {
162
+ # "name ": "patch",
163
+ # "description ": "Generate a git patch for a file",
164
+ # "parameters ": {
165
+ # "type ": "object",
166
+ # "properties ": {
167
+ # "file ": {
168
+ # "type": "string",
169
+ # "
170
+ ]
215
171
216
172
# Give it the initial information to work from, so it doesn't have to ask for it
217
173
files = otto .tree ("." )
218
174
todos = otto .grep ("TODO" , "." , recursive = True )
219
175
220
- thread = client .beta .threads .create ()
221
- message = client .beta .threads .messages .create (
222
- thread_id = thread .id ,
223
- role = "user" ,
224
- content = f"Current files in the repo (from `tree`):\n { files } \n \n Current TODOs in the repo (from `grep`):\n { todos } " ,
176
+ message = f"""{ instructions }
177
+
178
+ ---
179
+
180
+ Current files in the repo (from `tree`):
181
+ { files }
182
+
183
+ Current TODOs in the repo (from `grep`):
184
+ { todos }
185
+ """
186
+
187
+ stream = client .chat .completions .create (
188
+ model = "o1" ,
189
+ messages = [{"role" : "user" , "content" : message }],
190
+ tools = tools ,
191
+ stream = True
225
192
)
226
193
227
- with client .beta .threads .runs .stream (
228
- thread_id = thread .id , assistant_id = assistant .id , event_handler = EventHandler (otto )
229
- ) as stream :
230
- stream .until_done ()
194
+ for chunk in stream :
195
+ print (chunk )
196
+
197
+
198
+
199
+ # thread = client.beta.threads.create()
200
+ # message = client.beta.threads.messages.create(
201
+ # thread_id=thread.id,
202
+ # role="user",
203
+ # content=f"Current files in the repo (from `tree`):\n{files}\n\nCurrent TODOs in the repo (from `grep`):\n{todos}",
204
+ # )
205
+
206
+ # with client.beta.threads.runs.stream(
207
+ # thread_id=thread.id, assistant_id=assistant.id, event_handler=EventHandler(otto)
208
+ # ) as stream:
209
+ # stream.until_done()
0 commit comments