Class: LLM::DeepInfra::Images

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

Overview

The LLM::DeepInfra::Images class provides an interface for DeepInfra's images API. DeepInfra returns base64-encoded image data.

Examples:

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

llm = LLM.deepinfra(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::DeepInfra::Images

Parameters:



20
21
22
# File 'lib/llm/providers/deepinfra/images.rb', line 20

def initialize(provider)
  @provider = provider
end

Instance Method Details

#create(prompt:, model: "black-forest-labs/FLUX-2-klein-4b", size: "1024x1024", n: 1, response_format: "b64_json", quality: nil, style: nil) ⇒ LLM::Response<LLM::OpenAI::ResponseAdapter::Image>

Returns a response

Parameters:

  • prompt (String)

    A prompt

  • model (String) (defaults to: "black-forest-labs/FLUX-2-klein-4b")

    A text-to-image model. Defaults to the black-forest-labs/FLUX-2-klein-4b.

  • size (String) (defaults to: "1024x1024")

    Image size (eg 1024x1024)

  • n (Integer) (defaults to: 1)

    The number of images to default

  • response_format (String) (defaults to: "b64_json")

    No other options other than the default are supported.

  • quality (String) (defaults to: nil)

    Exists for compat. Noop.

  • style (String) (defaults to: nil)

    Exists for compat. Noop.

Returns:

See Also:



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

def create(prompt:, model: "black-forest-labs/FLUX-2-klein-4b", size: "1024x1024", n: 1, response_format: "b64_json", quality: nil, style: nil)
  req = LLM::Transport::Request.post(path("/images/generations"), headers)
  params = {prompt:, model:, size:, n:, response_format:, quality:, style:}.compact
  req.body = LLM.json.dump(params)
  res, span, tracer = execute(request: req, operation: "request")
  res = LLM::OpenAI::ResponseAdapter.adapt(res, type: :image)
  tracer.on_request_finish(operation: "request", model:, res:, span:)
  res
end

#edit(image:, prompt:, model: "black-forest-labs/FLUX-2-klein-4b", size: "1024x1024", n: 1, response_format: "b64_json", **params) ⇒ LLM::Response<LLM::OpenAI::ResponseAdapter::Image>

Returns a response

Parameters:

  • image (String, LLM::File, File)

    The image to edit.

  • prompt (String)

    A text description of the desired edits.

  • model (String) (defaults to: "black-forest-labs/FLUX-2-klein-4b")

    The model to use.

  • size (String) (defaults to: "1024x1024")

    Image size (eg 1024x1024)

  • n (Integer) (defaults to: 1)

    The number of images to generate.

  • response_format (String) (defaults to: "b64_json")

    DeepInfra currently supports b64_json.

  • params (Hash)

    Other parameters supported by DeepInfra, such as :mask or :user.

Returns:

See Also:



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/llm/providers/deepinfra/images.rb', line 71

def edit(image:, prompt:, model: "black-forest-labs/FLUX-2-klein-4b", size: "1024x1024", n: 1, response_format: "b64_json", **params)
  params = params.merge!(image: LLM.File(image), prompt:, model:, size:, n:, response_format:)
  params[:mask] = LLM.File(params[:mask]) if params[:mask]
  multi = LLM::Multipart.new(params)
  req = LLM::Transport::Request.post(path("/images/edits"), headers)
  req["content-type"] = multi.content_type
  transport.set_body_stream(req, multi.body)
  res, span, tracer = execute(request: req, operation: "request")
  res = LLM::OpenAI::ResponseAdapter.adapt(res, type: :image)
  tracer.on_request_finish(operation: "request", model:, res:, span:)
  res
end