GravityPhone / hatz

0 stars 0 forks source link

Sweep: adjust assistant manager for new streaming capabilities #4

Closed GravityPhone closed 6 months ago

GravityPhone commented 6 months ago

we no longer have to check for runs, there is a new paradigm that I have described in the docs. look at the code example:

from typing_extensions import override from openai import AssistantEventHandler

First, we create a EventHandler class to define

how we want to handle the events in the response stream.

class EventHandler(AssistantEventHandler):
@override def on_text_created(self, text) -> None: print(f"\nassistant > ", end="", flush=True)

@override def on_text_delta(self, delta, snapshot): print(delta.value, end="", flush=True)

def on_tool_call_created(self, tool_call): print(f"\nassistant > {tool_call.type}\n", flush=True)

def on_tool_call_delta(self, delta, snapshot): if delta.type == 'code_interpreter': if delta.code_interpreter.input: print(delta.code_interpreter.input, end="", flush=True) if delta.code_interpreter.outputs: print(f"\n\noutput >", flush=True) for output in delta.code_interpreter.outputs: if output.type == "logs": print(f"\n{output.logs}", flush=True)

Then, we use the create_and_stream SDK helper

with the EventHandler class to create the Run

and stream the response.

with client.beta.threads.runs.create_and_stream( thread_id=thread.id, assistant_id=assistant.id, instructions="Please address the user as Jane Doe. The user has a premium account.", event_handler=EventHandler(), ) as stream: stream.until_done()

Checklist - [X] Modify `assistant_manager.py` ✓ https://github.com/GravityPhone/hatz/commit/3088534235de52d53a069ed4688e583de41b5f41 [Edit](https://github.com/GravityPhone/hatz/edit/sweep/adjust_assistant_manager_for_new_streami/assistant_manager.py) - [X] Running GitHub Actions for `assistant_manager.py` ✓ [Edit](https://github.com/GravityPhone/hatz/edit/sweep/adjust_assistant_manager_for_new_streami/assistant_manager.py)
sweep-ai[bot] commented 6 months ago

🚀 Here's the PR! #5

See Sweep's progress at the progress dashboard!
💎 Sweep Pro: I'm using GPT-4. You have unlimited GPT-4 tickets. (tracking ID: 9b3881f15f)

[!TIP] I can email you next time I complete a pull request if you set up your email here!


Actions (click)


Step 1: 🔎 Searching

I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.

Some code snippets I think are relevant in decreasing order of relevance (click to expand). If some file is missing from here, you can mention the path in the ticket description. https://github.com/GravityPhone/hatz/blob/8026896b8522e3cca2948cf76f52faec0b1f4603/assistant_manager.py#L1-L65

Step 2: ⌨️ Coding

--- 
+++ 
@@ -1,4 +1,6 @@
 import openai
+from openai import AssistantEventHandler
+from typing_extensions import override
 import time

 class AssistantManager:
@@ -27,34 +29,20 @@
             return None

     def run_assistant(self, thread_id, assistant_id, instructions):
-        try:
-            run = self.client.beta.threads.runs.create(
-                thread_id=thread_id,
-                assistant_id=assistant_id,
-                instructions=instructions
-            )
-            return run.id
-        except Exception as e:
-            print(f"Failed to run assistant on thread {thread_id}: {e}")
-            return None
+      try:
+          with self.client.beta.threads.runs.create_and_stream(
+              thread_id=thread_id,
+              assistant_id=assistant_id,
+              instructions=instructions,
+              event_handler=EventHandler(),
+          ) as stream:
+              stream.until_done()
+          return True
+      except Exception as e:
+          print(f"Failed to run assistant on thread {thread_id}: {e}")
+          return False

-    def check_run_status(self, thread_id, run_id, timeout=300):
-        start_time = time.time()
-        while time.time() - start_time < timeout:
-            try:
-                run_status = self.client.beta.threads.runs.retrieve(
-                    thread_id=thread_id,
-                    run_id=run_id
-                )
-                if run_status.status == 'completed':
-                    return True
-                elif run_status.status in ['failed', 'cancelled']:
-                    return False
-                time.sleep(1)
-            except Exception as e:
-                print(f"Failed to check run status: {e}")
-                return False
-        return False
+    

     def retrieve_most_recent_message(self, thread_id):
         try:

Ran GitHub Actions for 3088534235de52d53a069ed4688e583de41b5f41:


Step 3: 🔁 Code Review

I have finished reviewing the code for completeness. I did not find errors for sweep/adjust_assistant_manager_for_new_streami.


🎉 Latest improvements to Sweep:
  • New dashboard launched for real-time tracking of Sweep issues, covering all stages from search to coding.
  • Integration of OpenAI's latest Assistant API for more efficient and reliable code planning and editing, improving speed by 3x.
  • Use the GitHub issues extension for creating Sweep issues directly from your editor.

💡 To recreate the pull request edit the issue title or description. Something wrong? Let us know.

This is an automated message generated by Sweep AI.