Module: Legion::LLM::API::Namespaces::OpenAI::Images

Extended by:
Legion::Logging::Helper, Sinatra::Extension
Defined in:
lib/legion/llm/api/namespaces/openai/images.rb

Class Method Summary collapse

Class Method Details

.capable_provider_available?(capability) ⇒ Boolean

─── helpers ─────────────────────────────────────────────────────

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/legion/llm/api/namespaces/openai/images.rb', line 23

def self.capable_provider_available?(capability)
  instances = begin
    Legion::LLM::Call::Registry.all_instances
  rescue StandardError
    []
  end
  instances.any? do |entry|
    caps = entry[:capabilities] || entry['capabilities'] || []
    caps.include?(capability) || caps.map(&:to_sym).include?(capability)
  end
rescue StandardError => e
  Legion::Logging::Helper.log.warn(
    "[llm][api][openai][images] action=capability_check capability=#{capability} error=#{e.message}"
  )
  false
end

.extract_images_from_response(pipeline_response) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/legion/llm/api/namespaces/openai/images.rb', line 40

def self.extract_images_from_response(pipeline_response)
  raw_msg = pipeline_response.message
  msg = raw_msg.respond_to?(:transform_keys) ? raw_msg.transform_keys(&:to_sym) : {}
  images = msg[:images]
  return images if images.is_a?(Array) && !images.empty?

  # Fallback: wrap content string as b64_json if it looks like base64
  content = msg[:content].to_s
  return [] if content.empty?

  [{ b64_json: content }]
end

.format_images_response(pipeline_response, response_format: 'b64_json') ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/legion/llm/api/namespaces/openai/images.rb', line 53

def self.format_images_response(pipeline_response, response_format: 'b64_json')
  images = extract_images_from_response(pipeline_response)
  data = images.map do |img|
    i = img.respond_to?(:transform_keys) ? img.transform_keys(&:to_sym) : img
    if i[:b64_json]
      { b64_json: i[:b64_json] }
    elsif i[:url] && response_format == 'url'
      { url: i[:url] }
    else
      { b64_json: '' }
    end
  end

  { created: Time.now.to_i, data: data }
end

.normalize_image_param(image_param) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/legion/llm/api/namespaces/openai/images.rb', line 83

def self.normalize_image_param(image_param)
  # Rack multipart uploads arrive as hashes: { filename:, type:, tempfile: }
  if image_param.is_a?(Hash)
    tf = image_param[:tempfile] || image_param['tempfile']
    return Base64.strict_encode64(tf.read) if tf.respond_to?(:read)
  end

  image_param.to_s
end

.parse_media_body(request) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/legion/llm/api/namespaces/openai/images.rb', line 69

def self.parse_media_body(request)
  ct = request.content_type.to_s.downcase
  if ct.include?('multipart/form-data') || ct.include?('application/x-www-form-urlencoded')
    request.params.transform_keys(&:to_sym)
  else
    raw = request.body.read
    return {} if raw.nil? || raw.empty?

    Legion::JSON.load(raw)
  end
rescue StandardError
  {}
end