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

            
                <h2>Send a message</h2>
<%= 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.
<div class="anwser">
<%= @response['choices'][0]['text'] if @response.present? %>
</div>
            
        

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.

Sachin Gevariya

Sachin Gaveriya

Sachin Gevariya is a Founder and Technical Director at Essence Solusoft. He is dedicated to making the best use of modern technologies to craft end-to-end solutions. He also has a vast knowledge of Cloud management. He loves to do coding so still doing the coding. Also, help employees for quality based solutions to clients. Always eager to learn new technology and implement for best solutions.

Say Hello To Essence

Tell us about your project and we are ready to transform your idea into stunning digital experiences

[contact-form-7 id="6"]
Contact form for CTA - Footer