Chat GPT basics with Ruby on Rails
Related videos
In today's lesson, we will explore building AI-powered chat applications using Ruby and the OpenAI Gem. We'll build a Chat GPT chatbot that pretends to be Jean-Luc Picard. Engage shields, and go to maximum warp. Let's start!
Getting Your OpenAI API Key
To get started, the first thing we need is an API key from OpenAI. Visit platform.openai.com, follow the instructions to sign up, and retrieve your API key.
Setting Up Your Credentials
Next, let's save your API key in your Rails credentials. Open the Rails credentials by running to bin/rails credentials:edit
. Add a new entry for your OpenAI API key as follows:
open_ai_api_key: [YOUR_API_KEY]
Ensure to replace [YOUR_API_KEY]
with the API key you got from OpenAI.
Installing Ruby OpenAI Gem
With our key in place, we need to install the OpenAI gem. Go to your terminal and type bundle add ruby-openai
. This command will download and install the OpenAI gem for your application.
Setting Up The Chat Service
Now, we are ready to develop our chatbot. Start by creating a ChatService
class within a new file named app/services/chat_service.rb
. Your class should have an initializer method that takes a message as an argument.
class ChatService
attr_reader :message
def initialize(message)
@message = message
end
end
Next, create a call
method where we'll set our chatbot's main functionality. This method will have multiple responsibilities:
- Create an instance of the OpenAI client
- Train the client with prompts
- Send the user's message
- Parse and return the client's response
class ChatService
attr_reader :message
def initialize(message)
@message = message
end
def call
# We will implement these tasks in following steps
end
end
At this stage, we can use this service in your Rails controller to accept user input, process it, and display the AI response. But to save time, for testing, we’ll just run the service directly in our rails console.
Implementing The OpenAI Client
In our call
method, let's implement the creation of the OpenAI client. We'll save it in an instance variable so that it's only instantiated once.
class ChatService
#...
private
def client
@_client ||= OpenAI::Client.new(api_key: Rails.application.credentials.open_ai_api_key)
end
end
Here, we're using our API key set in our Rails credentials file. If your application will use the client across different services, consider setting up an initializer in config/initializers/openai.rb
to handle this function across the scope of your application.
Training Chat GPT using prompts
Our client is set up. Now let's add some training prompts to make the bot learn more about our application. We'll use prompts about Jean-Luc Picard from Star Trek, but you're encouraged to come with your own prompts. An example of the training prompts is as follows:
class ChatService
# ...
private
def training_prompts
[
"Do you know who captain jean luc picard is? Just tell me yes or no",
"Can you pretend to be captain jean luc picard from here on out? Answer yes or no",
]
end
end
Sending User's Message
After training the bot, we'll send the user's message to the bot:
class ChatService
# ...
def call
messages = training_prompts.map do |prompt|
{ role: "system", content: prompt}
end
messages << { role: "user", content: message}
response = client.chat(
parameters: {
model: "gpt-3.5-turbo",
messages: messages,
temperature: 0.7,
}
)
response.dig("choices", 0, "message", "content")
end
# ...
end
Here, the call
function will take the user's message, add it to the training prompts, send it to Chat GPT and parse out the response.
Final Steps
With the implementations in place, we can run our service in the Rails console to test it out:
bin/rails console
Inside the console, call our chat service as shown:
ChatService.new(message: "the borg are coming to attack, what should we do captain?").call
The service returns ChatGPT’s response to our message.
That's it! We've built a basic AI-powered chatbot with Ruby and OpenAI gem. We have covered the basisc that are needed to incorporate chat AI features into your app.
I hope this walkthrough has been helpful. Let me know if you want more OpenAI or Chat GPT content. Cheers!
Also, you can probably tell but the thumbnail for this episode was fittingly generated using dalle-2.