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, 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)

    optional - defaults to ‘OmniAI::Google.config.api_key`

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

    optional - defaults to ‘OmniAI::Google.config.host`

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

    optional - defaults to ‘OmniAI::Google.config.version`

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

    optional - defaults to ‘OmniAI::Google.config.logger`

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

    optional - defaults to ‘OmniAI::Google.config.timeout`

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/omniai/google/client.rb', line 32

def initialize(
  api_key: OmniAI::Google.config.api_key,
  logger: OmniAI::Google.config.logger,
  host: OmniAI::Google.config.host,
  version: OmniAI::Google.config.version,
  timeout: OmniAI::Google.config.timeout
)
  raise(ArgumentError, %(ENV['GOOGLE_API_KEY'] must be defined or `api_key` must be passed)) if api_key.nil?

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

  @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)


59
60
61
# File 'lib/omniai/google/client.rb', line 59

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

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

Parameters:

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

    required

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

    optional

Raises:

  • (OmniAI::Error)


76
77
78
# File 'lib/omniai/google/client.rb', line 76

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

#pathString

Returns:

  • (String)


81
82
83
84
85
86
87
# File 'lib/omniai/google/client.rb', line 81

def path
  if @project_id
    "/#{@version}/projects/#{@project_id}/locations/#{@location}/publishers/google"
  else
    "/#{@version}"
  end
end

#upload(io) ⇒ OmniAI::Google::Upload::File

Parameters:

  • io (File, String)

    required - a file or URL

Returns:

Raises:



68
69
70
# File 'lib/omniai/google/client.rb', line 68

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