Skip to main content
The RAG class turns a PDF into Markdown ready to chunk and embed into a vector table. It wraps @talosjs/pdf for classification and text extraction, then automatically OCRs any page the extractor flags as needing it, using a vision model over OpenRouter.

Extracting a PDF

Create a RAG instance with the path to a source file, then call extract().
RAG accepts the same PDFExtractOptionsType as PDF.extract(), so you can restrict extraction to specific 0-indexed pages:

Feeding the result into a table

The merged markdown is your chunking input; split it however fits your content, then add records with your own id and metadata.

How OCR fallback works

  1. extract() runs PDF.extract() first. If no pages need OCR, the result is returned as-is with an empty ocrPages array.
  2. Otherwise, each page in pagesNeedingOcr is rendered to a PNG image and sent to a vision model on OpenRouter (qwen/qwen3-vl-235b-a22b-instruct) with a transcription prompt.
  3. Every OCR’d page’s Markdown is appended to the extracted markdown, each preceded by an HTML comment marking its page number (<!-- page N -->), and returned in ascending page order via ocrPages.
OCR only runs for the pages @talosjs/pdf flags in pagesNeedingOcr — scanned or image-based pages, or pages with extraction issues. Fully text-based PDFs never touch OpenRouter.

Options

The API key is only required when a page actually needs OCR. Set it in the environment so you don’t have to pass it explicitly:

Exceptions

RAGException is thrown when OCR fails. It carries a machine-readable key and the source path in its data.
Errors from the underlying PDF.extract() call (a PDFException, for example a missing file) propagate unchanged — see PDF exceptions.