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

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: OmniAI::Google.config.location_id, credentials: OmniAI::Google.config.credentials, logger: OmniAI::Google.config.logger, host: OmniAI::Google.config.host, version: OmniAI::Google.config.version, timeout: OmniAI::Google.config.timeout) ⇒ 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: OmniAI::Google.config.location_id)

    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: OmniAI::Google.config.host)

    default is OmniAI::Google.config.host

  • version (String) (defaults to: OmniAI::Google.config.version)

    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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/omniai/google/client.rb', line 35

def initialize(
  api_key: OmniAI::Google.config.api_key,
  project_id: OmniAI::Google.config.project_id,
  location_id: OmniAI::Google.config.location_id,
  credentials: OmniAI::Google.config.credentials,
  logger: OmniAI::Google.config.logger,
  host: OmniAI::Google.config.host,
  version: OmniAI::Google.config.version,
  timeout: OmniAI::Google.config.timeout
)
  if api_key.nil? && credentials.nil?
    raise(ArgumentError, "either an `api_key` or `credentials` must be provided")
  end

  super(api_key:, host:, logger:, timeout:)

  @project_id = project_id
  @location_id = location_id
  @credentials = Credentials.parse(credentials)
  @version = version
end

Instance Attribute Details

#versionString?

Returns:

  • (String, nil)


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

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)


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

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

#connectionHTTP::Client

Returns:

  • (HTTP::Client)


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

def connection
  http = super
  http = http.auth(auth) if credentials?
  http
end

#credentials?Boolean

Returns:

  • (Boolean)


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

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)


89
90
91
# File 'lib/omniai/google/client.rb', line 89

def embed(input, model: Embed::DEFAULT_MODEL, **)
  Embed.process!(input, model:, client: self, **)
end

#pathString

Returns:

  • (String)


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

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

Raises:

  • (OmniAI::Error)


99
100
101
# File 'lib/omniai/google/client.rb', line 99

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:



80
81
82
# File 'lib/omniai/google/client.rb', line 80

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

#vertex?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/omniai/google/client.rb', line 125

def vertex?
  @host.include?("aiplatform.googleapis.com")
end