OmniAI::Google

A Google implementation of the OmniAI APIs.

Installation

gem install omniai-google

Usage

Client

A client is setup as follows if ENV['GOOGLE_API_KEY'] exists:

client = OmniAI::Google::Client.new

A client may also be passed the following options:

  • api_key (required - default is ENV['GOOGLE_API_KEY'])

  • host (optional)

  • version (optional - options are v1 or v1beta)

Configuration

Global configuration is supported for the following options:

OmniAI::Google.configure do |config|
  config.api_key = 'sk-...' # default: ENV['GOOGLE_API_KEY']
  config.host = '...' # default: 'https://generativelanguage.googleapis.com'
  config.version = OmniAI::Google::Config::Version::BETA # either 'v1' or 'v1beta'
end

Chat

A chat completion is generated by passing in a simple text prompt:

completion = client.chat('Tell me a joke!')
completion.text # 'Why did the chicken cross the road? To get to the other side.'

A chat completion may also be generated by using the prompt builder:

completion = client.chat do |prompt|
  prompt.system('Your are an expert in geography.')
  prompt.user('What is the capital of Canada?')
end
completion.text # 'The capital of Canada is Ottawa.'

Model

model takes an optional string (default is gemini-1.5-pro):

completion = client.chat('How fast is a cheetah?', model: OmniAI::Google::Chat::Model::GEMINI_FLASH)
completion.text # 'A cheetah can reach speeds over 100 km/h.'

API Reference model

Temperature

temperature takes an optional float between 0.0 and 2.0:

completion = client.chat('Pick a number between 1 and 5', temperature: 2.0)
completion.text # '3'

API Reference temperature

Stream

stream takes an optional a proc to stream responses in real-time chunks instead of waiting for a complete response:

stream = proc do |chunk|
  print(chunk.text) # 'Better', 'three', 'hours', ...
end
client.chat('Be poetic.', stream:)

Upload

An upload is especially useful when processing audio / image / video / text files. To use:

CAT_URL = 'https://images.unsplash.com/photo-1472491235688-bdc81a63246e?fm=jpg'
DOG_URL = 'https://images.unsplash.com/photo-1517849845537-4d257902454a?fm=jpg'

begin
  cat_upload = client.upload(CAT_URL)
  dog_upload = client.upload(DOG_URL)

  completion = client.chat(stream: $stdout) do |prompt|
    prompt.user do |message|
      message.text 'What are these photos of?'
      message.url(cat_upload.uri, cat_upload.mime_type)
      message.url(dog_upload.uri, dog_upload.mime_type)
    end
  end
ensure
  cat_upload.delete!
  dog_upload.delete!
end

API Reference stream

Embed

Text can be converted into a vector embedding for similarity comparison usage via:

response = client.embed('The quick brown fox jumps over a lazy dog.')
response.embedding # [0.0, ...]