Class: LLM::Gemini::Images

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/providers/gemini/images.rb

Overview

The LLM::Gemini::Images class provides an images object for interacting with Gemini’s images API. Please note that unlike OpenAI, which can return either URLs or base64-encoded strings, Gemini’s images API will always return an image as a base64 encoded string that can be decoded into binary.

Examples:

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

llm = LLM.gemini(key: ENV["KEY"])
res = llm.images.create prompt: "A dog on a rocket to the moon"
IO.copy_stream res.images[0], "rocket.png"

Instance Method Summary collapse

Constructor Details

#initialize(provider) ⇒ LLM::Gemini::Responses

Returns a new Images object

Parameters:



25
26
27
# File 'lib/llm/providers/gemini/images.rb', line 25

def initialize(provider)
  @provider = provider
end

Instance Method Details

#create(prompt:, model: "gemini-2.0-flash-exp-image-generation", **params) ⇒ LLM::Response

Note:

The prompt should make it clear you want to generate an image, or you might unexpectedly receive a purely textual response. This is due to how Gemini implements image generation under the hood.

Create an image

Examples:

llm = LLM.gemini(key: ENV["KEY"])
res = llm.images.create prompt: "A dog on a rocket to the moon"
IO.copy_stream res.images[0], "rocket.png"

Parameters:

  • prompt (String)

    The prompt

  • params (Hash)

    Other parameters (see Gemini docs)

Returns:

See Also:



44
45
46
47
48
49
50
51
52
53
# File 'lib/llm/providers/gemini/images.rb', line 44

def create(prompt:, model: "gemini-2.0-flash-exp-image-generation", **params)
  req  = Net::HTTP::Post.new("/v1beta/models/#{model}:generateContent?key=#{key}", headers)
  body = JSON.dump({
    contents: [{parts: [{text: create_prompt}, {text: prompt}]}],
    generationConfig: {responseModalities: ["TEXT", "IMAGE"]}
  }.merge!(params))
  req.body = body
  res = execute(request: req)
  LLM::Response.new(res).extend(LLM::Gemini::Response::Image)
end

#edit(image:, prompt:, model: "gemini-2.0-flash-exp-image-generation", **params) ⇒ LLM::Response

Note:

The prompt should make it clear you want to generate an image, or you might unexpectedly receive a purely textual response. This is due to how Gemini implements image generation under the hood.

Edit an image

Examples:

llm = LLM.gemini(key: ENV["KEY"])
res = llm.images.edit image: "cat.png", prompt: "Add a hat to the cat"
IO.copy_stream res.images[0], "hatoncat.png"

Parameters:

  • image (String, LLM::File)

    The image to edit

  • prompt (String)

    The prompt

  • params (Hash)

    Other parameters (see Gemini docs)

Returns:

See Also:



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

def edit(image:, prompt:, model: "gemini-2.0-flash-exp-image-generation", **params)
  req   = Net::HTTP::Post.new("/v1beta/models/#{model}:generateContent?key=#{key}", headers)
  image = LLM.File(image)
  body  = JSON.dump({
    contents: [{parts: [{text: edit_prompt}, {text: prompt}, format.format_content(image)]}],
    generationConfig: {responseModalities: ["TEXT", "IMAGE"]}
  }.merge!(params)).b
  set_body_stream(req, StringIO.new(body))
  res = execute(request: req)
  LLM::Response.new(res).extend(LLM::Gemini::Response::Image)
end

#create_variationObject

Raises:

  • (NotImplementedError)

    This method is not implemented by Gemini



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

def create_variation
  raise NotImplementedError
end