Class: GPT3::Completion

Inherits:
Object
  • Object
show all
Defined in:
lib/gpt3.rb

Constant Summary collapse

DEFAULT_TEMPERATURE =
0.5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*params) ⇒ Completion

Returns a new instance of Completion.



28
29
30
# File 'lib/gpt3.rb', line 28

def initialize(*params)
  self.data = params
end

Instance Attribute Details

#dataObject

defaults:

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



27
28
29
# File 'lib/gpt3.rb', line 27

def data
  @data
end

Class Method Details

.create(prompt, max_tokens: 512, engine: ENGINE, temperature: DEFAULT_TEMPERATURE, n: 1, stop: nil, verbose: false) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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

.searchObject



65
66
67
# File 'lib/gpt3.rb', line 65

def self.search
  raise "Not Implemented"
end