class GPT3::Completion

Constants

DEFAULT_TEMPERATURE

Attributes

data[RW]

defaults: {

"prompt": "Once upon a time",
"max_tokens": 5,
"temperature": 1,
"top_p": 1,
"n": 1,
"stream": false,
"logprobs": null,
"stop": "\n"

}

Public Class Methods

create(prompt, max_tokens: 512, engine: ENGINE, temperature: DEFAULT_TEMPERATURE, n: 1, stop: nil, verbose: false) click to toggle source
# File lib/gpt3.rb, line 32
def self.create(prompt, max_tokens: 512, engine: ENGINE, temperature: DEFAULT_TEMPERATURE, n: 1, stop: nil, verbose: false)
  endpoint = URI.parse("#{GPT3::BASE_URL}/completions")
  header   = {
    'Content-Type':  'application/json',
    'Authorization': 'Bearer ' + SECRET_KEY
  }
  data     = {
    'prompt':      prompt,
    'max_tokens':  max_tokens,
    'temperature': temperature,
    'stop':        stop,
    'n':           n
  }

  if verbose
    puts "+ Creating GPT-3 Completion for the following prompt (with temperature=#{temperature}):"
    puts "| " + prompt.gsub("\n", "\n| ")
    puts "+" + ("-" * 50)
  end

  # Create the HTTP objects
  https         = Net::HTTP.new(endpoint.host, endpoint.port)
  https.use_ssl = true
  request       = Net::HTTP::Post.new(endpoint.request_uri, header)
  request.body  = data.to_json

  # Send the request
  response = https.request(request)

  # Parse it and return formatted API results
  JSON.parse(response.body)
end
new(*params) click to toggle source
# File lib/gpt3.rb, line 28
def initialize(*params)
  self.data = params
end