-
Notifications
You must be signed in to change notification settings - Fork 675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chat: feature to cancel active runs #1404
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ def __init__(self, mpstate, status_cb=None, wait_for_command_ack_fn=None): | |
self.client = None | ||
self.assistant = None | ||
self.assistant_thread = None | ||
self.latest_run = None | ||
|
||
# check connection to OpenAI assistant and connect if necessary | ||
# returns True if connection is good, False if not | ||
|
@@ -99,6 +100,25 @@ def set_api_key(self, api_key_str): | |
self.assistant = None | ||
self.assistant_thread = None | ||
|
||
# cancel the active run | ||
def cancel_run(self): | ||
# check the active thread and run | ||
if (self.assistant_thread and self.latest_run is not None): | ||
run_status = self.latest_run.status | ||
if (run_status != "completed" and run_status != "cancelled" and | ||
run_status != "cancelling"): | ||
|
||
# cancel the active run | ||
self.client.beta.threads.runs.cancel( | ||
thread_id=self.assistant_thread.id, | ||
run_id=self.run.id | ||
) | ||
else: | ||
if (self.latest_run.status == "completed"): | ||
print("Chat is completed, cannot be cancelled") | ||
elif (self.latest_run.status == "cancelled"): | ||
print("Chat is cancelled") | ||
|
||
# send text to assistant | ||
def send_to_assistant(self, text): | ||
# get lock | ||
|
@@ -132,7 +152,7 @@ def send_to_assistant(self, text): | |
time.sleep(0.1) | ||
|
||
# retrieve the run | ||
latest_run = self.client.beta.threads.runs.retrieve( | ||
self.latest_run = self.client.beta.threads.runs.retrieve( | ||
thread_id=self.assistant_thread.id, | ||
run_id=self.run.id | ||
) | ||
|
@@ -141,22 +161,22 @@ def send_to_assistant(self, text): | |
failure_message = None | ||
|
||
# check run status | ||
if latest_run.status in ["queued", "in_progress", "cancelling"]: | ||
if self.latest_run.status in ["queued", "in_progress", "cancelling"]: | ||
run_done = False | ||
elif latest_run.status in ["cancelled", "completed", "expired"]: | ||
elif self.latest_run.status in ["cancelled", "completed", "expired"]: | ||
run_done = True | ||
elif latest_run.status in ["failed"]: | ||
failure_message = latest_run.last_error.message | ||
elif self.latest_run.status in ["failed"]: | ||
failure_message = self.latest_run.last_error.message | ||
run_done = True | ||
elif latest_run.status in ["requires_action"]: | ||
self.handle_function_call(latest_run) | ||
elif self.latest_run.status in ["requires_action"]: | ||
self.handle_function_call(self.latest_run) | ||
run_done = False | ||
else: | ||
print("chat: unrecognised run status" + latest_run.status) | ||
print("chat: unrecognised run status" + self.latest_run.status) | ||
run_done = True | ||
|
||
# send status to status callback | ||
status_message = latest_run.status | ||
status_message = self.latest_run.status | ||
if failure_message is not None: | ||
status_message = status_message + ": " + failure_message | ||
self.send_status(status_message) | ||
|
@@ -165,7 +185,10 @@ def send_to_assistant(self, text): | |
reply_messages = self.client.beta.threads.messages.list(self.assistant_thread.id, | ||
order="asc", | ||
after=input_message.id) | ||
if reply_messages is None: | ||
|
||
if (self.latest_run.status == "cancelled"): | ||
return "cancelled successfully" | ||
elif reply_messages is None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a blocker but the "elif" could be just an "if" because of the return one line higher |
||
return "chat: failed to retrieve messages" | ||
|
||
# concatenate all messages into a single reply skipping the first which is our question | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.