Created
April 21, 2023 14:53
-
-
Save coderberry/fe6facab68ee99ba153ca8d9bf935496 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# USAGE: `ruby ask_cpteeves.rb` | |
# Type "exit" to quit | |
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'ruby-openai' | |
gem 'pry' | |
gem 'tty-prompt' | |
end | |
require 'openai' | |
require 'pry' | |
require 'tty-prompt' | |
def client | |
@client ||= OpenAI::Client.new(access_token: ENV["OPENAI_API_KEY"]) | |
end | |
# @param prompt [String] The prompt to send to OpenAI's Chat API. | |
# @return [String] The response from OpenAI's Chat API. | |
def send_request(prompt) | |
response = | |
client.chat( | |
parameters: { | |
model: "gpt-3.5-turbo", # Required. | |
messages: [{ role: "user", content: prompt}], # Required. | |
temperature: 0.7, | |
}) | |
response['choices'].first['message']['content'].strip | |
end | |
# Define a loop to prompt the user for input and display the response from OpenAI | |
loop do | |
# Prompt the user for input | |
prompt = TTY::Prompt.new | |
question = prompt.ask("What would you like to know? ") | |
# Exit the loop if the user types "exit" | |
break if question.downcase == 'exit' | |
# Send a request to OpenAI's Chat API and display the response | |
response = send_request(question) | |
puts "ChatGPT: #{response}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment