https://github.com/nuzair46/ask_gpt
A ruby gem to Interact with OpenAI GPT API with context and history
https://github.com/nuzair46/ask_gpt
chatgpt gpt openai ruby-gem rubygem rubygems
Last synced: 2 months ago
JSON representation
A ruby gem to Interact with OpenAI GPT API with context and history
- Host: GitHub
- URL: https://github.com/nuzair46/ask_gpt
- Owner: Nuzair46
- License: mit
- Created: 2023-04-14T18:18:37.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-04-14T20:24:18.000Z (about 3 years ago)
- Last Synced: 2026-03-21T02:51:13.042Z (3 months ago)
- Topics: chatgpt, gpt, openai, ruby-gem, rubygem, rubygems
- Language: Ruby
- Homepage: https://rubygems.org/gems/ask_gpt
- Size: 11.7 KB
- Stars: 7
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AskGpt
[](https://badge.fury.io/rb/ask_gpt)
## Introduction
AskGpt is a Ruby Gem that enables users to ask questions to ChatGPT with context.
## Installation
To install AskGpt, add the following line to your Gemfile:
```ruby
gem 'ask_gpt'
```
Then run `bundle install` to install the gem.
## Usage
After installation, 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)
```
- The `api_key` parameter is required and represents your OpenAI API key. You can get it from [here](https://platform.openai.com/account/api-keys).
- The `model` parameter is optional and represents the GPT model that you want to use. The default value is `gpt-3.5-turbo`. You can use `gpt-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 is `0.7`, which should provide a good balance between creativity and accuracy. Value ranges from `0.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:
```ruby
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.
## Example
Here's an example of how you can use AskGpt to ask a series of questions and get 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 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.
## Projects using AskGpt
- [ask_gpt_cli](https://github.com/Nuzair46/ask_gpt_cli)
### Remarks
This readme was generated by ChatGPT with some guidance and modifications.