Skip to content

Instantly share code, notes, and snippets.

@navjeetc
Last active December 16, 2023 12:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save navjeetc/d0ec3ec7b7660baedf567e84d5f3830e to your computer and use it in GitHub Desktop.
Save navjeetc/d0ec3ec7b7660baedf567e84d5f3830e to your computer and use it in GitHub Desktop.
OpenAI assistants API usage
require 'openai'
client = OpenAI::Client.new(access_token: ENV['openai_key'])
assistants = OpenAI::Assistants.new(client: client)
# assistant = assistants.create(
# parameters: {
# name: 'Math tutor',
# model: 'gpt-4',
# instructions: 'You are a personal math tutor. Write and run code to answer math questions.'
# }
# )
# you can also use the assistant created on previous line if uncommented
assistant = assistants.list['data'].first
threads = OpenAI::Threads.new(client: client)
thread = threads.create
# puts thread
messages = OpenAI::Messages.new(client: client)
message = messages.create(
thread_id: thread['id'],
parameters: {
role: 'user',
content: 'I need to solve the equation `3x + 11 = 14`. Can you help me?'
}
)
# puts message
runs = OpenAI::Runs.new(client: client)
run = runs.create(
thread_id: thread['id'],
parameters: {
assistant_id: assistant['id'],
instructions: 'Please address the user as Jane Doe. The user has a premium account.'
}
)
# puts run
begin
run_status = runs.retrieve(
thread_id: thread['id'],
id: run['id']
)
end while(run_status['status'] != 'completed')
messages = messages.list(
thread_id: thread['id']
)
# messages structure is:
# response = { 'object' => 'list',
# 'data' => [{
# 'id' => 'msg_j3opx6UtlZKLhFn0OwETJx5e',
# 'object' => 'thread.message',
# 'created_at' => 1_702_249_590,
# 'thread_id' => 'thread_FYe3CyQCsc0qLIMxgHZM73sf',
# 'role' => 'assistant',
# 'content' => [{
# 'type' => 'text',
# 'text' => {
# 'value' => "Of course, Jane Doe. I'd be glad to assist you with that.\n\nThe equation `3x + 11 = 14` can be simplified by subtracting 11 from each side to get `3x = 3`. The last step is to divide each side by 3 to isolate x, so `x = 1`.", 'annotations' => []
# }
# }],
# 'file_ids' => [],
# 'assistant_id' => 'asst_5VMFDgZdErcorXxN7ZWSmeMZ',
# 'run_id' => 'run_RvPDrHL8k3KT7XuUhJhSf6IV',
# 'metadata' => {}
# },
# {
# 'id' => 'msg_AEIf4NwShhxQAKeOx8xMltqM',
# 'object' => 'thread.message',
# 'created_at' => 1_702_249_589,
# 'thread_id' => 'thread_FYe3CyQCsc0qLIMxgHZM73sf',
# 'role' => 'user',
# 'content' => [{
# 'type' => 'text',
# 'text' => {
# 'value' => 'I need to solve the equation `3x + 11 = 14`. Can you help me?',
# 'annotations' => []
# }
# }],
# 'file_ids' => [],
# 'assistant_id' => nil,
# 'run_id' => nil,
# 'metadata' => {}
# }],
# 'first_id' => 'msg_j3opx6UtlZKLhFn0OwETJx5e',
# 'last_id' => 'msg_AEIf4NwShhxQAKeOx8xMltqM',
# 'has_more' => false }
# print content text
puts messages['data'][0]['content'][0]['text']['value']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment