Class: LLM::Gemini

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

Overview

The Gemini class implements a provider for Gemini. The Gemini 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.gemini(key: ENV["KEY"])
ses = LLM::Session.new(llm)
ses.talk ["Tell me about this photo", ses.local_file("/images/photo.png")]
ses.messages.select(&:assistant?).each { print "[#{_1.role}]", _1.content, "\n" }

Defined Under Namespace

Classes: Audio, Files, Images, Models

Constant Summary collapse

HOST =
"generativelanguage.googleapis.com"

Instance Method Summary collapse

Methods inherited from Provider

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

Constructor Details

#initializeGemini

Returns a new instance of Gemini.

Parameters:

  • key (String, nil)

    The secret key for authentication



36
37
38
# File 'lib/llm/providers/gemini.rb', line 36

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"



165
166
167
# File 'lib/llm/providers/gemini.rb', line 165

def assistant_role
  "model"
end

#audioLLM::Gemini::Audio

Provides an interface to Gemini's audio API

Returns:

See Also:



82
83
84
# File 'lib/llm/providers/gemini.rb', line 82

def audio
  LLM::Gemini::Audio.new(self)
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:



68
69
70
71
72
73
74
75
76
# File 'lib/llm/providers/gemini.rb', line 68

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

#default_modelString

Returns the default model for chat completions

Returns:

  • (String)

See Also:



114
115
116
# File 'lib/llm/providers/gemini.rb', line 114

def default_model
  "gemini-2.5-flash"
end

#developer_roleSymbol

Returns the providers developer role

Returns:

  • (Symbol)


159
160
161
# File 'lib/llm/providers/gemini.rb', line 159

def developer_role
  :user
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:



47
48
49
50
51
52
53
54
55
56
# File 'lib/llm/providers/gemini.rb', line 47

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

#filesLLM::Gemini::Files

Provides an interface to Gemini's file management API

Returns:

See Also:



98
99
100
# File 'lib/llm/providers/gemini.rb', line 98

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

#imagessee LLM::Gemini::Images

Provides an interface to Gemini's image generation API

Returns:

See Also:



90
91
92
# File 'lib/llm/providers/gemini.rb', line 90

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

#modelsLLM::Gemini::Models

Provides an interface to Gemini's models API

Returns:

See Also:



106
107
108
# File 'lib/llm/providers/gemini.rb', line 106

def models
  LLM::Gemini::Models.new(self)
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:



125
126
127
128
129
130
131
# File 'lib/llm/providers/gemini.rb', line 125

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

#system_roleSymbol

Returns the providers system role

Returns:

  • (Symbol)

    Returns the providers system role



152
153
154
# File 'lib/llm/providers/gemini.rb', line 152

def system_role
  :user
end

#user_roleSymbol

Returns the providers user role

Returns:

  • (Symbol)

    Returns the providers user role



145
146
147
# File 'lib/llm/providers/gemini.rb', line 145

def user_role
  :user
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:



138
139
140
# File 'lib/llm/providers/gemini.rb', line 138

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