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 Google's Imagen text-to-image models via the Imagen API: https://ai.google.dev/gemini-api/docs/imagen

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:



22
23
24
# File 'lib/llm/providers/gemini/images.rb', line 22

def initialize(provider)
  @provider = provider
end

Instance Method Details

#create(prompt:, n: 1, image_size: nil, aspect_ratio: nil, person_generation: nil, model: "imagen-4.0-generate-001", **params) ⇒ LLM::Response

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

  • n (Integer) (defaults to: 1)

    The number of images to generate

  • image_size (String) (defaults to: nil)

    The size of the image ("1K", "2K", etc.)

  • aspect_ratio (String) (defaults to: nil)

    The aspect ratio of the image ("1:1", "16:9", etc.)

  • person_generation (String) (defaults to: nil)

    Allow the model to generate images of people ("dont_allow", "allow_adult", "allow_all")

  • model (String) (defaults to: "imagen-4.0-generate-001")

    The model to use

  • params (Hash)

    Other parameters (see Imagen docs)

Returns:

See Also:



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

def create(prompt:, n: 1, image_size: nil, aspect_ratio: nil, person_generation: nil, model: "imagen-4.0-generate-001", **params)
  req  = Net::HTTP::Post.new("/v1beta/models/#{model}:predict?key=#{key}", headers)
  body = LLM.json.dump({
    parameters: {
      sampleCount: n,
      imageSize: image_size,
      aspectRatio: aspect_ratio,
      personGeneration: person_generation
    }.compact.merge!(params),
    instances: [{prompt:}]
  })
  req.body = body
  res = execute(request: req)
  ResponseAdapter.adapt(res, type: :image)
end

#create_variationObject

Raises:

  • (NotImplementedError)

    This method is not implemented by Gemini



78
79
80
# File 'lib/llm/providers/gemini/images.rb', line 78

def create_variation
  raise NotImplementedError
end

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

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:

Raises:

  • (NotImplementedError)

See Also:



71
72
73
# File 'lib/llm/providers/gemini/images.rb', line 71

def edit(image:, prompt:, model: "gemini-2.5-flash-image", **params)
  raise NotImplementedError, "image editing is not yet supported by Gemini"
end