Class: LLM::Google

Inherits:
Provider show all
Defined in:
lib/llm/providers/google.rb,
lib/llm/providers/google/audio.rb,
lib/llm/providers/google/files.rb,
lib/llm/providers/google/utils.rb,
lib/llm/providers/google/images.rb,
lib/llm/providers/google/models.rb,
lib/llm/providers/google/error_handler.rb,
lib/llm/providers/google/stream_parser.rb,
lib/llm/providers/google/request_adapter.rb,
lib/llm/providers/google/response_adapter.rb

Overview

The Google class implements a provider for Gemini. The Google provider can accept multiple inputs (text, images, audio, and video). The inputs can be provided inline via the prompt for files under 20MB or via the Gemini Files API for files that are over 20MB.

Examples:

#!/usr/bin/env ruby
require "llm"

llm = LLM.google(key: ENV["KEY"])
ctx = LLM::Context.new(llm)
ctx.talk ["Tell me about this photo", ctx.local_file("/images/photo.png")]
ctx.messages.select(&:assistant?).each { print "[#{_1.role}]", _1.content, "\n" }

Defined Under Namespace

Modules: Utils Classes: Audio, Files, Images, Models

Constant Summary collapse

HOST =
"generativelanguage.googleapis.com"

Instance Method Summary collapse

Methods inherited from Provider

#chat, #inspect, #interrupt!, #moderations, #persist!, #respond, #responses, #schema, #server_tool, #streamable?, #tool_role, #tracer, #tracer=, #vector_stores, #with

Constructor Details

#initializeGoogle

Returns a new instance of Google.

Parameters:

  • key (String, nil)

    The secret key for authentication



38
39
40
# File 'lib/llm/providers/google.rb', line 38

def initialize(**)
  super(host: HOST, **)
end

Instance Method Details

#assistant_roleString

Returns the role of the assistant in the conversation. Usually "assistant" or "model"

Returns:

  • (String)

    Returns the role of the assistant in the conversation. Usually "assistant" or "model"



174
175
176
# File 'lib/llm/providers/google.rb', line 174

def assistant_role
  "model"
end

#nameSymbol

Returns the provider's name

Returns:

  • (Symbol)

    Returns the provider's name



45
46
47
# File 'lib/llm/providers/google.rb', line 45

def name
  :google
end

#embed(input, model: "gemini-embedding-001", **params) ⇒ LLM::Response

Provides an embedding

Parameters:

  • input (String, Array<String>)

    The input to embed

  • model (String) (defaults to: "gemini-embedding-001")

    The embedding model to use

  • params (Hash)

    Other embedding parameters

Returns:



56
57
58
59
60
61
62
63
64
65
# File 'lib/llm/providers/google.rb', line 56

def embed(input, model: "gemini-embedding-001", **params)
  model = model.respond_to?(:id) ? model.id : model
  path = ["/v1beta/models/#{model}", "embedContent?key=#{@key}"].join(":")
  req = Net::HTTP::Post.new(path, headers)
  req.body = LLM.json.dump({content: {parts: [{text: input}]}})
  res, span, tracer = execute(request: req, operation: "embeddings", model:)
  res = ResponseAdapter.adapt(res, type: :embedding)
  tracer.on_request_finish(operation: "embeddings", model:, res:, span:)
  res
end

#complete(prompt, params = {}) ⇒ LLM::Response

Provides an interface to the chat completions API

Examples:

llm = LLM.openai(key: ENV["KEY"])
messages = [{role: "system", content: "Your task is to answer all of my questions"}]
res = llm.complete("5 + 2 ?", messages:)
print "[#{res.messages[0].role}]", res.messages[0].content, "\n"

Parameters:

  • prompt (String)

    The input prompt to be completed

  • params (Hash) (defaults to: {})

    The parameters to maintain throughout the conversation. Any parameter the provider supports can be included and not only those listed here.

Returns:

Raises:

See Also:



77
78
79
80
81
82
83
84
85
# File 'lib/llm/providers/google.rb', line 77

def complete(prompt, params = {})
  params, stream, tools, role, model = normalize_complete_params(params)
  req = build_complete_request(prompt, params, role, model, stream)
  res, span, tracer = execute(request: req, stream: stream, operation: "chat", model:)
  res = ResponseAdapter.adapt(res, type: :completion)
    .extend(Module.new { define_method(:__tools__) { tools } })
  tracer.on_request_finish(operation: "chat", model:, res:, span:)
  res
end

#audioLLM::Google::Audio

Provides an interface to Gemini's audio API

Returns:

See Also:



91
92
93
# File 'lib/llm/providers/google.rb', line 91

def audio
  LLM::Google::Audio.new(self)
end

#imagessee LLM::Google::Images

Provides an interface to Gemini's image generation API

Returns:

See Also:



99
100
101
# File 'lib/llm/providers/google.rb', line 99

def images
  LLM::Google::Images.new(self)
end

#filesLLM::Google::Files

Provides an interface to Gemini's file management API

Returns:

See Also:



107
108
109
# File 'lib/llm/providers/google.rb', line 107

def files
  LLM::Google::Files.new(self)
end

#modelsLLM::Google::Models

Provides an interface to Gemini's models API

Returns:

See Also:



115
116
117
# File 'lib/llm/providers/google.rb', line 115

def models
  LLM::Google::Models.new(self)
end

#default_modelString

Returns the default model for chat completions

Returns:

  • (String)

See Also:



123
124
125
# File 'lib/llm/providers/google.rb', line 123

def default_model
  "gemini-2.5-flash"
end

#server_toolsString => LLM::ServerTool

Note:

This method includes certain tools that require configuration through a set of options that are easier to set through the LLM::Provider#server_tool method.

Returns:

See Also:



134
135
136
137
138
139
140
# File 'lib/llm/providers/google.rb', line 134

def server_tools
  {
    google_search: server_tool(:google_search),
    code_execution: server_tool(:code_execution),
    url_context: server_tool(:url_context)
  }
end

#web_search(query:) ⇒ LLM::Response

A convenience method for performing a web search using the Google Search tool.

Parameters:

  • query (String)

    The search query.

Returns:



147
148
149
# File 'lib/llm/providers/google.rb', line 147

def web_search(query:)
  ResponseAdapter.adapt(complete(query, tools: [server_tools[:google_search]]), type: :web_search)
end

#user_roleSymbol

Returns the providers user role

Returns:

  • (Symbol)

    Returns the providers user role



154
155
156
# File 'lib/llm/providers/google.rb', line 154

def user_role
  :user
end

#system_roleSymbol

Returns the providers system role

Returns:

  • (Symbol)

    Returns the providers system role



161
162
163
# File 'lib/llm/providers/google.rb', line 161

def system_role
  :user
end

#developer_roleSymbol

Returns the providers developer role

Returns:

  • (Symbol)


168
169
170
# File 'lib/llm/providers/google.rb', line 168

def developer_role
  :user
end