Class: Rasti::AI::OpenAI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rasti/ai/open_ai/client.rb

Constant Summary collapse

BASE_URL =
'https://api.openai.com/v1'.freeze
RETRYABLE_STATUS_CODES =
[502, 503, 504].freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, logger: nil, http_connect_timeout: nil, http_read_timeout: nil, http_max_retries: nil) ⇒ Client

Returns a new instance of Client.



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

def initialize(api_key:nil, logger:nil, http_connect_timeout:nil, http_read_timeout:nil, http_max_retries:nil)
  @api_key = api_key || Rasti::AI.openai_api_key
  @logger = logger || Rasti::AI.logger
  @http_connect_timeout = http_connect_timeout || Rasti::AI.http_connect_timeout
  @http_read_timeout = http_read_timeout || Rasti::AI.http_read_timeout
  @http_max_retries = http_max_retries || Rasti::AI.http_max_retries
end

Instance Method Details

#chat_completions(messages:, model: nil, tools: [], response_format: nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rasti/ai/open_ai/client.rb', line 18

def chat_completions(messages:, model:nil, tools:[], response_format:nil)
  body = {
    model: model || Rasti::AI.openai_default_model,
    messages: messages,
    tools: tools,
    tool_choice: tools.empty? ? 'none' : 'auto'
  }

  body[:response_format] = response_format unless response_format.nil?

  post '/chat/completions', body
end