Skip to content
Projects

About

Run a small Chinese chat model in a PDF file

AI & Machine Learning

How these counts are measured

Measured star growth

in 24h
0
in 7 days
Collecting history
in 15 days
Collecting history
in 1 month
Collecting history

Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.

README.md

Original

llmpdf

中文 README

A proof-of-concept that embeds a small Chinese chat model directly into a PDF file and runs it with the PDF viewer's JavaScript engine.

The current v0.3.0 release uses a 27.26M-parameter decoder-only transformer, quantized to a custom Q8 format, plus an embedded deterministic Chinese Wikipedia knowledge base. The model, tokenizer, knowledge cards, inference runtime, and chat UI are all contained in one PDF. Large artifacts are distributed as GitHub Release assets rather than tracked in Git history:

llmpdf.pdf              29.10 MiB
tinygpt27_final.tgq     27.77 MiB

PDFium smoke test

What this is

  • a pure-JavaScript transformer inference runtime;
  • a compact TGQ8 model format with an embedded byte-level BPE tokenizer;
  • a UTF-16BE JavaScript payload embedding technique for PDF /JS streams;
  • a PDF AcroForm chat UI with live decode-speed readout that works around several PDFium focus/event limitations;
  • an experimental embedded Chinese Wikipedia knowledge base with deterministic local retrieval;
  • training and evaluation scripts for reproducing the model.

This is not a general-purpose ChatGPT replacement. It is a narrow-domain toy model and an engineering demonstration of how far PDF JavaScript can be pushed.

Quick start

Open the PDF demo

Download the release assets first:

./scripts/download_artifacts.sh

Then open this file in Firefox's built-in PDF viewer (the best performer in current manual testing); Chromium or Edge also work:

dist/llmpdf.pdf

You can also download llmpdf.pdf directly from GitHub Releases.

Notes:

  • The PDF contains about 36.6 MiB of UTF-16BE JavaScript source. Chromium PDFium may take tens of seconds to parse it before JS ready appears.
  • Firefox's built-in PDF viewer produced the fastest generation in manual testing. Chromium PDFium works, but PDF JavaScript runs in jitless mode and therefore emits tokens noticeably more slowly.
  • The first generation is slower because the payload is decoded and the model is initialized.
  • Generation is intentionally incremental so the viewer remains mostly responsive.

Run the model in Node.js

No npm dependencies are required:

node eval/test_tinygpt.js dist/tinygpt27_final.tgq '黑洞是什么' 80 0

Batch evaluation:

node eval/eval_tgq.js dist/tinygpt27_final.tgq /tmp/eval.json

Rebuild the PDF

If you have not downloaded the TGQ artifact yet, run ./scripts/download_artifacts.sh first.

Install Python dependencies, preferably in a virtual environment:

python3 -m venv .venv
. .venv/bin/activate
pip install -r requirements.txt

Training and data preparation additionally require requirements-training.txt; those dependencies are not needed just to rebuild the PDF.

Then rebuild:

make build-pdf

Equivalent manual commands:

python3 pdf/make_payload_js.py dist/tinygpt27_final.tgq dist/tgq_payload_utf16_27m.js --mode utf16be --kb-input kb/kb_zhwiki_vital.jsonl
python3 pdf/build_pdf_bundle.py dist/tgq_payload_utf16_27m.js dist/tinygpt27_pdf_stream.bin
python3 pdf/make_chat_pdf.py dist/tinygpt27_pdf_stream.bin dist/llmpdf_rebuilt.pdf

Deploy the demo page

The repository root includes a static index.html and vercel.json. To deploy the same style of landing page as linux.doompdf.dev, import this repository into Vercel as a static/Other project. During deployment, scripts/build_vercel.sh downloads the release PDF and writes the static output to public/. The landing page is served at /, and the PDF is available at /llmpdf.pdf through a rewrite to dist/llmpdf.pdf.

Embedded knowledge-base demo

v0.3.0 is an experimental retrieval-augmented PDF release. It embeds 3,943 compact knowledge cards derived from the Chinese Wikipedia vital-article lists: the global Level-3 list plus the Chinese-domain people, history, geography, and culture lists. The cards are stored in kb/kb_zhwiki_vital.jsonl and embedded in the same compressed PDF JavaScript payload as the model.

Retrieval is deterministic and local:

  • entity/alias matching and query-aware fact extraction run in PDF JavaScript;
  • a knowledge-base hit is answered directly from the embedded card instead of asking the 27M model to memorize the fact;
  • a miss falls back to the normal 27M chat model;
  • no network access is used at runtime.

