<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by SANJAY on Medium]]></title>
        <description><![CDATA[Stories by SANJAY on Medium]]></description>
        <link>https://medium.com/@sanjay-vinzuda?source=rss-13ebbbd32726------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*VJaF59nTTiSdubML</url>
            <title>Stories by SANJAY on Medium</title>
            <link>https://medium.com/@sanjay-vinzuda?source=rss-13ebbbd32726------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 21:48:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sanjay-vinzuda/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[ChatGPT With Ruby on Rails]]></title>
            <link>https://medium.com/simform-engineering/chatgpt-with-ruby-on-rails-8a27df7161ed?source=rss-13ebbbd32726------2</link>
            <guid isPermaLink="false">https://medium.com/p/8a27df7161ed</guid>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[openai]]></category>
            <category><![CDATA[chatgpt]]></category>
            <category><![CDATA[ruby-on-rails-development]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[SANJAY]]></dc:creator>
            <pubDate>Mon, 21 Aug 2023 12:44:51 GMT</pubDate>
            <atom:updated>2023-08-21T12:44:51.019Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Learn how to integrate ChatGPT into a Ruby on Rails web application.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ugSmhXP2VqcZ8EAAKV2_8w.png" /></figure><p>ChatGPT has quickly become one of the most talked about AI tools, providing human-like conversational abilities. In this post, I’ll demonstrate how to integrate ChatGPT into a Ruby on Rails web application. By leveraging the OpenAI API and Ruby client library, we can easily add advanced natural language features to our apps.</p><p>Let’s get started!</p><blockquote><strong>Step 1: Initialize a new Rails application.</strong></blockquote><p><strong><em>rails new ChatGPTDemo</em></strong></p><blockquote><strong>Step 2: Install gems.</strong></blockquote><p>Next, you must install the OpenAI Ruby gem in your Rails app. To do this, simply add the following line to your Gemfile:</p><p><strong><em>gem ‘openai’<br>gem ‘httparty’</em></strong></p><p>After adding run:</p><p><strong><em>bundle install</em></strong></p><blockquote><strong>Step 3: Set up your API key</strong></blockquote><p>After the <strong>bundle install</strong>, you’ll need to add an OpenAI API key. For that, go to the OpenAI website and create an account. After successfully signing up, you can find the API key from the URL below:</p><p><a href="https://platform.openai.com/account/api-keys">https://platform.openai.com/account/api-keys</a></p><p>To save the API key, you can use your .<strong>env</strong> file or <strong>credential</strong> file. Here, I’ve used <strong>credentials.yml.enc</strong> file to keep the API key secure.</p><p><strong>EDITOR=”nano” bin/rails credentials:edit</strong></p><p>It’ll open the nano editor to save your OpenAI API key into the credentials file.</p><p><strong><em>openai_api_key: your-openai-key</em></strong></p><p>After adding the key, just save and close it.</p><p>Additionally, you can checkout the rage limit and usage of the API here:</p><p><a href="https://platform.openai.com/account/rate-limits">https://platform.openai.com/account/rate-limits <br></a><a href="https://platform.openai.com/account/usage">https://platform.openai.com/account/usage</a></p><blockquote><strong>Step 4: Create a controller to call service.</strong></blockquote><p><strong>app/controllers/openai_controller.rb:</strong></p><pre>class OpenaiController &lt; ApplicationController<br>  def index<br>    if params[:query]<br>      @response = OpenaiService.new(params[:query]).call<br>    end<br>  end<br>end</pre><blockquote><strong>Step 5: Create a service to call ChatGPT API.</strong></blockquote><p>Now, let’s create a service to call OpenAI API.</p><p><strong>app/services/openai_service.rb:</strong></p><pre>  class OpenaiService<br>    include HTTParty<br>    attr_reader :api_url, :options, :query<br>  <br>    def initialize(query)<br>      @query = query<br>      @api_url = &#39;https://api.openai.com/v1/chat/completions&#39;<br>      @options = {<br>        headers: {<br>          &#39;Content-Type&#39; =&gt; &#39;application/json&#39;,<br>          &#39;Authorization&#39; =&gt; &quot;Bearer #{Rails.application.credentials.chatgpt_api_key}&quot;<br>        }<br>      }<br>    end<br>  <br>    def call<br>      body = {<br>        model: &#39;gpt-3.5-turbo&#39;, #Model can be change<br>        messages: [{ role: &#39;user&#39;, content: query }]<br>      }<br>      response = HTTParty.post(api_url, body: body.to_json, headers: options[:headers], timeout: 500)<br>      raise response[&#39;error&#39;][&#39;message&#39;] unless response.code == 200<br>      response[&#39;choices&#39;][0][&#39;message&#39;][&#39;content&#39;]<br>    end<br>  end</pre><p>You can find the model list <a href="https://platform.openai.com/docs/api-reference/models/list">here</a>.</p><p>Messages have two properties: <strong>role and content.</strong></p><ul><li>The ‘role’ can take one of three values: ‘system’, ‘user’, or the ‘assistant</li><li>The ‘content’ contains the text of the message from the role.</li></ul><blockquote><strong>Step 6: Setup the view to get response</strong></blockquote><p>Finally, you can set up your view as per your requirement. I’ve added a search button as of now to find the content based on the request.</p><p><strong>views/openai/index.html:</strong></p><pre>&lt;div class=&quot;row&quot;&gt;<br>  &lt;%= form_with url: openai_path, method: :get do |form| %&gt;<br>    &lt;div class=&quot;form-group&quot;&gt;<br>      &lt;%= form.label :query %&gt;<br>      &lt;%= form.text_area :query %&gt;<br>      &lt;%= form.submit &#39;get content&#39; %&gt;<br>     &lt;/div&gt;<br>  &lt;% end %&gt;<br>&lt;/div&gt;</pre><p><strong>Resources for ChatGPT:</strong></p><ul><li>Experiment in the <a href="https://platform.openai.com/playground?mode=chat">playground</a>.</li><li>Read the <a href="https://platform.openai.com/docs/api-reference">API reference</a>.</li><li>Explore each <a href="https://platform.openai.com/examples">feature</a>.</li><li>Visit the <a href="https://help.openai.com/en/">help center</a>.</li><li>View the current <a href="https://status.openai.com/">API status</a>.</li><li>Check out the <a href="https://community.openai.com/">OpenAI Developer Forum</a>.</li><li>Learn about the <a href="https://openai.com/policies/usage-policies">usage policies</a>.</li></ul><h3><strong>Conclusion</strong></h3><p>We have walked you through the steps to set up a demo application that effectively leverages ChatGPT’s capabilities. From starting a Rails application and adding necessary gems to handling API keys securely, creating a controller, and setting up a service for OpenAI API calls, you now have a solid understanding of how to seamlessly integrate ChatGPT in a Ruby on Rails application. With this knowledge, you can explore further and tailor this integration to your specific requirements, unlocking the potential of AI-powered conversations in your applications.</p><blockquote><strong>Keep following the </strong><a href="https://medium.com/simform-engineering"><strong>Simform Engineering</strong></a><strong> publication to know more about such insights and the latest trends in the development ecosystem.</strong></blockquote><blockquote><strong>Follow Us: </strong><a href="https://twitter.com/simform"><strong>Twitter</strong></a><strong> | </strong><a href="https://www.linkedin.com/company/simform/"><strong>LinkedIn</strong></a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8a27df7161ed" width="1" height="1" alt=""><hr><p><a href="https://medium.com/simform-engineering/chatgpt-with-ruby-on-rails-8a27df7161ed">ChatGPT With Ruby on Rails</a> was originally published in <a href="https://medium.com/simform-engineering">Simform Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>