Skip to content

Commit 6640dd0

Browse files
authored
Merge pull request #464 from alexrudall/assistant-streaming
Add docs for Assistant Run streaming
2 parents 2b8431c + 981124c commit 6640dd0

30 files changed

+1464
-157
lines changed

README.md

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -749,17 +749,36 @@ client.messages.retrieve(thread_id: thread_id, id: message_id) # -> Fails after
749749

750750
### Runs
751751

752-
To submit a thread to be evaluated with the model of an assistant, create a `Run` as follows (Note: This is one place where OpenAI will take your money):
752+
To submit a thread to be evaluated with the model of an assistant, create a `Run` as follows:
753753

754754
```ruby
755755
# Create run (will use instruction/model/tools from Assistant's definition)
756756
response = client.runs.create(thread_id: thread_id,
757757
parameters: {
758-
assistant_id: assistant_id
758+
assistant_id: assistant_id,
759+
max_prompt_tokens: 256,
760+
max_completion_tokens: 16
759761
})
760762
run_id = response['id']
763+
```
764+
765+
You can stream the message chunks as they come through:
766+
767+
```ruby
768+
client.runs.create(thread_id: thread_id,
769+
parameters: {
770+
assistant_id: assistant_id,
771+
max_prompt_tokens: 256,
772+
max_completion_tokens: 16,
773+
stream: proc do |chunk, _bytesize|
774+
print chunk.dig("delta", "content", 0, "text", "value") if chunk["object"] == "thread.message.delta"
775+
end
776+
})
777+
```
761778

762-
# Retrieve/poll Run to observe status
779+
To get the status of a Run:
780+
781+
```
763782
response = client.runs.retrieve(id: run_id, thread_id: thread_id)
764783
status = response['status']
765784
```
@@ -768,23 +787,22 @@ The `status` response can include the following strings `queued`, `in_progress`,
768787

769788
```ruby
770789
while true do
771-
772790
response = client.runs.retrieve(id: run_id, thread_id: thread_id)
773791
status = response['status']
774792

775793
case status
776794
when 'queued', 'in_progress', 'cancelling'
777-
puts 'Sleeping'
778-
sleep 1 # Wait one second and poll again
795+
puts 'Sleeping'
796+
sleep 1 # Wait one second and poll again
779797
when 'completed'
780-
break # Exit loop and report result to user
798+
break # Exit loop and report result to user
781799
when 'requires_action'
782-
# Handle tool calls (see below)
800+
# Handle tool calls (see below)
783801
when 'cancelled', 'failed', 'expired'
784-
puts response['last_error'].inspect
785-
break # or `exit`
802+
puts response['last_error'].inspect
803+
break # or `exit`
786804
else
787-
puts "Unknown status response: #{status}"
805+
puts "Unknown status response: #{status}"
788806
end
789807
end
790808
```

spec/fixtures/cassettes/runs_cancel.yml

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/cassettes/runs_cancel_assistant_setup.yml

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/cassettes/runs_cancel_run_setup.yml

Lines changed: 92 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/cassettes/runs_cancel_thread_setup.yml

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)