Class: ActiveAI::NeuralNetwork::GPT3

Inherits:
ActiveAI::NeuralNetwork show all
Defined in:
lib/activeai/neural_network/gpt3.rb

Constant Summary collapse

DEFAULTS =
{
  model: 'text-davinci-003',
  temperature: 0.7,
  max_tokens: 1000
}

Instance Method Summary collapse

Constructor Details

#initialize(token, uuid: 'system', max_tokens: DEFAULTS[:max_tokens], temperature: DEFAULTS[:temperature], model: DEFAULTS[:model]) ⇒ GPT3

Returns a new instance of GPT3.



14
15
16
17
18
19
20
# File 'lib/activeai/neural_network/gpt3.rb', line 14

def initialize(token, uuid: 'system', max_tokens: DEFAULTS[:max_tokens], temperature: DEFAULTS[:temperature], model: DEFAULTS[:model])
  @token = token
  @uuid = uuid
  @max_tokens = max_tokens
  @temperature = temperature
  @model = model
end

Instance Method Details

#complete(prompt:, stop: nil, suffix: nil) ⇒ Object

TODO move the other stuff besides prompt out?



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/activeai/neural_network/gpt3.rb', line 37

def complete(prompt:, stop: nil, suffix: nil) # TODO move the other stuff besides prompt out?
  post("v1/completions", {
    model: @model,
    prompt: prompt,
    suffix: suffix, # NOTE: doesn't work for fine-tuned models
    stop: stop,
    max_tokens: @max_tokens,
    temperature: @temperature,
    user: @uuid
  })
end

#json_connectionObject



22
23
24
25
26
27
28
29
30
# File 'lib/activeai/neural_network/gpt3.rb', line 22

def json_connection
  @json_connection ||= Faraday.new(
    url: 'https://api.openai.com',
    headers: { 'Authorization' => "Bearer #{@token}" }
  ) do |f|
    f.request :json
    f.response :json
  end
end

#post(path, params = {}) ⇒ Object



32
33
34
35
# File 'lib/activeai/neural_network/gpt3.rb', line 32

def post(path, params={})
  response = json_connection.post(path, params.merge({ user: @uuid }))
  response.body
end