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/format.rb,
lib/llm/providers/gemini/images.rb,
lib/llm/providers/gemini/error_handler.rb,
lib/llm/providers/gemini/response_parser.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(ENV["KEY"])
bot = LLM::Chat.new(llm).lazy
bot.chat LLM::File("/images/capybara.png")
bot.chat "Describe the image"
bot.messages.select(&:assistant?).each { print "[#{_1.role}]", _1.content, "\n" }
#!/usr/bin/env ruby
require "llm"

llm = LLM.gemini(ENV["KEY"])
bot = LLM::Chat.new(llm).lazy
bot.chat ["Describe the image", LLM::File("/images/capybara.png")]
bot.messages.select(&:assistant?).each { print "[#{_1.role}]", _1.content, "\n" }

Defined Under Namespace

Classes: Audio, Files, Images

Constant Summary collapse

HOST =
"generativelanguage.googleapis.com"

Instance Method Summary collapse

Methods inherited from Provider

#chat, #chat!, #inspect, #respond, #respond!, #responses

Constructor Details

#initialize(secret) ⇒ Gemini

Returns a new instance of Gemini.

Parameters:

  • secret (String)

    The secret key for authentication



43
44
45
# File 'lib/llm/providers/gemini.rb', line 43

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

Instance Method Details

#modelsHash<String, LLM::Model>

Returns a hash of available models

Returns:

  • (Hash<String, LLM::Model>)

    Returns a hash of available models



112
113
114
# File 'lib/llm/providers/gemini.rb', line 112

def models
  @models ||= load_models!("gemini")
end

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

Provides an embedding

Parameters:

  • input (String, Array<String>)

    The input to embed

  • model (String) (defaults to: "text-embedding-004")

    The embedding model to use

  • params (Hash)

    Other embedding parameters

Returns:

Raises:



54
55
56
57
58
59
60
# File 'lib/llm/providers/gemini.rb', line 54

def embed(input, model: "text-embedding-004", **params)
  path = ["/v1beta/models/#{model}", "embedContent?key=#{@secret}"].join(":")
  req = Net::HTTP::Post.new(path, headers)
  req.body = JSON.dump({content: {parts: [{text: input}]}})
  res = request(@http, req)
  Response::Embedding.new(res).extend(response_parser)
end

#complete(prompt, role = :user, model: "gemini-1.5-flash", **params) ⇒ LLM::Response::Completion

Provides an interface to the chat completions API

Examples:

llm = LLM.openai(ENV["KEY"])
messages = [
  {role: "system", content: "Your task is to answer all of my questions"},
  {role: "system", content: "Your answers should be short and concise"},
]
res = llm.complete("Hello. What is the answer to 5 + 2 ?", :user, messages:)
print "[#{res.choices[0].role}]", res.choices[0].content, "\n"

Parameters:

  • prompt (String)

    The input prompt to be completed

  • role (Symbol) (defaults to: :user)

    The role of the prompt (e.g. :user, :system)

  • model (String) (defaults to: "gemini-1.5-flash")

    The model to use for the completion

  • params (Hash)

    Other completion parameters

Returns:

Raises:

See Also:



72
73
74
75
76
77
78
79
80
# File 'lib/llm/providers/gemini.rb', line 72

def complete(prompt, role = :user, model: "gemini-1.5-flash", **params)
  path = ["/v1beta/models/#{model}", "generateContent?key=#{@secret}"].join(":")
  req  = Net::HTTP::Post.new(path, headers)
  messages = [*(params.delete(:messages) || []), LLM::Message.new(role, prompt)]
  body = JSON.dump({contents: format(messages)}).b
  req.body_stream = StringIO.new(body)
  res = request(@http, req)
  Response::Completion.new(res).extend(response_parser)
end

#audioObject

Provides an interface to Gemini’s audio API

See Also:



85
86
87
# File 'lib/llm/providers/gemini.rb', line 85

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

#imagessee LLM::Gemini::Images

Provides an interface to Gemini’s image generation API

Returns:

See Also:



93
94
95
# File 'lib/llm/providers/gemini.rb', line 93

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

#filesObject

Provides an interface to Gemini’s file management API

See Also:



100
101
102
# File 'lib/llm/providers/gemini.rb', line 100

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

#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”



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

def assistant_role
  "model"
end