Class: ChatGPTRuby::Client
- Inherits:
-
Object
- Object
- ChatGPTRuby::Client
- Includes:
- HTTParty
- Defined in:
- lib/chatgpt/client.rb
Instance Method Summary collapse
-
#generate(prompt, options = {}) ⇒ Object
Methods to interact with the API.
-
#initialize(api_key) ⇒ Client
constructor
A new instance of Client.
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, = {}) body = { 'prompt' => prompt, 'max_tokens' => .fetch(:max_tokens, 100), 'n' => .fetch(:n, 1), 'stop' => .fetch(:stop, nil), 'temperature' => .fetch(:temperature, 1.0), 'top_p' => .fetch(:top_p, 1.0), 'frequency_penalty' => .fetch(:frequency_penalty, 0.0), 'presence_penalty' => .fetch(:presence_penalty, 0.0), }.to_json response = self.class.post('/davinci-codex/completions', headers: @headers, body: body) handle_response(response) end |