Class: OmniAI::Google::Client

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

Overview

A Google client implementation. Usage:

w/ `api_key``:

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

w/ ENV:

ENV['GOOGLE_API_KEY'] = '...'
client = OmniAI::Google::Client.new

w/ config:

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

client = OmniAI::Google::Client.new

Constant Summary collapse

VERTEX_HOSTNAME =

Vertex AI is served from three host shapes under googleapis.com: the global aiplatform, a region-prefixed <region>-aiplatform, and the multi-region aiplatform.<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 aiplatform and googleapis.com, so a future multi-region shape needs no change here. Everything it accepts is still under googleapis.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

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.

Parameters:

  • api_key (String) (defaults to: OmniAI::Google.config.api_key) —

    default is OmniAI::Google.config.api_key

  • project_id (String) (defaults to: OmniAI::Google.config.project_id) —

    default is OmniAI::Google.config.project_id

  • location_id (String) (defaults to: UNSET) —

    default is OmniAI::Google.config.location_id

  • credentials (Google::Auth::ServiceAccountCredentials) (defaults to: OmniAI::Google.config.credentials) —

    default is OmniAI::Google.config.credentials

  • host (String) (defaults to: UNSET) —

    default is OmniAI::Google.config.host

  • version (String) (defaults to: nil) —

    default is OmniAI::Google.config.version

  • logger (Logger) (defaults to: OmniAI::Google.config.logger) —

    default is OmniAI::Google.config.logger

  • timeout (Integer) (defaults to: OmniAI::Google.config.timeout) —

    default is OmniAI::Google.config.timeout

  • locations (Hash{String => String}, nil) (defaults to: OmniAI::Google.config.locations) —

    default is OmniAI::Google.config.locations; ignored when host or location_id is passed



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?

Returns:

  • (String, nil)


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

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::Chat::Tool>, nil) (defaults to: nil) —

    optional

Yields:

  • (prompt) —

    optional

Yield Parameters:

  • prompt (OmniAI::Chat::Prompt)

Returns:

  • (OmniAI::Chat::Completion)

Raises:

  • (OmniAI::Error)


102
103
104
105
# File 'lib/omniai/google/client.rb', line 102

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

#connection ⇒ HTTP::Client

Returns:

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

Returns:

  • (Boolean)


164
165
166
# File 'lib/omniai/google/client.rb', line 164

def credentials?
  !@credentials.nil?
end

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

Parameters:

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

    required

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

    optional

  • options (Hash) —

    provider-specific options (e.g. task_type: "RETRIEVAL_DOCUMENT")

Raises:

  • (OmniAI::Error)


121
122
123
# File 'lib/omniai/google/client.rb', line 121

def embed(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.

Parameters:

  • model (String)

Returns:

Raises:



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.

Yield Parameters:

Returns:

Raises:



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.

Returns:

  • (Boolean) —

    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

Returns:

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

Parameters:

  • input (String, File, IO) —

    required - audio file path, file object, or GCS URI

  • model (String) (defaults to: Transcribe::DEFAULT_MODEL) —

    optional

  • language (String, Array<String>) (defaults to: nil) —

    optional - language codes for transcription

  • format (Symbol) (defaults to: nil) —

    optional - :json or :verbose_json

  • options (Hash) —

    Gemini transcribe models only: diarization:, word_timestamps:, vocabulary:

Raises:

  • (OmniAI::Error)


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

Parameters:

  • io (File, String) —

    required - a file or URL

Returns:

Raises:



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

def upload(io)
  Upload.process!(client: self, io:)
end

#vertex? ⇒ Boolean

Returns:

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