I’m a longtime operations person who picked up coding to better back my developers. Everyone told me Python was the only path for AI, but I saw real potential in Ruby on Rails. I used Rails 8.0.2 and wanted to weave in an integration with OpenAI.
Picking OpenAI was simply a requirements match of needing chat completions, summarization, some research, and potentially diagramming and image generation.
I got things working after literally 10 minutes of setting up the environment. It runs smoothly, and deploying on Heroku is a breeze.
Here is the bare essentials of how I did it and why I think this route is worth considering.
Quick Rails 8.0.2 Example
Add the ruby-openai Gem to your Gemfile which is a fantastic gem that wraps all the goodness of the OpenAI API without you having to deal with coding your own, future, breaking changes.
I know I may take some heat for using gems over building out everything. The whole reason I love Rails is that I don’t have to.
Configure your Gemfile with the ruby-openai gem info
# OpenAI API integration
gem "ruby-openai"
Then run bundle to get things installed and ready
bundle install
Set Your Environment
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
You can locally set your OPENAI_ACCESS_
TOKEN and other optional OpenAI specific configs using the encrypted credentials. Just make sure to exclude that in your .gitignore file to keep it from being included in your repo.
Use It in a Controller
response = client.chat(
parameters: {
model: "gpt-4o", # Required to define which model to use
messages: [{ role: "user", content: "Hello!"}], # Required
temperature: 0.7,
}
)
Check your response
puts response.dig("choices", 0, "message", "content")
# => "Hello! How may I assist you today?"
Now you can just build it into your form processes and other controllers for data work you want to do with summarization, etc.
There is much more to it than that, but the point is showing how quick it was to AI-ify my Rails app.
Deploy to Heroku
I’m a huge fan of Heroku because it is just a few nifty commands away from being in production and secured.
heroku create
git push heroku main
heroku config:set OPENAI_API_KEY=your-key-here
That’s it. Now your Rails app has direct access to OpenAI with tons of AI features now easily accessible…and no Python required!
There are a ton of code samples you can borrow and learn from on the Github for the ruby-openai project. It’s also got links to the Anthropic gem there as well. Lots of options and a Discord link to connect with other Rails fans like us!
Pros with using Rails (I may be biased)
- Rapid development: Rails scaffolds everything so you can prototype in record time. Rails 8 makes it particularly easy now with Stimulus and Turbo for UI/UX elements. I hate trying to get javascript to work.
- Clear conventions: The Rails structure keeps your project neat and predictable. Especially helpful to me as I do not have a “true” programming background.
- Community backup: Gems, forums, and plenty of Rails veterans ready to help. I am betting we will see much more soon.
Cons
- Fewer AI libraries: Python has a rich AI ecosystem, which means you might build some pieces yourself.
- Instrumentation challenges: One downside I find is that a lot of the observability tools are biased towards Javascript, Python, Go, and some more common system programming languages. I found a few challenges getting clean, efficient instrumentation for perf testing and tracing.
- Limited docs for AI in Rails: The official AI guides often assume Python usage. This is likely because Rails is not often the place people may look for a first-party DSL to build an AI-centric application.
It’s been a blast being back to more coding in Rails. And before you tell me that “Rails is not built to be able to scale” I would suggest you check where you are checking your code into 😉
If you’re into Ruby, don’t let any lack of Python experience stop you. Rails 8 with OpenAI just works. Spin up your app on Heroku and watch it work some magic. That’s what I did, and I’m not looking back.