@mlx-node/vlm

OCR and document pipelines: PaddleOCR-VL, Qianfan OCR and PP-StructureV3.

Install

bash
npm install @mlx-node/vlm @mlx-node/lm

@mlx-node/lm is named because the document sample below imports ChatSession from it. The OCR sample needs only @mlx-node/vlm.

This package is for reading documents: OCR, layout and tables. To show a picture to a chat model, use images in @mlx-node/lm.

Get a model

The samples read model folders from disk. Each model below is a folder in MLX layout. There is no download command for these models on this site yet, so the folder names in the samples are examples: use the paths of your own folders. The package readme lists the models and their files.

Document models and the class that loads each
Model Class Use
PaddleOCR-VL VLModel OCR of a page or a region
Qianfan OCR QianfanOCRModel Questions about a document, over more than one turn
PP-DocLayoutV3 and PP-OCRv5 StructureV3Pipeline Layout, text lines and tables without a language model

Read a page

ts
import { readFile } from 'node:fs/promises';
import { homedir } from 'node:os';
import { join } from 'node:path';

import { VLModel } from '@mlx-node/vlm';

const ocr = await VLModel.load(join(homedir(), '.mlx-node', 'models', 'paddleocr-vl'));
console.log(await ocr.ocr(await readFile('page.png')));

Ask about a document

ts
import { readFile } from 'node:fs/promises';
import { homedir } from 'node:os';
import { join } from 'node:path';

import { ChatSession } from '@mlx-node/lm';
import { QianfanOCRModel } from '@mlx-node/vlm';

const models = join(homedir(), '.mlx-node', 'models');
const model = await QianfanOCRModel.load(join(models, 'qianfan-ocr'));
const session = new ChatSession(model);

const first = await session.send('Extract the text from this receipt.', {
  images: [await readFile('receipt.jpg')],
});
console.log(first.text);

const total = await session.send('What is the total price?');
console.log(total.text);

QianfanOCRModel works with ChatSession from @mlx-node/lm. loadSession() cannot load it, so build the session yourself as shown. A follow-up without an image reuses the cache of the first turn.

Layout and tables

StructureV3Pipeline.load(config) is not async. It needs four paths: layoutModelPath, textDetModelPath, textRecModelPath and dictPath. Then await pipeline.analyze(bytesOrPath) returns the elements of the page in reading order and the page as markdown.

For a PaddleOCR-VL answer, parseVlmOutput(text) turns the text into a parsed document, and documentToXlsx(doc) turns that document into an XLSX buffer.

Main exports

Name What it is
VLModel PaddleOCR-VL: load, ocr, chat
QianfanOCRModel Qianfan OCR, used through ChatSession
StructureV3Pipeline Layout detection plus OCR: load, analyze
parseVlmOutput(text) The text of a PaddleOCR-VL answer as a parsed document
documentToXlsx(doc) A parsed document as an XLSX buffer

Go deeper