Ruby OpenAI
Use the OpenAI API with Ruby! 🤖❤️
Stream GPT-5 chats with the Responses API, initiate Realtime WebRTC conversations, and much more...
Sponsors
|
InferToGo - The inference addon for your PaaS application. |
SerpApi - Search API - Enhance your LLM's knowledge with data from search engines like Google and Bing using our simple API. |
🎮 Ruby AI Builders Discord | 🐦 X | 🧠 Anthropic Gem | 🚂 Midjourney Gem | ♥️ Thanks to all sponsors!
Contents
- Ruby OpenAI
- Contents
- Project Policies
- Installation
- How to use
- Quickstart
- With Config
- Counting Tokens
- Models
- Chat
- Responses API
- Functions
- Completions
- Embeddings
- Batches
- Files
- Finetunes
- Vector Stores
- Vector Store Files
- Vector Store File Batches
- Conversations
- Assistants
- Threads and Messages
- Runs
- Image Generation
- Image Edit
- Image Variations
- Moderations
- Whisper
- Real-Time
- Usage
- Errors
- Development
- Release
- Contributing
- License
- Code of Conduct
Project Policies
Installation
Bundler
Add this line to your application's Gemfile:
gem "ruby-openai"
And then execute:
bundle install
Gem install
Or install with:
gem install ruby-openai
and require with:
require "openai"
How to use
- Get your API key from https://platform.openai.com/account/api-keys
- If you belong to multiple organizations, you can get your Organization ID from https://platform.openai.com/account/org-settings
Quickstart
For a quick test you can pass your token directly to a new client:
client = OpenAI::Client.new(
access_token: "access_token_goes_here",
log_errors: true # Highly recommended in development, so you can see what errors OpenAI is returning. Not recommended in production because it could leak private data to your logs.
)
With Config
For a more robust setup, you can configure the gem with your API keys, for example in an openai.rb initializer file. Never hardcode secrets into your codebase - instead use something like dotenv to pass the keys safely into your environments.
OpenAI.configure do |config|
config.access_token = ENV.fetch("OPENAI_ACCESS_TOKEN")
config.admin_token = ENV.fetch("OPENAI_ADMIN_TOKEN") # Optional, used for admin endpoints, created here: https://platform.openai.com/settings/organization/admin-keys
config.organization_id = ENV.fetch("OPENAI_ORGANIZATION_ID") # Optional
config.log_errors = true # Highly recommended in development, so you can see what errors OpenAI is returning. Not recommended in production because it could leak private data to your logs.
end
Then you can create a client like this:
client = OpenAI::Client.new
You can still override the config defaults when making new clients; any options not included will fall back to any global config set with OpenAI.configure. e.g. in this example the organization_id, request_timeout, etc. will fallback to any set globally using OpenAI.configure, with only the access_token and admin_token overridden:
client = OpenAI::Client.new(access_token: "access_token_goes_here", admin_token: "admin_token_goes_here")
Custom timeout or base URI
- The default timeout for any request using this library is 120 seconds. You can change that by passing a number of seconds to the
request_timeoutwhen initializing the client. - You can also change the base URI used for all requests, eg. to use observability tools like Helicone or Velvet
- You can also add arbitrary other headers e.g. for openai-caching-proxy-worker, eg.:
client = OpenAI::Client.new(
access_token: "access_token_goes_here",
uri_base: "https://oai.hconeai.com/",
request_timeout: 240,
extra_headers: {
"X-Proxy-TTL" => "43200", # For https://github.com/6/openai-caching-proxy-worker#specifying-a-cache-ttl
"X-Proxy-Refresh": "true", # For https://github.com/6/openai-caching-proxy-worker#refreshing-the-cache
"Helicone-Auth": "Bearer HELICONE_API_KEY", # For https://docs.helicone.ai/getting-started/integration-method/openai-proxy
"helicone-stream-force-format" => "true", # Use this with Helicone otherwise streaming drops chunks # https://github.com/alexrudall/ruby-openai/issues/251
}
)
or when configuring the gem:
OpenAI.configure do |config|
config.access_token = ENV.fetch("OPENAI_ACCESS_TOKEN")
config.admin_token = ENV.fetch("OPENAI_ADMIN_TOKEN") # Optional, used for admin endpoints, created here: https://platform.openai.com/settings/organization/admin-keys
config.organization_id = ENV.fetch("OPENAI_ORGANIZATION_ID") # Optional
config.log_errors = true # Optional
config.uri_base = "https://oai.hconeai.com/" # Optional
config.request_timeout = 240 # Optional
config.extra_headers = {
"X-Proxy-TTL" => "43200", # For https://github.com/6/openai-caching-proxy-worker#specifying-a-cache-ttl
"X-Proxy-Refresh": "true", # For https://github.com/6/openai-caching-proxy-worker#refreshing-the-cache
"Helicone-Auth": "Bearer HELICONE_API_KEY" # For https://docs.helicone.a