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
- .ocr_url ⇒ Object
- .parse_ocr_block_text(block) ⇒ Object
- .parse_ocr_blocks(blocks) ⇒ Object
- .parse_ocr_page(page) ⇒ Object
- .parse_ocr_response(response, model:) ⇒ Object
- .render_ocr_payload(file, model:, pages: nil, provider_options: {}) ⇒ Object
Class Method Details
.ocr_url ⇒ Object
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 "})" 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
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: {}) = file.is_a?(Attachment) ? file : Attachment.new(file, config: @config) raise UnsupportedAttachmentError, .mime_type unless .image? raise ArgumentError, 'Cohere Parse accepts one image; pages must be [0]' unless pages.nil? || pages == [0] reference = .url? ? .source.to_s : .for_llm { model: model, document: { type: 'image_url', image_url: reference }, output_format: 'markdown' }.merge() end |