DEV Community


Posted on

AskGpt: Ask Questions with Context to ChatGPT in your Ruby project

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!


The source code can be found here


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

gem 'ask_gpt'
Enter fullscreen mode Exit fullscreen mode

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


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

require 'ask_gpt'
Enter fullscreen mode Exit fullscreen mode

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

gpt =, model: 'gpt-3.5-turbo', temperature: 0.7, unable_to_get_answer_text: nil)
Enter fullscreen mode Exit fullscreen mode

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:

answer = gpt.ask("What is the capital of France?")
Enter fullscreen mode Exit fullscreen mode

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.


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

require 'ask_gpt'

api_key = 'your_api_key_here'
gpt =

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"
Enter fullscreen mode Exit fullscreen mode

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. Happy coding!

Top comments (0)