Class: OmniAI::OpenAI::Client

Inherits:
Client
  • Object
show all
Defined in:
lib/omniai/openai/client.rb

Overview

An OpenAI client implementation. Usage:

w/ ‘api_key“:

client = OmniAI::OpenAI::Client.new(api_key: '...')

w/ ENV:

ENV['OPENAI_API_KEY'] = '...'
client = OmniAI::OpenAI::Client.new

w/ config:

OmniAI::OpenAI.configure do |config|
  config.api_key = '...'
end

client = OmniAI::OpenAI::Client.new

Constant Summary collapse

VERSION =
'v1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: OmniAI::OpenAI.config.api_key, api_prefix: '', host: OmniAI::OpenAI.config.host, organization: OmniAI::OpenAI.config.organization, project: OmniAI::OpenAI.config.project, logger: OmniAI::OpenAI.config.logger, timeout: OmniAI::OpenAI.config.timeout) ⇒ Client

Returns a new instance of Client.

Parameters:

  • api_key (String, nil) (defaults to: OmniAI::OpenAI.config.api_key)

    optional - defaults to ‘OmniAI::OpenAI.config.api_key`

  • api_prefix (String, nil) (defaults to: '')

    optional - defaults to empty string

  • host (String) (defaults to: OmniAI::OpenAI.config.host)

    optional - defaults to ‘OmniAI::OpenAI.config.host`

  • project (String, nil) (defaults to: OmniAI::OpenAI.config.project)

    optional - defaults to ‘OmniAI::OpenAI.config.project`

  • organization (String, nil) (defaults to: OmniAI::OpenAI.config.organization)

    optional - defaults to ‘OmniAI::OpenAI.config.organization`

  • logger (Logger, nil) (defaults to: OmniAI::OpenAI.config.logger)

    optional - defaults to ‘OmniAI::OpenAI.config.logger`

  • timeout (Integer, nil) (defaults to: OmniAI::OpenAI.config.timeout)

    optional - defaults to ‘OmniAI::OpenAI.config.timeout`



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/omniai/openai/client.rb', line 34

