You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given an `assistant_id` you can `retrieve` the current field values:
477
477
478
-
```
478
+
```ruby
479
479
client.assistants.retrieve(id: assistant_id)
480
480
```
481
481
482
482
You can get a `list` of all assistants currently available under the organization:
483
483
484
-
```
484
+
```ruby
485
485
client.assistants.list
486
486
```
487
487
488
488
You can modify an existing assistant using the assistant's id (see [API documentation](https://platform.openai.com/docs/api-reference/assistants/modifyAssistant)):
489
489
490
-
```
490
+
```ruby
491
491
response = client.assistants.modify(
492
492
id: assistant_id,
493
493
parameters: {
@@ -502,6 +502,163 @@ You can delete assistants:
502
502
client.assistants.delete(id: assistant_id)
503
503
```
504
504
505
+
### Threads and Messages
506
+
507
+
Once you have created an assistant as described above, you need to prepare a `Thread` of `Messages` for the assistant to work on (see [introduction on Assistants](https://platform.openai.com/docs/assistants/how-it-works)). For example, as an initial setup you could do:
508
+
509
+
```ruby
510
+
# Create thread
511
+
response = client.threads.create # Note: Once you create a thread, there is no way to list it
512
+
# or recover it currently (as of 2023-12-10). So hold onto the `id`
513
+
thread_id = response["id"]
514
+
515
+
# Add initial message from user (see https://platform.openai.com/docs/api-reference/messages/createMessage)
516
+
message_id = client.messages.create(
517
+
thread_id: thread_id,
518
+
parameters: {
519
+
role:"user", # Required for manually created messages
520
+
content:"Can you help me write an API library to interact with the OpenAI API please?"
# To delete the thread (and all associated messages):
534
+
client.threads.delete(id: thread_id)
535
+
536
+
client.messages.retrieve(thread_id: thread_id, id: message_id) # -> Fails after thread is deleted
537
+
```
538
+
539
+
540
+
### Runs
541
+
542
+
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):
543
+
544
+
```ruby
545
+
# Create run (will use instruction/model/tools from Assistant's definition)
The `status` response can include the following strings `queued`, `in_progress`, `requires_action`, `cancelling`, `cancelled`, `failed`, `completed`, or `expired` which you can handle as follows:
# Find the actual response text in the content array of the messages
602
+
new_messages.each { |msg|
603
+
msg['content'].each { |content_item|
604
+
case content_item['type']
605
+
when'text'
606
+
puts content_item.dig('text', 'value')
607
+
# Also handle annotations
608
+
when'image_file'
609
+
# Use File endpoint to retrieve file contents via id
610
+
id = content_item.dig('image_file', 'file_id')
611
+
end
612
+
}
613
+
}
614
+
```
615
+
616
+
At any time you can list all runs which have been performed on a particular thread or are currently running (in descending/newest first order):
617
+
618
+
```ruby
619
+
client.runs.list(thread_id: thread_id)
620
+
```
621
+
622
+
#### Runs involving function tools
623
+
624
+
In case you are allowing the assistant to access `function` tools (they are defined in the same way as functions during chat completion), you might get a status code of `requires_action` when the assistant wants you to evaluate one or more function tools:
625
+
626
+
```ruby
627
+
defget_current_weather(location:, unit:"celsius")
628
+
# Your function code goes here
629
+
if location =~/San Francisco/i
630
+
return unit =="celsius"?"The weather is nice 🌞 at 27°C" : "The weather is nice 🌞 at 80°F"
631
+
else
632
+
return unit =="celsius"?"The weather is icy 🥶 at -5°C" : "The weather is icy 🥶 at 23°F"
0 commit comments