The OpenAI component provides seamless integration with OpenAI's powerful AI models including GPT-4, GPT-3.5-turbo, and more. This component simplifies API interactions and provides a clean Rails interface for AI-powered features.
Whether you're building chatbots, content generation tools, or AI-assisted features, the OpenAI component handles authentication, rate limiting, and error handling for you.
In order to use the OpenAI API integration, you'll need to have a developer account, which you can get on OpenAI's developer portal.
Then, you'll need to get the following:
Start by running the installation command for the OpenAI component:
rails g instrumental:openai
Then add the following to your Rails credentials, and add your OpenAI access token and organization ID.
openai:
access_token:
organization_id:
log_errors: false
Finally, uncomment the code that's in the config/initializers/openai.rb
file.
Restart your Rails server. You're now ready to use the OpenAI API integration!
It installs these essential files for using the OpenAI API integration:
config/initializers/openai.rb
app/services/openai_service.rb
In addition, it adds the following files, which serve as examples for how to use the OpenAI API integration:
openai_prompt_controller.rb
app/views/openai_prompt
That example gives you a page at /openai_prompt
where a user can input instructions, along with several options, click submit, and get an AI-generated response.
The example includes options for a user to provide system instructions, along with various parameters to control the AI's response, such as temperature, max tokens, selected model, and more.
This is intended to be a generic example to show you how you can make use of the OpenAI integration, and the service class that Instrumental Components provides.
The OpenAI component provides a service class to interact with OpenAI's API: app/services/openai_service.rb
Use this to give it a prompt (along with optional parameters), then generate and use a response in your application.
Here's a simple example: Let's say you have a Posts model, and immediately after creating a new post, you want to use AI to rewrite the post's title and make it more click-baity (just what the internet needs, right? 😂)
In your Post.rb
model, you can have something like this:
class Post < ApplicationRecord
# ... other model code ...
after_create :rename
private
def rename
openai_service = OpenaiService.new
prompt = "Generate a single, post title that is more click-baity than the current title, while still being accurate and relevant to the post's content. Just return the title, nothing else."
options = {
system_instructions: "You are a creative content marketing expert specializing in SEO and click-baity titles. Generate only the title, no explanations or quotes.",
max_tokens: 50,
temperature: 0.8
}
response = openai_service.generate_response(prompt, options)
if response[:success] && response[:content].present?
new_name = response[:content].strip.gsub(/['""]/, '')
update(name: new_name)
Rails.logger.info "Post title updated to: #{new_name}"
else
Rails.logger.error "Failed to generate post title: #{response[:error]}"
end
rescue => e
Rails.logger.error "Error renaming post: #{e.message}"
end
end
I created Instrumental Components to make designing and building professional apps with Ruby on Rails easy, fast, and fun. I use it on all of my projects and I hope it helps you build something great.