Table of Contents

๐Ÿ‘‰ 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 with ParsedDocument.FromJson.
  • Index each page's body with MarkdownChunking and 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>.
  • source and page metadata 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.

๐Ÿ“š Additional Resources

Share