Module: RubyLLM::Providers::Mistral::OCR

Included in:
ChatCompletions
Defined in:
lib/ruby_llm/providers/mistral/ocr.rb

Overview

Document OCR methods for the Mistral API. Remote files are passed as URLs; local files are inlined as base64 data URIs. Images go through the image_url document variant, everything else through document_url.

Class Method Summary collapse

Class Method Details

.ocr_data_uri(attachment) ⇒ Object



34
35
36
# File 'lib/ruby_llm/providers/mistral/ocr.rb', line 34

def ocr_data_uri(attachment)
  "data:#{attachment.mime_type};base64,#{attachment.encoded}"
end

.ocr_document_part(attachment) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/ruby_llm/providers/mistral/ocr.rb', line 24

def ocr_document_part(attachment)
  reference = attachment.url? ? attachment.source.to_s : ocr_data_uri(attachment)

  if attachment.image?
    { type: 'image_url', image_url: reference }
  else
    { type: 'document_url', document_url: reference }
  end
end

.ocr_urlObject



12
13
14
# File 'lib/ruby_llm/providers/mistral/ocr.rb', line 12

def ocr_url
  'ocr'
end

.parse_ocr_response(response, model:) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby_llm/providers/mistral/ocr.rb', line 38

def parse_ocr_response(response, model:)
  data = response.body

  RubyLLM::OCR.new(
    pages: data['pages'],
    model: data['model'] || model,
    usage: data['usage_info'],
    raw: data
  )
end

.render_ocr_payload(file, model:, pages: nil, provider_options: {}) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/ruby_llm/providers/mistral/ocr.rb', line 16

def render_ocr_payload(file, model:, pages: nil, provider_options: {})
  attachment = file.is_a?(Attachment) ? file : Attachment.new(file, config: @config)

  payload = { model: model, document: ocr_document_part(attachment) }
  payload[:pages] = pages if pages
  payload.merge(provider_options)
end