ChatGPT + Ruby provides a simple and easy-to-use Gem to access the OpenAI API
MIT License
The chatgpt-ruby
is a Ruby SDK for the OpenAI API, providing methods for generating text and completing prompts using the ChatGPT model.
Add this line to your application's Gemfile:
gem 'chatgpt-ruby'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install chatgpt-ruby
To use the ChatGPT API SDK, you will need an API key from OpenAI. You can obtain an API key by signing up for the OpenAI API beta program .
Once you have an API key, you can create a new ChatGPT::Client
instance with your API key:
require 'chatgpt/client'
api_key = 'your-api-key'
client = ChatGPT::Client.new(api_key)
To generate completions given a prompt, you can use the completions
method:
prompt = 'Hello, my name is'
completions = client.completions(prompt)
# Output: an array of completion strings
You can customize the generation process by passing in additional parameters as a hash:
params = {
engine: 'text-davinci-002',
max_tokens: 50,
temperature: 0.7
}
completions = client.completions(prompt, params)
puts completions["choices"].map { |c| c["text"] }
# Output: an array of completion strings
The chat
method allows for a dynamic conversation with the GPT model. It requires an array of messages where each message is a hash with two properties: role
and content
.
role
can be:
'system'
: Used for instructions that guide the conversation.'user'
: Represents the user's input.'assistant'
: Represents the model's output.content
contains the text message from the corresponding role.
Here is how you would start a chat:
# Define the conversation messages
messages = [
{
role: "system",
content: "You are a helpful assistant."
},
{
role: "user",
content: "Who won the world series in 2020?"
}
]
# Start a chat
response = client.chat(messages)
The response will be a hash containing the model's message(s). You can extract the assistant's message like this:
puts response['choices'][0]['message']['content'] # Outputs the assistant's message
The conversation can be continued by extending the messages
array and calling the chat
method again:
messages << {role: "user", content: "Tell me more about it."}
response = client.chat(messages)
puts response['choices'][0]['message']['content'] # Outputs the assistant's new message
With this method, you can build an ongoing conversation with the model.
For a detailed list of changes for each version of this project, please see the CHANGELOG.
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
.
Bug reports and pull requests are welcome on GitHub at https://github.com/nagstler/chatgpt-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Chatgpt::Ruby project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.