This deliberately favors factual reliability over fluent synthesis. In Node.js evaluation, the current retriever passes 23/23 representative world-knowledge prompts and correctly declines all 14 non-knowledge, how-to, arithmetic, code, translation, and multi-step reasoning prompts (eval/eval_kb.js). The knowledge-enabled PDF is about 29.10 MiB, only about 1.2 MiB larger than the model-only PDF because the /JS stream is Flate-compressed.

The knowledge text is reproduced directly rather than merely learned by the model. Chinese Wikipedia text is CC BY-SA, so the generated PDF includes a visible attribution line and redistribution must preserve the applicable attribution/share-alike obligations. See NOTICE.md.

To rebuild the knowledge cards from the MediaWiki API, install requirements-training.txt and run:

python3 kb/build_vital_wiki_kb.py kb/kb_zhwiki_vital.jsonl --refresh

The script caches large API extracts in kb/vital_extract_cache.json; that cache is intentionally not tracked in Git.

Verified compatibility

Viewer Status
Firefox built-in PDF viewer / pdf.js Verified manually on Linux; best observed performance
Chromium PDF viewer / PDFium Verified on Linux; works, but slower because of jitless execution
Microsoft Edge PDF viewer Expected to work; not fully verified
Generic/web pdf.js outside Firefox Not verified
Adobe Acrobat Not verified
Mobile PDF viewers Generally unsupported

Firefox's built-in PDF viewer is currently recommended based on manual testing. Chromium PDFium remains the reference implementation for the form focus/event workaround because that mechanism was originally developed against PDFium behavior.

Model quality

The current 27M model is substantially better than both the earlier 15M experiment and the first non-distilled 27M release on narrow Chinese QA. In the included evaluation it correctly answers prompts such as:

  • 为什么会下雨
  • 黑洞是什么 / 什么是黑洞 / 黑洞是怎么形成的
  • 地球为什么会有四季
  • 什么是通货膨胀
  • 什么是USDT
  • 什么是光合作用
  • 用 Python 怎么反转一个列表
  • 9乘以7等于多少
  • simple logic, translation, leave-message, and health-advice prompts

See eval/eval_runtime_27m.json.

Known limitations:

  • open-domain factual hallucination remains likely;
  • long answers may degrade or truncate;
  • math, complex code, and multi-step reasoning are unreliable;
  • generation speed depends heavily on the viewer; Chromium PDFium is slow because of jitless JavaScript execution, while Firefox's built-in viewer performed best in manual testing.

Repository layout

runtime/       handwritten pure-JS TGQ8 runtime, KB retriever, and PDF glue
pdf/           payload encoder, JS-stream bundler, PDF form builder
export/        PyTorch checkpoint to TGQ8 exporter
training/      model, tokenizer, data preparation, training, evaluation scripts
eval/          Node.js smoke tests, benchmark, batch evaluation, KB retrieval evaluation
kb/            compact Chinese Wikipedia knowledge cards and builders
tokenizer/     byte-level BPE tokenizer used by the released model
docs/          PDFium notes, TGQ8 format, training notes
dist/          release-staging copy of the demo PDF and TGQ model

Training

The model was first trained from scratch on Chinese instruction/conversation data and Wikipedia-derived knowledge QA. The current release then continued training with sequence-level distillation data generated by Gemma 4 E2B-it, mixed with license-clean procedural arithmetic/logic data, curated project QA, and a small amount of replay data. See docs/training.md for details and licensing caveats.

The released TGQ uses:

vocab      6374
d_model    480
layers     10
heads      8 query heads / 2 KV heads
hidden     1280
seq len    256
parameters 27.26M

License and acceptable use

  • Source code: MIT license; see LICENSE.
  • Model weights and generated PDF: research/demo artifacts with significant upstream data restrictions. Do not treat them as permissively licensed or commercially usable without a legal review. See NOTICE.md and MODEL_CARD.md.

In particular, parts of the training mixture derive from datasets that are non-commercial, research-only, GPL-licensed, unclearly licensed, or CC BY-SA. Experimental knowledge-enabled PDFs additionally reproduce Chinese Wikipedia text directly under CC BY-SA. The conservative interpretation is that the released model and PDF are for non-commercial research and demonstration only.

SHA256

600c8353a26fb05a5bc3a6742fb8c6ff439be25f1708d3523ebbc9a633ee337a  dist/llmpdf.pdf
77c6e564745645b0e99973ccc91a8c56919e6780a4ed85fa37b45298a819c8b3  dist/tinygpt27_final.tgq

Explore related projects

Loading…