Class: OmniAI::Client

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

Overview

An abstract class that must be subclassed (e.g. OmniAI::OpenAI::Client).

Usage:

class OmniAI::OpenAI::Client < OmniAI::Client
  def initialize(api_key: ENV.fetch('OPENAI_API_KEY'), logger: nil)
    super
  end

  # @return [HTTP::Client]
  def connection
    @connection ||= super.auth("Bearer: #{@api_key}")
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, logger: nil, host: nil, timeout: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

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

    optional

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

    optional - supports for customzing the host of the client (e.g. ‘localhost:8080’)

  • logger (Logger, nil) (defaults to: nil)

    optional

  • timeout (Integer, nil) (defaults to: nil)

    optional



97
98
99
100
101
102
# File 'lib/omniai/client.rb', line 97

def initialize(api_key: nil, logger: nil, host: nil, timeout: nil)
  @api_key = api_key
  @host = host
  @logger = logger
  @timeout = timeout
end

Instance Attribute Details

#api_keyString?

Returns:

  • (String, nil)


20
21
22
# File 'lib/omniai/client.rb', line 20

def api_key
  @api_key
end

#hostString?

Returns:

  • (String, nil)


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

def host
  @host
end

#loggerLogger?

Returns:

  • (Logger, nil)


23
24
25
# File 'lib/omniai/client.rb', line 23

def logger
  @logger
end

#timeoutInteger?

Returns:

  • (Integer, nil)


29
30
31
# File 'lib/omniai/client.rb', line 29

def timeout
  @timeout
end

Class Method Details

.anthropicClass<OmniAI::Client>

Initialize a client for Anthropic. This method requires the provider if it is undefined.

Returns:

Raises:

  • (OmniAI::Error)

    if the provider is not defined and the gem is not installed



35
36
37
38
39
40
# File 'lib/omniai/client.rb', line 35

def self.anthropic
  require 'omniai/anthropic' unless defined?(OmniAI::Anthropic::Client)
  OmniAI::Anthropic::Client
rescue LoadError
  raise Error, "requires 'omniai-anthropic': `gem install omniai-anthropic`"
end

.find(provider:) ⇒ OmniAI::Client

Initialize a client by provider (e.g. ‘openai’). This method attempts to require the provider.

Parameters:

  • provider (String, Symbol)

    required (e.g. ‘anthropic’, ‘google’, ‘mistral’, ‘openai’, etc)

Returns:

Raises:

  • (OmniAI::Error)

    if the provider is not defined and the gem is not installed



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/omniai/client.rb', line 80

def self.find(provider:, **)
  klass =
    case provider
    when :anthropic, 'anthropic' then anthropic
    when :google, 'google' then google
    when :mistral, 'mistral' then mistral
    when :openai, 'openai' then openai
    else raise Error, "unknown provider=#{provider.inspect}"
    end

  klass.new(**)
end

.googleClass<OmniAI::Client>

Lookup the ‘OmniAI::Google::Client“. This method requires the provider if it is undefined.

Returns:

Raises:

  • (OmniAI::Error)

    if the provider is not defined and the gem is not installed



46
47
48
49
50
51
# File 'lib/omniai/client.rb', line 46

def self.google
  require 'omniai/google' unless defined?(OmniAI::Google::Client)
  OmniAI::Google::Client
rescue LoadError
  raise Error, "requires 'omniai-google': `gem install omniai-google`"
end

.mistralClass<OmniAI::Client>

Initialize a client for Mistral. This method requires the provider if it is undefined.

Returns:

Raises:

  • (OmniAI::Error)

    if the provider is not defined and the gem is not installed



57
58
59
60
61
62
# File 'lib/omniai/client.rb', line 57

def self.mistral
  require 'omniai/mistral' unless defined?(OmniAI::Mistral::Client)
  OmniAI::Mistral::Client
rescue LoadError
  raise Error, "requires 'omniai-mistral': `gem install omniai-mistral`"
end

.openaiClass<OmniAI::Client>

Initialize a client for OpenAI. This method requires the provider if it is undefined.

Returns:

Raises:

  • (OmniAI::Error)

    if the provider is not defined and the gem is not installed



68
69
70
71
72
73
# File 'lib/omniai/client.rb', line 68

def self.openai
  require 'omniai/openai' unless defined?(OmniAI::OpenAI::Client)
  OmniAI::OpenAI::Client
rescue LoadError
  raise Error, "requires 'omniai-openai': `gem install omniai-openai`"
end

Instance Method Details

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

Parameters:

  • messages (String) (defaults to: nil)

    optional

  • model (String)

    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>) (defaults to: nil)

    optional

Yields:

  • (prompt)

    optional

Yield Parameters:

Returns:

  • (OmniAI::Chat::Completion)

Raises:



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

def chat(messages = nil, model:, temperature: nil, format: nil, stream: nil, tools: nil, &)
  raise NotImplementedError, "#{self.class.name}#chat undefined"
end

#connectionHTTP::Client

Returns:

  • (HTTP::Client)


118
119
120
121
122
123
# File 'lib/omniai/client.rb', line 118

def connection
  http = HTTP.persistent(@host)
  http = http.use(instrumentation: { instrumenter: Instrumentation.new(logger: @logger) }) if @logger
  http = http.timeout(@timeout) if @timeout
  http
end

#embed(input, model:) ⇒ OmniAI::Embed::Embedding

Parameters:

  • input (String)

    required

  • model (String)

    required

Returns:

  • (OmniAI::Embed::Embedding)

Raises:



183
184
185
# File 'lib/omniai/client.rb', line 183

def embed(input, model:)
  raise NotImplementedError, "#{self.class.name}#embed undefined"
end

#inspectString

Returns:

  • (String)


105
106
107
108
109
110
# File 'lib/omniai/client.rb', line 105

def inspect
  props = []
  props << "api_key=#{masked_api_key.inspect}" if @api_key
  props << "host=#{@host.inspect}" if @host
  "#<#{self.class.name} #{props.join(' ')}>"
end

#masked_api_keyString

Returns:

  • (String)


113
114
115
# File 'lib/omniai/client.rb', line 113

def masked_api_key
  "#{api_key[..2]}***" if api_key
end

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

Parameters:

  • input (String)

    required

  • model (String)

    required

  • voice (String)

    required

  • 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:



173
174
175
# File 'lib/omniai/client.rb', line 173

def speak(input, model:, voice:, speed: nil, format: nil, &stream)
  raise NotImplementedError, "#{self.class.name}#speak undefined"
end

#transcribe(io, model:, language: nil, prompt: nil, temperature: nil, format: nil) ⇒ OmniAI::Transcribe::Transcription

Parameters:

  • io (String, Pathname, IO)

    required

  • model (String)
  • 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:

Raises:



152
153
154
# File 'lib/omniai/client.rb', line 152

def transcribe(io, model:, language: nil, prompt: nil, temperature: nil, format: nil)
  raise NotImplementedError, "#{self.class.name}#speak undefined"
end