Class: Layered::Assistant::Clients::OpenAI

Inherits:
Base
  • Object
show all
Defined in:
app/services/layered/assistant/clients/openai.rb

Instance Method Summary collapse

Methods inherited from Base

for, #initialize

Constructor Details

This class inherits a constructor from Layered::Assistant::Clients::Base

Instance Method Details

#chat(messages:, model:, stream_proc:) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/services/layered/assistant/clients/openai.rb', line 5

def chat(messages:, model:, stream_proc:)
  formatted = MessagesService.new.format(messages, provider: @provider)

  client_options = {
    access_token: @api_key,
    log_errors: Layered::Assistant.log_errors,
    request_timeout: Layered::Assistant.api_request_timeout
  }
  if @provider.url.present?
    client_options[:uri_base] = @provider.url.sub(/\/\z/, "")
    client_options[:api_version] = "" # Gemini and other OpenAI-compatible APIs use their own path
  end

  ::OpenAI::Client.new(**client_options) do |f|
    if Layered::Assistant.log_errors
      f.response :logger, Logger.new($stdout), bodies: true
    end
  end.chat(
    parameters: {
      model: model,
      messages: formatted[:messages],
      stream: stream_proc,
      stream_options: { include_usage: true }
    }
  )
end