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.
-
.find(provider:) ⇒ OmniAI::Client
Initialize a client by provider (e.g. ‘openai’).
-
.google ⇒ Class<OmniAI::Client>
Lookup the ‘OmniAI::Google::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::Completion
- #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.
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_key ⇒ String?
20 21 22 |
# File 'lib/omniai/client.rb', line 20 def api_key @api_key end |
#host ⇒ String?
26 27 28 |
# File 'lib/omniai/client.rb', line 26 def host @host end |
#logger ⇒ Logger?
23 24 25 |
# File 'lib/omniai/client.rb', line 23 def logger @logger end |
#timeout ⇒ Integer?
29 30 31 |
# File 'lib/omniai/client.rb', line 29 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.
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.
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 |
.google ⇒ Class<OmniAI::Client>
Lookup the ‘OmniAI::Google::Client“. This method requires the provider if it is undefined.
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 |
.mistral ⇒ Class<OmniAI::Client>
Initialize a client for Mistral. This method requires the provider if it is undefined.
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 |
.openai ⇒ Class<OmniAI::Client>
Initialize a client for OpenAI. This method requires the provider if it is undefined.
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
138 139 140 |
# File 'lib/omniai/client.rb', line 138 def chat( = nil, model:, temperature: nil, format: nil, stream: nil, tools: nil, &) raise NotImplementedError, "#{self.class.name}#chat undefined" end |
#connection ⇒ 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
183 184 185 |
# File 'lib/omniai/client.rb', line 183 def (input, model:) raise NotImplementedError, "#{self.class.name}#embed undefined" end |
#inspect ⇒ 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_key ⇒ 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``
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
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 |