Module: RubyLLM::Providers::OpenRouter::Images

Included in:
ChatCompletions
Defined in:
lib/ruby_llm/providers/openrouter/images.rb

Overview

Image generation methods for the OpenRouter API integration. OpenRouter has a unified images endpoint that generates and edits images across providers and reports the exact cost of each call.

Class Method Summary collapse

Class Method Details

.build_input_references(with) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/ruby_llm/providers/openrouter/images.rb', line 29

def build_input_references(with)
  Attachment.wrap(with, config: @config).map do |attachment|
    raise UnsupportedAttachmentError, attachment.mime_type unless attachment.image?

    Protocols::ChatCompletions::Media.format_image(attachment)
  end
end

.image_usage(usage) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/ruby_llm/providers/openrouter/images.rb', line 60

def image_usage(usage)
  {
    'input_tokens' => usage['prompt_tokens'],
    'output_tokens' => usage['completion_tokens'],
    'cost' => reported_cost(usage)
  }.compact
end

.images_url(with: nil, mask: nil) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



12
13
14
# File 'lib/ruby_llm/providers/openrouter/images.rb', line 12

def images_url(with: nil, mask: nil) # rubocop:disable Lint/UnusedMethodArgument
  'images'
end

.parse_image_response(response, model:) ⇒ Object

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ruby_llm/providers/openrouter/images.rb', line 46

def parse_image_response(response, model:)
  data = response.body
  image_data = Array(data['data']).first

  raise Error, 'Unexpected response format from OpenRouter image API' unless image_data

  Image.new(
    data: image_data['b64_json'],
    mime_type: image_data['media_type'] || 'image/png',
    model: model,
    usage: image_usage(data['usage'] || {})
  )
end

.parse_image_responses(response, model:) ⇒ Object

OpenRouter's images endpoint returns one image per request.



42
43
44
# File 'lib/ruby_llm/providers/openrouter/images.rb', line 42

def parse_image_responses(response, model:)
  [parse_image_response(response, model:)]
end

.render_image_payload(prompt, model:, size:, count: nil, with: nil, mask: nil, provider_options: {}) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruby_llm/providers/openrouter/images.rb', line 16

def render_image_payload(prompt, model:, size:, count: nil, with: nil, mask: nil, provider_options: {}) # rubocop:disable Lint/UnusedMethodArgument
  RubyLLM.logger.debug { "Ignoring size #{size}. Use aspect_ratio/resolution provider options instead." }
  if count && count > 1
    RubyLLM.logger.debug do
      "Ignoring count #{count}. OpenRouter generates one image per request."
    end
  end
  payload = { model: model, prompt: prompt }
  references = build_input_references(with)
  payload[:input_references] = references if references.any?
  payload.merge(provider_options)
end

.validate_paint_inputs!(with:, mask:) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



37
38
39
# File 'lib/ruby_llm/providers/openrouter/images.rb', line 37

def validate_paint_inputs!(with:, mask:) # rubocop:disable Lint/UnusedMethodArgument
  raise UnsupportedAttachmentError, 'image mask' unless mask.nil?
end