Skip to content

Instantly share code, notes, and snippets.

@coderberry
Created April 21, 2023 14:53
Show Gist options
  • Save coderberry/fe6facab68ee99ba153ca8d9bf935496 to your computer and use it in GitHub Desktop.
Save coderberry/fe6facab68ee99ba153ca8d9bf935496 to your computer and use it in GitHub Desktop.
# 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