Module: RubyLLM::Protocols::Mistral::Conversations::Images

Defined in:
lib/ruby_llm/protocols/mistral/conversations/images.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#images_urlObject



8
9
10
# File 'lib/ruby_llm/protocols/mistral/conversations/images.rb', line 8

def images_url(**)
  'conversations'
end

#parse_image_response(response, model:) ⇒ Object



23
24
25
# File 'lib/ruby_llm/protocols/mistral/conversations/images.rb', line 23

def parse_image_response(response, model:)
  parse_image_responses(response, model:).first
end

#parse_image_responses(response, model:) ⇒ Object

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby_llm/protocols/mistral/conversations/images.rb', line 27

def parse_image_responses(response, model:)
  data = response.body
  attachments = parse_conversation_content(data.fetch('outputs'))[:attachments]
  raise Error.new('Mistral returned no generated image', response:) if attachments.empty?

  usage = parse_conversation_usage(data['usage'] || {}).transform_keys(&:to_s)
  attachments.each_with_index.map do |attachment, index|
    bytes = @provider.download_file(attachment.provider_file_id)
    Image.new(data: Base64.strict_encode64(bytes), mime_type: RubyLLM::Files::MimeType.for(StringIO.new(bytes)),
              model: model, usage: index.zero? ? usage : {})
  end
end

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



12
13
14
15
16
17
18
19
20
21
# File 'lib/ruby_llm/protocols/mistral/conversations/images.rb', line 12

def render_image_payload(prompt, model:, size:, count: nil, with: nil, mask: nil, provider_options: {})
  if size || (count && count != 1)
    raise ArgumentError,
          'Mistral image generation does not accept size or count options'
  end
  raise UnsupportedAttachmentError, 'image editing' if with || mask

  payload = { model: model, store: false, inputs: prompt, tools: [{ type: 'image_generation' }] }
  Support::Utils.deep_merge(payload, provider_options)
end