Class: LLM::Gemini

Inherits:
Provider show all
Includes:
Format
Defined in:
lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini.rb,
lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/audio.rb,
lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/files.rb,
lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/format.rb,
lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/images.rb,
lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/models.rb,
lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/error_handler.rb,
lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/stream_parser.rb

Overview

The Gemini class implements a provider for [Gemini](ai.google.dev/). 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"])
bot = LLM::Bot.new(llm)
bot.chat ["Tell me about this photo", File.open("/images/horse.jpg", "rb")]
bot.messages.select(&:assistant?).each { print "[#{_1.role}]", _1.content, "\n" }

Defined Under Namespace

Modules: Format, Response Classes: Audio, ErrorHandler, Files, Images, Models, StreamParser

Constant Summary collapse

HOST =
"generativelanguage.googleapis.com"

Instance Method Summary collapse

Methods included from Format

#format

Methods inherited from Provider

#chat, clients, #inspect, #moderations, #respond, #responses, #schema, #server_tool, #vector_stores, #with

Constructor Details

#initializeGemini



38
39
40
# File 'lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini.rb', line 38

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

Instance Method Details

#assistant_roleString



120
121
122
# File 'lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini.rb', line 120

def assistant_role
  "model"
end

#audioLLM::Gemini::Audio

Provides an interface to Gemini’s audio API

See Also:



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

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.choices[0].role}]", res.choices[0].content, "\n"

Raises:

See Also:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini.rb', line 68

def complete(prompt, params = {})
  params = {role: :user, model: default_model}.merge!(params)
  tools  = resolve_tools(params.delete(:tools))
  params = [params, format_schema(params), format_tools(tools)].inject({}, &:merge!).compact
  role, model, stream = [:role, :model, :stream].map { params.delete(_1) }
  action = stream ? "streamGenerateContent?key=#{@key}&alt=sse" : "generateContent?key=#{@key}"
  model.respond_to?(:id) ? model.id : model
  path = ["/v1beta/models/#{model}", action].join(":")
  req  = Net::HTTP::Post.new(path, headers)
  messages = [*(params.delete(:messages) || []), LLM::Message.new(role, prompt)]
  body = JSON.dump({contents: format(messages)}.merge!(params))
  set_body_stream(req, StringIO.new(body))
  res = execute(request: req, stream:)
  LLM::Response.new(res)
    .extend(LLM::Gemini::Response::Completion)
    .extend(Module.new { define_method(:__tools__) { tools } })
end

#default_modelString

Returns the default model for chat completions

See Also:



128
129
130
# File 'lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini.rb', line 128

def default_model
  "gemini-2.5-flash"
end

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

Provides an embedding



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

def embed(input, model: "text-embedding-004", **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 = JSON.dump({content: {parts: [{text: input}]}})
  res = execute(request: req)
  LLM::Response.new(res).extend(LLM::Gemini::Response::Embedding)
end

#filesLLM::Gemini::Files

Provides an interface to Gemini’s file management API

See Also:



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

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

#imagessee LLM::Gemini::Images

Provides an interface to Gemini’s image generation API

See Also:



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

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

#modelsLLM::Gemini::Models

Provides an interface to Gemini’s models API

See Also:



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

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.

See Also:



139
140
141
142
143
144
145
# File 'lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini.rb', line 139

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.



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

def web_search(query:)
  complete(query, tools: [server_tools[:google_search]])
    .extend(LLM::Gemini::Response::WebSearch)
end