What is chatGPT?
ChatGPT is a natural language processing tool developed by the AI research company OpenAI.
Released in Novermber 2022. ChatGPT allows user to ask it questions using conversational, or
natural, language. The language model can answer questions and assist you with tasks like
composing emails, essays, and code.
allows a user to ask it questions using conversational, or natural, language
Now let’s back to our topic.
Here are the steps using which we can easily integergrate Chatgpt’s API in your rails application.
1. Install Gem
First of all, we need to add openAI gem into our gemfile. File: Gemfile
gem "ruby-openai"
Then, run bundle install.
2. ChatGPT-related configuration.
Once you’ve installed the gem, we need to set up openAI API key
2.1 Where do we find openAI key?
Get your API key from https://beta.openai.com/account/api-keys
NOTE: If you are not registered/login into this URL then first do that.
We can pass the API key directly to a new client, like
client = OpenAI::Client.new(access_token: "your_key")
2.2. Added into our app
let’s create a ChatGPT client in our Rails app.
To do this just follow the suggested steps.
Create a new route in our app, like so
get '/chat_gpt', to: 'communication'
Now create communication_controller.rb file.
class CommunicationController < ActionController::Base
skip_before_action :verify_authenticity_token
def chat_gpt
return unless params['question'].present?
@client = OpenAI::Client.new #(access_token: "your_key")
@response = @client.completions(
parameters: {
model: "text-davinci-003",
prompt: params['question'],
max_tokens: 2000
})
end
end
2.3 Now let’s work on the view part.
NOTE: Here i have simply implemented the view part, You can make it more attractive if
you want.
FILE: views/communication/chat_gpt.html.erb
Send a message
<%= form_tag(chat_gpt_path, :method => :get) do %>
<%= text_field_tag 'question', nil, placeholder: 'your quetsion
please' %>
<% end %>
# I AM TAKING RESPONSES ON THE SAME PAGE.
<%= @response['choices'][0]['text'] if @response.present? %>
If you want data into Json form
class CommunicationController < ActionController::Base
skip_before_action :verify_authenticity_token
def chat_gpt
return unless params['question'].present?
@client = OpenAI::Client.new #(access_token: "your_key")
@response = @client.completions(
parameters: {
model: "text-davinci-003",
prompt: params['question'],
max_tokens: 2000
})
end
render json: { text: @response['choices'][0] }
end
API ENDPOINT: http://localhost:3000/chat_gpt
PARAMS: question
RESPONSE:
{
"text": {
"text": "\n\nRuby is a general-purpose, object-oriented programming language. It is
dynamic, reflective, and offers an intuitive syntax. Ruby is commonly used to create web
applications that are scalable, robust and secure. It is also used to develop back-end
systems and API services.",
"index": 0,
"logprobs": null,
"finish_reason": "stop"
}
}
Here are some filters
1. How to use ChatGPT for the name generator?
As we know that we can use chatGPT in many examples. Here is an example of a Business Name Generator. Using that we can get our business name with the help of chatGPT. So for that, we need set prompts like it. Let’s implement this into the above example
class CommunicationController < ActionController::Base
skip_before_action :verify_authenticity_token
def chat_gpt
return unless params['question'].present?
@client = OpenAI::Client.new #(access_token: "your_key")
prompt = 'generate a Business Name as domain\n\nKeywords:' +
params['question']
@response = @client.completions(
parameters: {
model: "text-davinci-003",
prompt: prompt,
max_tokens: 2000
})
end
render json: { text: @response['choices'][0] }
end
RESPONSE:
{
"text": {
"text": "\n\n1. nYogafashion.com",
"index": 0,
"logprobs": null,
"finish_reason": "stop"
}
}
2. Count-related filter
If you want 5 names then just set this prompt.
prompt = "generate a #{params['count']} Business Name as
domain\n\nKeywords: "+ params['question']
And many more..
Yeah, Your app is now ready for checking. Now just start the server and check your chatGPT
responses on the same page.
Do you need any help or advice regarding ROR we’d be happy to update or create new ones for
you.
Get in touch with us.
