Class: ChatGPTRuby::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/chatgpt/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
# File 'lib/chatgpt/client.rb', line 10

def initialize(api_key)
  @api_key = api_key
  @headers = {
    'Authorization' => "Bearer #{@api_key}",
    'Content-Type' => 'application/json'
  }
end

Instance Method Details

#generate(prompt, options = {}) ⇒ Object

Methods to interact with the API



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/chatgpt/client.rb', line 19

def generate(prompt, options = {})
  body = {
    'prompt' => prompt,
    'max_tokens' => options.fetch(:max_tokens, 100),
    'n' => options.fetch(:n, 1),
    'stop' => options.fetch(:stop, nil),
    'temperature' => options.fetch(:temperature, 1.0),
    'top_p' => options.fetch(:top_p, 1.0),
    'frequency_penalty' => options.fetch(:frequency_penalty, 0.0),
    'presence_penalty' => options.fetch(:presence_penalty, 0.0),
  }.to_json

  response = self.class.post('/davinci-codex/completions', headers: @headers, body: body)
  handle_response(response)
end