Class: OmniAI::Client
- Inherits:
-
Object
- Object
- OmniAI::Client
- Defined in:
- lib/omniai/client.rb
Overview
Instance Attribute Summary collapse
Class Method Summary collapse
-
.anthropic ⇒ Class<OmniAI::Client>
Initialize a client for Anthropic.
-
.deepseek ⇒ Class<OmniAI::Client>
Initialize a client for DeepSeek.
-
.discover ⇒ OmniAI::Client
Discover a client by provider (‘openai’ then ‘anthropic’ then ‘google’ then ‘mistral’ then ‘deepseek’).
-
.find(provider:) ⇒ OmniAI::Client
Initialize a client by provider (e.g. ‘openai’).
-
.google ⇒ Class<OmniAI::Client>
Lookup the ‘OmniAI::Google::Client“.
-
.llama ⇒ Class<OmniAI::Client>
Lookup the ‘OmniAI::LLama::Client“.
-
.mistral ⇒ Class<OmniAI::Client>
Initialize a client for Mistral.
-
.openai ⇒ Class<OmniAI::Client>
Initialize a client for OpenAI.
Instance Method Summary collapse
- #chat(messages = nil, model:, temperature: nil, format: nil, stream: nil, tools: nil) {|prompt| ... } ⇒ OmniAI::Chat::Response
- #connection ⇒ HTTP::Client
- #embed(input, model:) ⇒ OmniAI::Embed::Embedding
-
#initialize(api_key: nil, logger: nil, host: nil, timeout: nil) ⇒ Client
constructor
A new instance of Client.
- #inspect ⇒ String
- #masked_api_key ⇒ String
- #speak(input, model:, voice:, speed: nil, format: nil) {|output| ... } ⇒ Tempfile
- #transcribe(io, model:, language: nil, prompt: nil, temperature: nil, format: nil) ⇒ OmniAI::Transcribe::Transcription
Constructor Details
#initialize(api_key: nil, logger: nil, host: nil, timeout: nil) ⇒ Client
Returns a new instance of Client.
158 159 160 161 162 163 |
# File 'lib/omniai/client.rb', line 158 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_key ⇒ String?
21 22 23 |
# File 'lib/omniai/client.rb', line 21 def api_key @api_key end |
#host ⇒ String?
29 30 31 |
# File 'lib/omniai/client.rb', line 29 def host @host end |
#logger ⇒ Logger?
25 26 27 |
# File 'lib/omniai/client.rb', line 25 def logger @logger end |
#timeout ⇒ Integer?
33 34 35 |
# File 'lib/omniai/client.rb', line 33 def timeout @timeout end |
Class Method Details
.anthropic ⇒ Class<OmniAI::Client>
Initialize a client for Anthropic. This method requires the provider if it is undefined.
40 41 42 43 44 45 |
# File 'lib/omniai/client.rb', line 40 def self.anthropic require "omniai/anthropic" unless defined?(OmniAI::Anthropic::Client) OmniAI::Anthropic::Client rescue ::LoadError raise LoadError, "requires 'omniai-anthropic': `gem install omniai-anthropic`" end |
.deepseek ⇒ Class<OmniAI::Client>
Initialize a client for DeepSeek. This method requires the provider if it is undefined.
52 53 54 55 56 57 |
# File 'lib/omniai/client.rb', line 52 def self.deepseek require "omniai/deepseek" unless defined?(OmniAI::DeepSeek::Client) OmniAI::DeepSeek::Client rescue ::LoadError raise LoadError, "requires 'omniai-deepseek': `gem install omniai-deepseek`" end |
.discover ⇒ OmniAI::Client
Discover a client by provider (‘openai’ then ‘anthropic’ then ‘google’ then ‘mistral’ then ‘deepseek’).
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/omniai/client.rb', line 112 def self.discover(**) %i[openai anthropic google mistral deepseek].each do |provider| return find(provider:, **) rescue LoadError next end raise LoadError, <<~TEXT Please run one of the following commands to install a provider specific gem: `gem install omniai-openai` `gem install omniai-anthropic` `gem install omniai-deepseek` `gem install omniai-llama` `gem install omniai-google` `gem install omniai-mistral` TEXT end |
.find(provider:) ⇒ OmniAI::Client
Initialize a client by provider (e.g. ‘openai’). This method attempts to require the provider.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/omniai/client.rb', line 139 def self.find(provider:, **) klass = case provider when :anthropic, "anthropic" then anthropic when :deepseek, "deepseek" then deepseek when :google, "google" then google when :llama, "llama" then llama when :mistral, "mistral" then mistral when :openai, "openai" then openai else raise Error, "unknown provider=#{provider.inspect}" end klass.new(**) end |
.google ⇒ Class<OmniAI::Client>
Lookup the ‘OmniAI::Google::Client“. This method requires the provider if it is undefined.
76 77 78 79 80 81 |
# File 'lib/omniai/client.rb', line 76 def self.google require "omniai/google" unless defined?(OmniAI::Google::Client) OmniAI::Google::Client rescue ::LoadError raise LoadError, "requires 'omniai-google': `gem install omniai-google`" end |
.llama ⇒ Class<OmniAI::Client>
Lookup the ‘OmniAI::LLama::Client“. This method requires the provider if it is undefined.
64 65 66 67 68 69 |
# File 'lib/omniai/client.rb', line 64 def self.llama require "omniai/llama" unless defined?(OmniAI::Llama::Client) OmniAI::Llama::Client rescue ::LoadError raise LoadError, "requires 'omniai-llama': `gem install omniai-llama`" end |
.mistral ⇒ Class<OmniAI::Client>
Initialize a client for Mistral. This method requires the provider if it is undefined.
88 89 90 91 92 93 |
# File 'lib/omniai/client.rb', line 88 def self.mistral require "omniai/mistral" unless defined?(OmniAI::Mistral::Client) OmniAI::Mistral::Client rescue ::LoadError raise LoadError, "requires 'omniai-mistral': `gem install omniai-mistral`" end |
.openai ⇒ Class<OmniAI::Client>
Initialize a client for OpenAI. This method requires the provider if it is undefined.
100 101 102 103 104 105 |
# File 'lib/omniai/client.rb', line 100 def self.openai require "omniai/openai" unless defined?(OmniAI::OpenAI::Client) OmniAI::OpenAI::Client rescue ::LoadError raise LoadError, "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::Response
199 200 201 |
# File 'lib/omniai/client.rb', line 199 def chat( = nil, model:, temperature: nil, format: nil, stream: nil, tools: nil, &) raise NotImplementedError, "#{self.class.name}#chat undefined" end |
#connection ⇒ HTTP::Client
179 180 181 182 183 184 |
# File 'lib/omniai/client.rb', line 179 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
244 245 246 |
# File 'lib/omniai/client.rb', line 244 def (input, model:) raise NotImplementedError, "#{self.class.name}#embed undefined" end |
#inspect ⇒ String
166 167 168 169 170 171 |
# File 'lib/omniai/client.rb', line 166 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_key ⇒ String
174 175 176 |
# File 'lib/omniai/client.rb', line 174 def masked_api_key "#{api_key[..2]}***" if api_key end |
#speak(input, model:, voice:, speed: nil, format: nil) {|output| ... } ⇒ Tempfile
234 235 236 |
# File 'lib/omniai/client.rb', line 234 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
213 214 215 |
# File 'lib/omniai/client.rb', line 213 def transcribe(io, model:, language: nil, prompt: nil, temperature: nil, format: nil) raise NotImplementedError, "#{self.class.name}#transcribe undefined" end |