def initialize(
  api_key: OmniAI::OpenAI.config.api_key,
  api_prefix: '',
  host: OmniAI::OpenAI.config.host,
  organization: OmniAI::OpenAI.config.organization,
  project: OmniAI::OpenAI.config.project,
  logger: OmniAI::OpenAI.config.logger,
  timeout: OmniAI::OpenAI.config.timeout
)
  if api_key.nil? && host.eql?(Config::DEFAULT_HOST)
    raise(
      ArgumentError,
      %(ENV['OPENAI_API_KEY'] must be defined or `api_key` must be passed when using #{Config::DEFAULT_HOST})
    )
  end

  super(api_key:, host:, logger:, timeout:)

  @organization = organization
  @project = project

  @api_prefix = api_prefix
  return if @api_prefix.empty? || @api_prefix.start_with?('/')

  @api_prefix.prepend('/')
end

Instance Attribute Details

#api_prefixObject (readonly)

Returns the value of attribute api_prefix.



25
26
27
# File 'lib/omniai/openai/client.rb', line 25

def api_prefix
  @api_prefix
end

Instance Method Details

#assistantsOmniAI::OpenAI::Assistants



138
139
140
# File 'lib/omniai/openai/client.rb', line 138

def assistants
  Assistants.new(client: self)
end

#chat(messages = nil, model: Chat::DEFAULT_MODEL, temperature: nil, format: nil, stream: nil, tools: nil) {|prompt| ... } ⇒ OmniAI::Chat::Completion

Parameters:

  • messages (String) (defaults to: nil)

    optional

  • model (String) (defaults to: Chat::DEFAULT_MODEL)

    optional

  • format (Symbol) (defaults to: nil)

    optional :text or :json

  • temperature (Float, nil) (defaults to: nil)

    optional

  • stream (Proc, nil) (defaults to: nil)

    optional

  • tools (Array<OmniAI::Tool>, nil) (defaults to: nil)

    optional

Yields:

  • (prompt)

Yield Parameters:

  • prompt (OmniAI::Chat::Prompt)

Returns:

  • (OmniAI::Chat::Completion)

Raises:

  • (OmniAI::Error)


85
86
87
# File 'lib/omniai/openai/client.rb', line 85

def chat(messages = nil, model: Chat::DEFAULT_MODEL, temperature: nil, format: nil, stream: nil, tools: nil, &)
  Chat.process!(messages, model:, temperature:, format:, stream:, tools:, client: self, &)
end

#connectionHTTP::Client

Returns:

  • (HTTP::Client)


62
63
64
65
66
67
68
69
70
# File 'lib/omniai/openai/client.rb', line 62

def connection
  @connection ||= begin
    http = super
    http = http.auth("Bearer #{@api_key}") if @api_key
    http = http.headers('OpenAI-Organization': @organization) if @organization
    http = http.headers('OpenAI-Project': @project) if @project
    http
  end
end

#embed(input, model: Embed::DEFAULT_MODEL) ⇒ Object

Parameters:

  • input (String, Array<String>, Array<Integer>)

    required

  • model (String) (defaults to: Embed::DEFAULT_MODEL)

    optional

Raises:

  • (OmniAI::Error)


93
94
95
# File 'lib/omniai/openai/client.rb', line 93

def embed(input, model: Embed::DEFAULT_MODEL)
  Embed.process!(input, model:, client: self)
end

#filesOmniAI::OpenAI::Files



133
134
135
# File 'lib/omniai/openai/client.rb', line 133

def files
  Files.new(client: self)
end

#speak(input, model: Speak::Model::TTS_1_HD, voice: Speak::Voice::ALLOY, speed: nil, format: nil) {|output| ... } ⇒ Tempfile``

Parameters:

  • input (String)

    required

  • model (String) (defaults to: Speak::Model::TTS_1_HD)

    optional

  • voice (String) (defaults to: Speak::Voice::ALLOY)

    optional

  • speed (Float) (defaults to: nil)

    optional

  • format (String) (defaults to: nil)

    optional (default “aac”):

    • “aac”

    • “mp3”

    • “flac”

    • “opus”

    • “pcm”

    • “wav”

Yields:

  • (output)

    optional

Returns:

  • (Tempfile``)

Raises:

  • (OmniAI::Error)


128
129
130
# File 'lib/omniai/openai/client.rb', line 128

def speak(input, model: Speak::Model::TTS_1_HD, voice: Speak::Voice::ALLOY, speed: nil, format: nil, &)
  Speak.process!(input, model:, voice:, speed:, format:, client: self, &)
end

#threadsOmniAI::OpenAI::Threads



143
144
145
# File 'lib/omniai/openai/client.rb', line 143

def threads
  Threads.new(client: self)
end

#transcribe(path, model: Transcribe::Model::WHISPER, language: nil, prompt: nil, temperature: nil, format: nil) ⇒ OmniAI::Transcribe

Parameters:

  • path (String)
  • model (String) (defaults to: Transcribe::Model::WHISPER)
  • language (String, nil) (defaults to: nil)

    optional

  • prompt (String, nil) (defaults to: nil)

    optional

  • temperature (Float, nil) (defaults to: nil)

    optional

  • format (Symbol) (defaults to: nil)

    :text, :srt, :vtt, or :json (default)

Returns:

  • (OmniAI::Transcribe)

Raises:

  • (OmniAI::Error)


107
108
109
# File 'lib/omniai/openai/client.rb', line 107

def transcribe(path, model: Transcribe::Model::WHISPER, language: nil, prompt: nil, temperature: nil, format: nil)
  Transcribe.process!(path, model:, language:, prompt:, temperature:, format:, client: self)
end