AskGpt: Ask Questions with Context to ChatGPT in your Ruby project
Tags: ruby, chatgpt, openai, rails
Published: 2023-06-02 17:27:25

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!

### Repository
The source code can be found [here](https://github.com/Nuzair46/ask_gpt)

### Installation

To install AskGpt, simply add the following line to your Gemfile:

```ruby
gem 'ask_gpt'
```

Then, run `bundle install` to install the gem and its dependencies.

### Usage

After installing the gem, you need to import AskGpt into your Ruby program:

```ruby
require 'ask_gpt'
```

Next, create a new instance of the `AskGpt::GPT` class:

```ruby
gpt = AskGpt::GPT.new(api_key, model: 'gpt-3.5-turbo', temperature: 0.7, unable_to_get_answer_text: nil)
```

The `api_key` parameter is required and represents your OpenAI API key, which you can obtain from the OpenAI website.

The `model` parameter is optional and allows you to specify the GPT model you want to use. The default value is `gpt-3.5-turbo`, but if you have access to GPT-4, you can use `gpt-4`.

The `temperature` parameter is also optional and controls the "creativity" of the model's responses. The default value is `0.7`, 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.

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

Once you have created a new instance of the `GPT` class, you can ask questions by calling the `ask` method:

```ruby
answer = gpt.ask("What is the capital of France?")
```

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

If the model successfully generates a response, the `ask` method will return the answer as a string.

### Example

Here's an example demonstrating how you can utilize AskGpt to ask a series of questions and receive corresponding answers:

```ruby
require 'ask_gpt'

api_key = 'your_api_key_here'
gpt = AskGpt::GPT.new(api_key)

questions = [
  "What is the capital of France?",
  "Who was the first president of the United States?",
  "What is the tallest mountain in the world?"
]

questions.each do |question|
  answer = gpt.ask(question)
  puts "Q: #{question}\nA: #{answer}\n\n"
end
```

In this example, we start by creating a new instance of the `GPT` 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.

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 [here](https://github.com/Nuzair46/ask_gpt). Happy coding!