A Ruby interface to the OpenAI API
Follow
For updates and announcements, follow @sferik on X.
Installation
Install the gem and add to the application's Gemfile:
bundle add openai
Or, if Bundler is not being used to manage dependencies:
gem install openai
Usage
First, obtain an API key from https://platform.openai.com/api-keys.
require "openai"
# Initialize an OpenAI API client with your API secret key
openai_client = OpenAI::Client.new(bearer_token: "sk-thats1smallStep4man1giantLeap4mankind")
# Retrieve a model instance
openai_client.get("models/gpt-4")
# {"id"=>"gpt-4", "object"=>"model", "created"=>1687882411, "owned_by"=>"openai"}
Features
- OAuth 2.0 Bearer Token
- Thread safety
- HTTP redirect following
- HTTP proxy support
- HTTP logging
- HTTP timeout configuration
- HTTP error handling
- Rate limit handling
- Parsing JSON into custom response objects (e.g. OpenStruct)
- Configurable base URLs for accessing different APIs/versions
Sponsorship
By contributing to the project, you help:
- Maintain the library: Keeping it up-to-date and secure.
- Add new features: Enhancements that make your life easier.
- Provide support: Faster responses to issues and feature requests.
⭐️ Bonus: Sponsors will get priority support and influence over the project roadmap. We will also list your name or your company's logo on our GitHub page.
Building and maintaining an open-source project like this takes a considerable amount of time and effort. Your sponsorship can help sustain this project. Even a small monthly donation makes a huge difference!
Click here to sponsor this project.
Development
-
Checkout and repo:
git checkout git@github.com:sferik/openai-ruby.git
-
Enter the repo’s directory:
cd openai-ruby
-
Install dependencies via Bundler:
bin/setup
-
Run the default Rake task to ensure all tests pass:
bundle exec rake
-
Create a new branch for your feature or bug fix:
git checkout -b my-new-branch
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/sferik/openai-ruby.
Pull requests will only be accepted if they meet all the following criteria:
-
Code must conform to Standard Ruby. This can be verified with:
bundle exec rake standard
-
Code must conform to the RuboCop rules. This can be verified with:
bundle exec rake rubocop
-
100% C0 code coverage. This can be verified with:
bundle exec rake test
-
100% mutation coverage. This can be verified with:
bundle exec rake mutant
-
RBS type signatures (in
sig/openai.rbs
). This can be verified with:bundle exec rake steep
License
The gem is available as open source under the terms of the MIT License.