AskGpt is a Ruby Gem that enables users to ask questions to ChatGPT with context.
To install AskGpt, add the following line to your Gemfile:
gem 'ask_gpt'
Then run bundle install
to install the gem.
After installation, import AskGpt into your Ruby program:
require 'ask_gpt'
Next, create a new instance of the AskGpt::GPT
class:
gpt = AskGpt::GPT.new(api_key)
-
The
api_key
parameter is required and represents your OpenAI API key. You can get it from here. -
The
model
parameter is optional and represents the GPT model that you want to use. The default value isgpt-3.5-turbo
. You can usegpt-4
if you have access to it. -
The
temperature
parameter is also optional and represents the "creativity" of the model's responses. The default value is0.7
, which should provide a good balance between creativity and accuracy. Value ranges from0.0 - 1.0
and the higher the value, the more creative the model's responses will be. -
The
unable_to_get_answer_text
parameter is optional and represents the message that will be returned if the model is unable 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?")
The ask
method takes a single parameter, which is the question that you want to ask the model. If the question is empty, the method will return nil
. Otherwise, it will use the context of previous questions and answers to generate a response. If the model is unable to generate a response, the ask
method will return the unable_to_get_answer_text
message.
If the model is able to generate a response, the ask
method will return the answer as a string.
Here's an example of how you can use AskGpt to ask a series of questions and get answers:
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 first create a new instance of the GPT class using our API key. We then define an array of questions that we want to ask the model. Finally, we loop through each question, ask the model for an answer, and print the question and answer to the console.
This readme was generated by ChatGPT with some guidance and modifications.