Class: OmniAI::Google::Client
- Inherits:
-
Client
- Object
- Client
- OmniAI::Google::Client
- Defined in:
- lib/omniai/google/client.rb
Overview
Constant Summary collapse
- VERTEX_HOSTNAME =
Vertex AI is served from three host shapes under
googleapis.com: the globalaiplatform, a region-prefixed<region>-aiplatform, and the multi-regionaiplatform.<geo>.rep. Only the first two contain the literal "aiplatform.googleapis.com", so a substring test misses the multi-region endpoint.Deliberately wider than those three shapes: any labels are accepted between
aiplatformandgoogleapis.com, so a future multi-region shape needs no change here. Everything it accepts is still undergoogleapis.com.Matched against the parsed hostname rather than the raw host, so a proxy whose path or query merely mentions a Vertex host is not treated as Vertex. A host given with no scheme has no hostname to parse, so the raw value is matched instead; the pattern is anchored, which keeps that safe — note that a schemeless host carrying a path therefore does not match.
/\A(?:[a-z0-9-]+-)?aiplatform(?:\.[a-z0-9-]+)*\.googleapis\.com\z/
Instance Attribute Summary collapse
Instance Method Summary collapse
- #chat(messages = nil, model: Chat::DEFAULT_MODEL, temperature: nil, format: nil, stream: nil, tools: nil) {|prompt| ... } ⇒ OmniAI::Chat::Completion
- #connection ⇒ HTTP::Client
- #credentials? ⇒ Boolean
- #embed(input, model: Embed::DEFAULT_MODEL) ⇒ Object
-
#for_model(model) ⇒ Client
A copy of this client pointed at the model's location in
locations, or this client when no map applies. -
#initialize(api_key: OmniAI::Google.config.api_key, project_id: OmniAI::Google.config.project_id, location_id: UNSET, credentials: OmniAI::Google.config.credentials, logger: OmniAI::Google.config.logger, host: UNSET, version: nil, timeout: OmniAI::Google.config.timeout, locations: OmniAI::Google.config.locations) ⇒ Client
constructor
A new instance of Client.
-
#live(model: Live::DEFAULT_MODEL) {|event| ... } ⇒ OmniAI::Google::Live
Opens a Gemini Live session (Vertex only).
-
#located? ⇒ Boolean
True for a client returned by
#for_model. - #path ⇒ String
- #transcribe(input, model: Transcribe::DEFAULT_MODEL, language: nil, format: nil) ⇒ Object
- #upload(io) ⇒ OmniAI::Google::Upload::File
- #vertex? ⇒ Boolean
Constructor Details
#initialize(api_key: OmniAI::Google.config.api_key, project_id: OmniAI::Google.config.project_id, location_id: UNSET, credentials: OmniAI::Google.config.credentials, logger: OmniAI::Google.config.logger, host: UNSET, version: nil, timeout: OmniAI::Google.config.timeout, locations: OmniAI::Google.config.locations) ⇒ Client
Returns a new instance of Client.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/omniai/google/client.rb', line 41 def initialize( api_key: OmniAI::Google.config.api_key, project_id: OmniAI::Google.config.project_id, location_id: UNSET, credentials: OmniAI::Google.config.credentials, logger: OmniAI::Google.config.logger, host: UNSET, version: nil, timeout: OmniAI::Google.config.timeout, locations: OmniAI::Google.config.locations ) if api_key.nil? && credentials.nil? raise(ArgumentError, "either an `api_key` or `credentials` must be provided") end pinned = !(UNSET.equal?(host) && UNSET.equal?(location_id)) @locations = locations unless pinned host = OmniAI::Google.config.host if UNSET.equal?(host) location_id = OmniAI::Google.config.location_id if UNSET.equal?(location_id) super(api_key:, host:, logger:, timeout:) @project_id = project_id @location_id = location_id @credentials = Credentials.parse(credentials) @version_override = version @version = version || default_version end |
Instance Attribute Details
#version ⇒ String?
29 30 31 |
# File 'lib/omniai/google/client.rb', line 29 def version @version end |
Instance Method Details
#chat(messages = nil, model: Chat::DEFAULT_MODEL, temperature: nil, format: nil, stream: nil, tools: nil) {|prompt| ... } ⇒ OmniAI::Chat::Completion
102 103 104 105 |
# File 'lib/omniai/google/client.rb', line 102 def chat( = nil, model: Chat::DEFAULT_MODEL, temperature: nil, format: nil, stream: nil, tools: nil, **, &) Chat.process!(, model:, temperature:, format:, stream:, tools:, client: self, **, &) end |
#connection ⇒ HTTP::Client
157 158 159 160 161 |
# File 'lib/omniai/google/client.rb', line 157 def connection http = super http = http.auth(auth) if credentials? http end |
#credentials? ⇒ Boolean
164 165 166 |
# File 'lib/omniai/google/client.rb', line 164 def credentials? !@credentials.nil? end |
#embed(input, model: Embed::DEFAULT_MODEL) ⇒ Object
121 122 123 |
# File 'lib/omniai/google/client.rb', line 121 def (input, model: Embed::DEFAULT_MODEL, **) Embed.process!(input, model:, client: self, **) end |
#for_model(model) ⇒ Client
A copy of this client pointed at the model's location in locations, or this client when no map applies.
77 78 79 80 81 82 |
# File 'lib/omniai/google/client.rb', line 77 def for_model(model) return self if @locations.nil? location_id = @locations.fetch(model) { raise UnknownLocationError, model } dup.tap { |client| client.locate!(location_id) } end |
#live(model: Live::DEFAULT_MODEL) {|event| ... } ⇒ OmniAI::Google::Live
Opens a Gemini Live session (Vertex only). See OmniAI::Google::Live#initialize for the options.
143 144 145 |
# File 'lib/omniai/google/client.rb', line 143 def live(model: Live::DEFAULT_MODEL, **, &) Live.open(client: self, model:, **, &) end |
#located? ⇒ Boolean
Returns true for a client returned by #for_model.
85 86 87 |
# File 'lib/omniai/google/client.rb', line 85 def located? @located || false end |
#path ⇒ String
148 149 150 151 152 153 154 |
# File 'lib/omniai/google/client.rb', line 148 def path if @project_id && @location_id "/#{@version}/projects/#{@project_id}/locations/#{@location_id}/publishers/google" else "/#{@version}" end end |
#transcribe(input, model: Transcribe::DEFAULT_MODEL, language: nil, format: nil) ⇒ Object
132 133 134 |
# File 'lib/omniai/google/client.rb', line 132 def transcribe(input, model: Transcribe::DEFAULT_MODEL, language: nil, format: nil, **) Transcribe.process!(input, model:, language:, format:, client: self, **) end |
#upload(io) ⇒ OmniAI::Google::Upload::File
112 113 114 |
# File 'lib/omniai/google/client.rb', line 112 def upload(io) Upload.process!(client: self, io:) end |
#vertex? ⇒ Boolean
183 184 185 186 187 188 |
# File 'lib/omniai/google/client.rb', line 183 def vertex? hostname = URI.parse(@host).hostname || @host VERTEX_HOSTNAME.match?(hostname.downcase) rescue URI::InvalidURIError false end |