diff --git a/.github/ottobot/ottobot.py b/.github/ottobot/ottobot.py index 375a50927a..f05a6cf89c 100644 --- a/.github/ottobot/ottobot.py +++ b/.github/ottobot/ottobot.py @@ -42,46 +42,6 @@ def grep(self, pattern: str, path: str, recursive: bool) -> str: return result.stderr.decode("utf-8") -class EventHandler(AssistantEventHandler): - def __init__(self, otto): - self.otto = otto - super().__init__() - - @override - def on_event(self, event): - # Retrieve events that are denoted with 'requires_action' - # since these will have our tool_calls - if event.event == "thread.run.requires_action": - run_id = event.data.id # Retrieve the run ID from the event data - self.handle_requires_action(event.data, run_id) - - def handle_requires_action(self, data, run_id): - tool_outputs = [] - - for tool in data.required_action.submit_tool_outputs.tool_calls: - otto_func = getattr(self.otto, tool.function.name) - kwargs = json.loads(tool.function.arguments) - print(bold(f"Running tool: {tool.function.name} with arguments: {kwargs}")) - tool_outputs.append( - {"tool_call_id": tool.id, "output": otto_func(**kwargs)} - ) - - # Submit all tool_outputs at the same time - self.submit_tool_outputs(tool_outputs, run_id) - - def submit_tool_outputs(self, tool_outputs, run_id): - # Use the submit_tool_outputs_stream helper - with client.beta.threads.runs.submit_tool_outputs_stream( - thread_id=self.current_run.thread_id, - run_id=self.current_run.id, - tool_outputs=tool_outputs, - event_handler=EventHandler(self.otto), - ) as stream: - for text in stream.text_deltas: - print(text, end="", flush=True) - print() - - if __name__ == "__main__": load_dotenv() otto = Ottobot(live_mode="--live" in sys.argv) @@ -98,133 +58,152 @@ def submit_tool_outputs(self, tool_outputs, run_id): os.chdir(repo_root) # Move to the repo root so everything runs from there - assistant = client.beta.assistants.create( - instructions=instructions, - model="gpt-4o", # try mini? - tools=[ - { - "type": "function", - "function": { - "name": "ls", - "description": "List files in a directory", - "parameters": { - "type": "object", - "properties": { - "path": { - "type": "string", - "description": "The path to the directory to list", - } - }, - "required": ["path"], + tools = [ + { + "type": "function", + "function": { + "name": "ls", + "description": "List files in a directory", + "parameters": { + "type": "object", + "properties": { + "path": { + "type": "string", + "description": "The path to the directory to list", + } }, + "required": ["path"], }, }, - { - "type": "function", - "function": { - "name": "tree", - "description": "List files in a directory in a tree format", - "parameters": { - "type": "object", - "properties": { - "path": { - "type": "string", - "description": "The path to the directory to list", - } - }, - "required": ["path"], + }, + { + "type": "function", + "function": { + "name": "tree", + "description": "List files in a directory in a tree format", + "parameters": { + "type": "object", + "properties": { + "path": { + "type": "string", + "description": "The path to the directory to list", + } }, + "required": ["path"], }, }, - { - "type": "function", - "function": { - "name": "cat", - "description": "Print the contents of a file", - "parameters": { - "type": "object", - "properties": { - "file": { - "type": "string", - "description": "The path to the file to print", - } - }, - "required": ["file"], + }, + { + "type": "function", + "function": { + "name": "cat", + "description": "Print the contents of a file", + "parameters": { + "type": "object", + "properties": { + "file": { + "type": "string", + "description": "The path to the file to print", + } }, + "required": ["file"], }, }, - { - "type": "function", - "function": { - "name": "grep", - "description": "Search for a pattern in a file", - "parameters": { - "type": "object", - "properties": { - "pattern": { - "type": "string", - "description": "The pattern to search for", - }, - "path": { - "type": "string", - "description": "The path to search in", - }, - "recursive": { - "type": "boolean", - "description": "Whether to search recursively", - }, + }, + { + "type": "function", + "function": { + "name": "grep", + "description": "Search for a pattern in a file", + "parameters": { + "type": "object", + "properties": { + "pattern": { + "type": "string", + "description": "The pattern to search for", + }, + "path": { + "type": "string", + "description": "The path to search in", + }, + "recursive": { + "type": "boolean", + "description": "Whether to search recursively", }, - "required": ["pattern", "path", "recursive"], }, + "required": ["pattern", "path", "recursive"], }, }, - # { - # "type": "function", - # "function": { - # "name": "find", - # "description": "Search for a file in a directory", - # "parameters": { - # "type": "object", - # "properties": { - # "name": { - # "type": "string", - # "description": "The name of the file to search for" - # }, - # "path": { - # "type": "string", - # "description": "The path to the directory to search in" - # } - # }, - # "required": ["name", "path"] - # } - # } - # }, - # { - # "type": "function", - # "function": { - # "name": "patch", - # "description": "Generate a git patch for a file", - # "parameters": { - # "type": "object", - # "properties": { - # "file": { - # "type": "string", - # " - ], - ) + }, + # { + # "type": "function", + # "function": { + # "name": "find", + # "description": "Search for a file in a directory", + # "parameters": { + # "type": "object", + # "properties": { + # "name": { + # "type": "string", + # "description": "The name of the file to search for" + # }, + # "path": { + # "type": "string", + # "description": "The path to the directory to search in" + # } + # }, + # "required": ["name", "path"] + # } + # } + # }, + # { + # "type": "function", + # "function": { + # "name": "patch", + # "description": "Generate a git patch for a file", + # "parameters": { + # "type": "object", + # "properties": { + # "file": { + # "type": "string", + # " + ] # Give it the initial information to work from, so it doesn't have to ask for it files = otto.tree(".") todos = otto.grep("TODO", ".", recursive=True) - thread = client.beta.threads.create() - message = client.beta.threads.messages.create( - thread_id=thread.id, - role="user", - content=f"Current files in the repo (from `tree`):\n{files}\n\nCurrent TODOs in the repo (from `grep`):\n{todos}", + message = f"""{instructions} + +--- + +Current files in the repo (from `tree`): +{files} + +Current TODOs in the repo (from `grep`): +{todos} +""" + + stream = client.chat.completions.create( + model="o1", + messages=[{"role": "user", "content": message}], + tools=tools, + stream=True ) - with client.beta.threads.runs.stream( - thread_id=thread.id, assistant_id=assistant.id, event_handler=EventHandler(otto) - ) as stream: - stream.until_done() + for chunk in stream: + print(chunk) + + + + # thread = client.beta.threads.create() + # message = client.beta.threads.messages.create( + # thread_id=thread.id, + # role="user", + # content=f"Current files in the repo (from `tree`):\n{files}\n\nCurrent TODOs in the repo (from `grep`):\n{todos}", + # ) + + # with client.beta.threads.runs.stream( + # thread_id=thread.id, assistant_id=assistant.id, event_handler=EventHandler(otto) + # ) as stream: + # stream.until_done()