Module: RubyLLM::Agents::ImageGenerator::Templates

Defined in:
lib/ruby_llm/agents/image/generator/templates.rb

Overview

Prompt template support for image generators

Allows defining reusable prompt templates that wrap user input with consistent styling, quality, or context instructions.

Examples:

Using templates in a generator

class ProductPhotoGenerator < RubyLLM::Agents::ImageGenerator
  model "gpt-image-1"
  template "Professional product photography of {prompt}, " \
           "white background, studio lighting, 8k resolution"
end

result = ProductPhotoGenerator.call(prompt: "a red sneaker")
# Actual prompt: "Professional product photography of a red sneaker, ..."

Template with multiple placeholders

class StyleTransferGenerator < RubyLLM::Agents::ImageGenerator
  def build_prompt
    Templates.apply(
      "{prompt} in the style of {style}, detailed, high quality",
      prompt: @prompt,
      style: options[:style] || "impressionism"
    )
  end
end

Constant Summary collapse

PRESETS =

Common prompt templates for different use cases

{
  # Photography styles
  product: "Professional product photography of {prompt}, " \
           "white background, studio lighting, high resolution, commercial quality",

  portrait: "Professional portrait of {prompt}, " \
            "soft lighting, shallow depth of field, 85mm lens, studio quality",

  landscape: "Stunning landscape photograph of {prompt}, " \
             "golden hour lighting, dramatic sky, high dynamic range",

  # Artistic styles
  watercolor: "Watercolor painting of {prompt}, " \
              "soft brushstrokes, muted colors, artistic, on textured paper",

  oil_painting: "Oil painting of {prompt}, " \
                "rich colors, visible brushwork, classical style, museum quality",

  digital_art: "Digital art of {prompt}, " \
               "vibrant colors, detailed, trending on artstation, 4k",

  anime: "Anime style illustration of {prompt}, " \
         "detailed, Studio Ghibli inspired, beautiful lighting",

  # Technical styles
  isometric: "Isometric 3D render of {prompt}, " \
             "clean lines, bright colors, game asset style",

  blueprint: "Technical blueprint of {prompt}, " \
             "detailed engineering drawing, white lines on blue background",

  wireframe: "3D wireframe render of {prompt}, " \
             "clean geometric lines, technical visualization",

  # UI/Design
  icon: "App icon design of {prompt}, " \
        "flat design, bold colors, minimal, iOS style, high resolution",

  logo: "Minimalist logo design for {prompt}, " \
        "clean lines, professional, vector style, brand identity",

  ui_mockup: "Modern UI mockup of {prompt}, " \
             "clean design, shadows, glass morphism, Figma style"
}.freeze

Class Method Summary collapse

Class Method Details

.apply(template, **vars) ⇒ String

Apply a template to a prompt with variable substitution

Parameters:

  • template (String)

    Template string with placeholder syntax

  • vars (Hash)

    Variables to substitute

Returns:

  • (String)

    The rendered template



85
86
87
88
89
90
91
# File 'lib/ruby_llm/agents/image/generator/templates.rb', line 85

def apply(template, **vars)
  result = template.dup
  vars.each do |key, value|
    result.gsub!("{#{key}}", value.to_s)
  end
  result
end

.apply_preset(name, prompt) ⇒ String

Apply a preset template to a prompt

Parameters:

  • name (Symbol)

    Preset name

  • prompt (String)

    User prompt

Returns:

  • (String)

    Rendered template

Raises:

  • (ArgumentError)

    If preset not found



114
115
116
117
118
119
# File 'lib/ruby_llm/agents/image/generator/templates.rb', line 114

def apply_preset(name, prompt)
  template = preset(name)
  raise ArgumentError, "Unknown template preset: #{name}" unless template

  apply(template, prompt: prompt)
end

.preset(name) ⇒ String?

Get a preset template by name

Parameters:

  • name (Symbol)

    Preset name

Returns:

  • (String, nil)

    The template string or nil



97
98
99
# File 'lib/ruby_llm/agents/image/generator/templates.rb', line 97

def preset(name)
  PRESETS[name.to_sym]
end

.preset_namesArray<Symbol>

List all available preset names

Returns:

  • (Array<Symbol>)

    Preset names



104
105
106
# File 'lib/ruby_llm/agents/image/generator/templates.rb', line 104

def preset_names
  PRESETS.keys
end