๐ Try the demo: https://github.com/LM-Kit/lm-kit-net-samples/tree/main/console_net/document-intelligence/document-parsing/parsed_document_rag
Q&A over Parsed Documents with Page Citations for C# .NET Applications
๐ฏ Purpose of the Demo
Parsed Document RAG answers questions over a folder of documents parsed by
DocumentParser. The parser keeps what flat text extraction loses (the columns of a table,
the numbers behind a chart, the heading a passage belongs to), each page is indexed as its own
RAG section, and every answer lists the pages it came from.
The sample shows how to:
- Parse documents once and store each parse with
ToJson; reload it withParsedDocument.FromJson. - Index each page's body with
MarkdownChunkingand page metadata. - Leave running heads and feet out of the index (
ParsedPage.Body). - Release the parser before the chat model loads, so the GPU holds one of them at a time.
๐ฅ Target Audience
- Knowledge base builders indexing reports, manuals and filings.
- Compliance and audit teams who need every answer traced to a page.
- Developers moving a RAG prototype from flat text to structured documents.
๐ Problem Solved
- Tables and charts retrievable. They are indexed as tables and data, not as a stream of numbers.
- Citations. Each passage carries its document and page.
- Parse once. A stored parse re-indexes without the parser's models.
๐ป Demo Application Overview
Enter a folder, then choose a chat model. Documents without a stored parse are parsed at
Medium effort; every page is indexed; then ask questions. Each answer is followed by its
sources.
โจ Key Features
- One RAG section per page, identified as
<document> p.<page>. sourceandpagemetadata on every chunk.- Stored parses (
<file>.parsed.json) reused on the next run. - Eight chat models to choose from, or any model ID or URI.
Example Output
Question (empty to quit): On which benchmarks is the trace-based JIT the fastest, and by how much?
Answer: The trace-based JIT is the fastest on 9 of the 26 benchmarks: 3d-morph, bitops-3bit-bits-in-byte, bitsops-bitwise-and, crypto-shal, math-cordic, math-partial-sums, math-spectral-norm, string-base64, and string-validate-input.
For one of the benchmark programs, it achieved up to a 25x speedup on bitops-bitwise-and compared to the SpiderMonkey interpreter, and was almost 5 times faster than V8 and SFX.
Sources:
paper.pdf p.11
paper.pdf p.10
paper.pdf p.12
paper.pdf p.1
๐๏ธ Architecture
folder โโบ DocumentParser(Medium) โโบ ParsedDocument โโบ <file>.parsed.json
โ
page.Body โโบ ToMarkdown() โโบ MarkdownChunking
โ
RagEngine.ImportText(section = "<file> p.<n>")
โ
question โโบ FindMatchingPartitions โโบ QueryPartitions(chat) โโบ answer + sources
โ๏ธ Getting Started
๐ Prerequisites
- .NET 8.0 or later
- A GPU is recommended; the chat model's VRAM is listed in the menu
- About 2.5 GB of disk for the parser's models, plus the embedding and chat models
๐ฅ Download the Project
git clone https://github.com/LM-Kit/lm-kit-net-samples
cd lm-kit-net-samples/console_net/document-intelligence/document-parsing/parsed_document_rag
โถ๏ธ Run the Application
dotnet run
๐ง Troubleshooting
| Problem | Fix |
|---|---|
| "No passage matches" | Rephrase the question, or lower minScore in the code |
| A document changed | Delete its .parsed.json so it is parsed again |
| Out of GPU memory | Choose a smaller chat model from the menu |
๐ Extend the Demo
- Persist the index with
DataSource.CreateFileDataSource(see Build a RAG Pipeline). - Highlight the cited region: every element's bounds are in the stored JSON.