Module: RubyLLM::Protocols::Cohere::OCR

Defined in:
lib/ruby_llm/protocols/cohere/ocr.rb

Overview

Cohere Parse accepts one document image and returns Markdown or blocks.

Class Method Summary collapse

Class Method Details

.ocr_urlObject



10
11
12
# File 'lib/ruby_llm/protocols/cohere/ocr.rb', line 10

def ocr_url
  'v2/parse'
end

.parse_ocr_block_text(block) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/ruby_llm/protocols/cohere/ocr.rb', line 53

def parse_ocr_block_text(block)
  case block['type']
  when 'text' then block.dig('text', 'content')
  when 'table' then block.dig('table', 'html')
  when 'image' then "![#{block.dig('image', 'description')}](#{block.dig('image', 'id')})"
  end
end

.parse_ocr_blocks(blocks) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/ruby_llm/protocols/cohere/ocr.rb', line 45

def parse_ocr_blocks(blocks)
  {
    'content' => blocks.filter_map { |block| parse_ocr_block_text(block) }.join("\n\n"),
    'images' => blocks.filter_map { |block| block['image'] },
    'tables' => blocks.filter_map { |block| block['table'] }
  }
end

.parse_ocr_page(page) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/ruby_llm/protocols/cohere/ocr.rb', line 37

def parse_ocr_page(page)
  content = page['markdown'] || parse_ocr_blocks(page.fetch('blocks'))
  RubyLLM::OCR::Page.new(
    index: page['index'], markdown: content['content'],
    images: content['images'], tables: content['tables'], raw: page
  )
end

.parse_ocr_response(response, model:) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/ruby_llm/protocols/cohere/ocr.rb', line 27

def parse_ocr_response(response, model:)
  data = response.body
  RubyLLM::OCR.new(
    pages: data.fetch('pages').map { |page| parse_ocr_page(page) },
    model: model,
    usage: data.dig('meta', 'billed_units'),
    raw: data
  )
end

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

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ruby_llm/protocols/cohere/ocr.rb', line 14

def render_ocr_payload(file, model:, pages: nil, provider_options: {})
  attachment = file.is_a?(Attachment) ? file : Attachment.new(file, config: @config)
  raise UnsupportedAttachmentError, attachment.mime_type unless attachment.image?
  raise ArgumentError, 'Cohere Parse accepts one image; pages must be [0]' unless pages.nil? || pages == [0]

  reference = attachment.url? ? attachment.source.to_s : attachment.for_llm
  {
    model: model,
    document: { type: 'image_url', image_url: reference },
    output_format: 'markdown'
  }.merge(provider_options)
end