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 aRAG 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 mergedmarkdown is your chunking input; split it however fits your content, then add records with your own id and metadata.
How OCR fallback works
extract()runsPDF.extract()first. If no pages need OCR, the result is returned as-is with an emptyocrPagesarray.- Otherwise, each page in
pagesNeedingOcris rendered to a PNG image and sent to a vision model on OpenRouter (qwen/qwen3-vl-235b-a22b-instruct) with a transcription prompt. - 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 viaocrPages.
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
Exceptions
RAGException is thrown when OCR fails. It carries a machine-readable key and the source path in its data.
PDF.extract() call (a PDFException, for example a missing file) propagate unchanged — see PDF exceptions.