<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nuzair</title>
    <description>The latest articles on DEV Community by Nuzair (@red46).</description>
    <link>https://dev.to/red46</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1093587%2F5bdd4f28-c6b1-439f-9a28-ba45b62c6426.jpeg</url>
      <title>DEV Community: Nuzair</title>
      <link>https://dev.to/red46</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/red46"/>
    <language>en</language>
    <item>
      <title>AskGpt: Ask Questions with Context to ChatGPT in your Ruby project</title>
      <dc:creator>Nuzair</dc:creator>
      <pubDate>Fri, 02 Jun 2023 17:27:25 +0000</pubDate>
      <link>https://dev.to/red46/askgpt-ask-questions-with-context-to-chatgpt-in-your-ruby-project-4h8l</link>
      <guid>https://dev.to/red46/askgpt-ask-questions-with-context-to-chatgpt-in-your-ruby-project-4h8l</guid>
      <description>&lt;p&gt;Introducing AskGpt, a Ruby Gem that allows you to ask questions to ChatGPT with context. With AskGpt, you can easily integrate ChatGPT's advanced language capabilities into your Ruby applications and get context-aware responses. Let's explore how to get started!&lt;/p&gt;

&lt;h3&gt;
  
  
  Repository
&lt;/h3&gt;

&lt;p&gt;The source code can be found &lt;a href="https://github.com/Nuzair46/ask_gpt" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;

&lt;p&gt;To install AskGpt, simply add the following line to your Gemfile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="s1"&gt;'ask_gpt'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, run &lt;code&gt;bundle install&lt;/code&gt; to install the gem and its dependencies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Usage
&lt;/h3&gt;

&lt;p&gt;After installing the gem, you need to import AskGpt into your Ruby program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'ask_gpt'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, create a new instance of the &lt;code&gt;AskGpt::GPT&lt;/code&gt; class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;gpt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;AskGpt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;GPT&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;api_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;model: &lt;/span&gt;&lt;span class="s1"&gt;'gpt-3.5-turbo'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;temperature: &lt;/span&gt;&lt;span class="mf"&gt;0.7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;unable_to_get_answer_text: &lt;/span&gt;&lt;span class="kp"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;api_key&lt;/code&gt; parameter is required and represents your OpenAI API key, which you can obtain from the OpenAI website.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;model&lt;/code&gt; parameter is optional and allows you to specify the GPT model you want to use. The default value is &lt;code&gt;gpt-3.5-turbo&lt;/code&gt;, but if you have access to GPT-4, you can use &lt;code&gt;gpt-4&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;temperature&lt;/code&gt; parameter is also optional and controls the "creativity" of the model's responses. The default value is &lt;code&gt;0.7&lt;/code&gt;, providing a good balance between creativity and accuracy. You can adjust the value between 0.0 and 1.0, where higher values yield more creative responses.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;unable_to_get_answer_text&lt;/code&gt; parameter is optional and sets the message returned if the model fails to generate a response. The default value is &lt;code&gt;'Unable to get answer from ChatGPT. Make sure API key is valid and has enough credits.'&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once you have created a new instance of the &lt;code&gt;GPT&lt;/code&gt; class, you can ask questions by calling the &lt;code&gt;ask&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gpt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"What is the capital of France?"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;ask&lt;/code&gt; method takes a single parameter, the question you want to ask the model. If the question is empty, the method will return &lt;code&gt;nil&lt;/code&gt;. Otherwise, it will utilize the context of previous questions and answers to generate a response. If the model fails to generate a response, the &lt;code&gt;ask&lt;/code&gt; method will return the &lt;code&gt;unable_to_get_answer_text&lt;/code&gt; message.&lt;/p&gt;

&lt;p&gt;If the model successfully generates a response, the &lt;code&gt;ask&lt;/code&gt; method will return the answer as a string.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;Here's an example demonstrating how you can utilize AskGpt to ask a series of questions and receive corresponding answers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'ask_gpt'&lt;/span&gt;

&lt;span class="n"&gt;api_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'your_api_key_here'&lt;/span&gt;
&lt;span class="n"&gt;gpt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;AskGpt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;GPT&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;api_key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;questions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="s2"&gt;"What is the capital of France?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"Who was the first president of the United States?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s2"&gt;"What is the tallest mountain in the world?"&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;questions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gpt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Q: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;A: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;answer&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we start by creating a new instance of the &lt;code&gt;GPT&lt;/code&gt; class using our API key. Then, we define an array of questions we want to ask the model. Finally, we iterate through each question, ask the model for an answer, and print the question and answer to the console.&lt;/p&gt;

&lt;p&gt;I hope this Ruby Gem simplifies your integration with ChatGPT and enhances the capabilities of your applications. If you have any questions, feedback, or issues, please don't hesitate to reach out &lt;a href="https://github.com/Nuzair46/ask_gpt" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Happy coding!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>chatgpt</category>
      <category>openai</category>
      <category>rails</category>
    </item>
  </channel>
</rss>
