anchordoc 0.9.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- anchordoc-0.9.3/LICENSE +19 -0
- anchordoc-0.9.3/PKG-INFO +284 -0
- anchordoc-0.9.3/README.md +250 -0
- anchordoc-0.9.3/adapters/__init__.py +11 -0
- anchordoc-0.9.3/adapters/finance_10k.py +36 -0
- anchordoc-0.9.3/adapters/legal_nda.py +36 -0
- anchordoc-0.9.3/adapters/multimodal_doc.py +72 -0
- anchordoc-0.9.3/adapters/procurement_demo.py +52 -0
- anchordoc-0.9.3/anchordoc/__init__.py +57 -0
- anchordoc-0.9.3/anchordoc/adjudicate/__init__.py +10 -0
- anchordoc-0.9.3/anchordoc/adjudicate/answer_contract.py +444 -0
- anchordoc-0.9.3/anchordoc/adjudicate/calc.py +161 -0
- anchordoc-0.9.3/anchordoc/adjudicate/engine.py +756 -0
- anchordoc-0.9.3/anchordoc/cli.py +250 -0
- anchordoc-0.9.3/anchordoc/context.py +586 -0
- anchordoc-0.9.3/anchordoc/docbase/__init__.py +4 -0
- anchordoc-0.9.3/anchordoc/docbase/ooxml_tables.py +166 -0
- anchordoc-0.9.3/anchordoc/docbase/pdf_loader.py +220 -0
- anchordoc-0.9.3/anchordoc/extract/__init__.py +3 -0
- anchordoc-0.9.3/anchordoc/extract/contract.py +158 -0
- anchordoc-0.9.3/anchordoc/index/__init__.py +5 -0
- anchordoc-0.9.3/anchordoc/index/anchor_map.py +588 -0
- anchordoc-0.9.3/anchordoc/index/spancard.py +78 -0
- anchordoc-0.9.3/anchordoc/index/vision_cards.py +139 -0
- anchordoc-0.9.3/anchordoc/mcp_server.py +346 -0
- anchordoc-0.9.3/anchordoc/report/__init__.py +3 -0
- anchordoc-0.9.3/anchordoc/report/exporter.py +215 -0
- anchordoc-0.9.3/anchordoc/retrieve/__init__.py +36 -0
- anchordoc-0.9.3/anchordoc/retrieve/pipeline.py +539 -0
- anchordoc-0.9.3/anchordoc/retrieve/router.py +142 -0
- anchordoc-0.9.3/anchordoc/runtime/__init__.py +14 -0
- anchordoc-0.9.3/anchordoc/runtime/llm.py +266 -0
- anchordoc-0.9.3/anchordoc/runtime/text.py +261 -0
- anchordoc-0.9.3/anchordoc/store/__init__.py +22 -0
- anchordoc-0.9.3/anchordoc/store/bundle.py +402 -0
- anchordoc-0.9.3/anchordoc/types.py +108 -0
- anchordoc-0.9.3/anchordoc/verify.py +1245 -0
- anchordoc-0.9.3/anchordoc.egg-info/PKG-INFO +284 -0
- anchordoc-0.9.3/anchordoc.egg-info/SOURCES.txt +58 -0
- anchordoc-0.9.3/anchordoc.egg-info/dependency_links.txt +1 -0
- anchordoc-0.9.3/anchordoc.egg-info/entry_points.txt +2 -0
- anchordoc-0.9.3/anchordoc.egg-info/requires.txt +14 -0
- anchordoc-0.9.3/anchordoc.egg-info/top_level.txt +2 -0
- anchordoc-0.9.3/pyproject.toml +52 -0
- anchordoc-0.9.3/setup.cfg +4 -0
- anchordoc-0.9.3/tests/test_adversarial_verify.py +307 -0
- anchordoc-0.9.3/tests/test_anchor_and_pdf.py +38 -0
- anchordoc-0.9.3/tests/test_bundle_v07.py +147 -0
- anchordoc-0.9.3/tests/test_calc_and_decide.py +58 -0
- anchordoc-0.9.3/tests/test_cli_and_multimodal.py +128 -0
- anchordoc-0.9.3/tests/test_context_mcp_v07.py +234 -0
- anchordoc-0.9.3/tests/test_end_to_end_offline.py +178 -0
- anchordoc-0.9.3/tests/test_locator_v06.py +313 -0
- anchordoc-0.9.3/tests/test_ooxml_tables.py +57 -0
- anchordoc-0.9.3/tests/test_readall_grounding_v07.py +128 -0
- anchordoc-0.9.3/tests/test_router_v07.py +121 -0
- anchordoc-0.9.3/tests/test_text_and_quote.py +40 -0
- anchordoc-0.9.3/tests/test_v06_adjudicate.py +272 -0
- anchordoc-0.9.3/tests/test_v071_hardening.py +206 -0
- anchordoc-0.9.3/tests/test_v08_calibration_and_prefix.py +188 -0
anchordoc-0.9.3/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
Copyright 2026 AnchorDoc Contributors
|
|
8
|
+
|
|
9
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
you may not use this file except in compliance with the License.
|
|
11
|
+
You may obtain a copy of the License at
|
|
12
|
+
|
|
13
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
|
|
15
|
+
Unless required by applicable law or agreed to in writing, software
|
|
16
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
+
See the License for the specific language governing permissions and
|
|
19
|
+
limitations under the License.
|
anchordoc-0.9.3/PKG-INFO
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: anchordoc
|
|
3
|
+
Version: 0.9.3
|
|
4
|
+
Summary: Structured long-PDF retrieval, cross-page evidence localization, quote verification, and auditable numerical recomputation for AI agents.
|
|
5
|
+
Author-email: ryanvii <ryanwuvii@gmail.com>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/ryanvii/AnchorDoc
|
|
8
|
+
Project-URL: Repository, https://github.com/ryanvii/AnchorDoc
|
|
9
|
+
Project-URL: Issues, https://github.com/ryanvii/AnchorDoc/issues
|
|
10
|
+
Keywords: document-ai,vectorless-rag,long-document,compliance,auditing,pdf,financebench,mcp
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: openai>=1.40.0
|
|
23
|
+
Requires-Dist: httpx>=0.27.0
|
|
24
|
+
Requires-Dist: pypdfium2>=4.25.0
|
|
25
|
+
Requires-Dist: pillow>=10.0.0
|
|
26
|
+
Requires-Dist: mcp<2.0.0,>=1.3.0
|
|
27
|
+
Provides-Extra: dev
|
|
28
|
+
Requires-Dist: pytest>=8.0.0; extra == "dev"
|
|
29
|
+
Provides-Extra: pymupdf
|
|
30
|
+
Requires-Dist: pymupdf>=1.24.0; extra == "pymupdf"
|
|
31
|
+
Provides-Extra: mcp
|
|
32
|
+
Requires-Dist: mcp<2.0.0,>=1.3.0; extra == "mcp"
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
|
|
35
|
+
<div align="center">
|
|
36
|
+
|
|
37
|
+
# ⚓ AnchorDoc
|
|
38
|
+
|
|
39
|
+
**Let AI Agents Read Long PDFs on Demand — Locate Cross-Page Evidence & Verify Every Claim**
|
|
40
|
+
|
|
41
|
+
[](LICENSE)
|
|
42
|
+
[](pyproject.toml)
|
|
43
|
+
[](https://doi.org/10.5281/zenodo.24453581)
|
|
44
|
+
[](#independent-claim-verification-verify-any-ai-answer-v093)
|
|
45
|
+
[](benchmarks/LEADERBOARD.md)
|
|
46
|
+
[](benchmarks/LEADERBOARD.md)
|
|
47
|
+
[](benchmarks/LEADERBOARD.md)
|
|
48
|
+
[](#mcp-server-for-ai-agents-zero-api-key)
|
|
49
|
+
[](https://glama.ai/mcp/servers/ryanvii/AnchorDoc)
|
|
50
|
+
|
|
51
|
+
**English** | [简体中文](README_ZH.md)
|
|
52
|
+
|
|
53
|
+
</div>
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
**AnchorDoc** lets AI agents read long documents on demand to find the exact evidence needed to answer a question. It uses document structure, printed page numbers, and tables to locate relevant passages, keeps digging when evidence is insufficient, and lets every quote and calculation be traced back to the source page.
|
|
58
|
+
|
|
59
|
+
> **The moment AnchorDoc is built for:** Ask *"Did FY2024 Gross Margin expand vs FY2023 after excluding one-time restructuring charges?"* On Round 1, reading the MD&A (Page 1) and Income Statement (Page 2) shows Reported GAAP Gross Margin rose from `36.0%` to `40.0%` (**`+4.0 pp` — looks like YES**). Instead of stopping there, AnchorDoc's Sufficiency Gate detects an unresolved cross-reference (`See Note 7`) needed to know how much restructuring was booked inside Cost of Goods Sold, issues a targeted Round-2 read of **Page 6 (`Note 7`)**, recomputes Adjusted Gross Margin across Page 2 + Page 6 cells (`46.0% → 41.0%`), and **flips the answer to NO (`-5.0 pp` contraction)** — or, if `Note 7` is removed from the PDF, explicitly returns `PARTIALLY_VERIFIED` (`GAAP +4.0 pp verified; adjusted margin has INSUFFICIENT_EVIDENCE`).
|
|
60
|
+
|
|
61
|
+
### Try That Exact Moment in 10 Seconds (Clean Clone, 0 Downloads, 0 API Keys)
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
git clone https://github.com/ryanvii/AnchorDoc.git && cd AnchorDoc
|
|
65
|
+
pip install -e .
|
|
66
|
+
python3 examples/quickstart_verify.py
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Runs both live cases back-to-back on a reproducible **6-page synthetic filing** with real `time.perf_counter()` timing, zero hardcoded page numbers, and zero hardcoded operands:
|
|
70
|
+
- **Run A (Complete 6-page filing with Note 7 on Page 6)**: Round 1 hits `[1, 2]` (`GAAP +4.0 pp`) → detects unresolved `Note 7` → Round 2 reads `[6]` → binds 6 cross-page cells (`p2:L03`, `p2:L04`, `p6:L05`) → flips answer to **`-5.0 pp` (`VERIFIED`)**.
|
|
71
|
+
- **Run B (Same filing with Note 7 removed)**: Round 2 searches unread pages, finds no Note 7, refuses to guess → **`PARTIALLY_VERIFIED`** (`INSUFFICIENT_EVIDENCE`).
|
|
72
|
+
|
|
73
|
+

|
|
74
|
+
|
|
75
|
+
> **Full 1080p HD Launch Demos**: [English 1080p MP4](https://github.com/ryanvii/AnchorDoc/releases/download/v0.9.1/anchordoc_launch_en.mp4) · [中文 1080p MP4](https://github.com/ryanvii/AnchorDoc/releases/download/v0.9.1/anchordoc_launch_zh.mp4)
|
|
76
|
+
|
|
77
|
+

|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## Why AnchorDoc?
|
|
82
|
+
|
|
83
|
+
| Approach | Indexing Cost (250p PDF) | FinanceBench 150/150 E2E* | Multi-Page Recall (AllGold@5) | MMLongBench-Doc (101q) |
|
|
84
|
+
|---|---:|---:|---:|---|
|
|
85
|
+
| Flat Chunk BM25 | `0.3s` · `0` LLM calls | `33.3%` | `32.0%` | `36.6%` (`0.0%` Chart/Fig) |
|
|
86
|
+
| Dense Vector RAG (`BGE-small` ONNX) | `47.6s` (neural embed) | `39.3%` | `48.7%` | Blind to pure-image slides |
|
|
87
|
+
| LLM Tree-Index (PageIndex-style 12p win) | `11.4s` · `19.2` calls · `33k` tok | `35.9%` *(39q subset)* | `43.6%` *(39q subset)* | Requires VLM per page |
|
|
88
|
+
| **⚓ AnchorDoc (`v0.9.3`)** | **`0.33s` · `0` LLM calls** *(warm `3.9ms`)* | **`85.3%`** *(`96.0%` metrics · `84.7%` auto)* | **`89.3%`** *(`100%` metrics)* | **`68.3%`** *(`54.5%` Chart/Fig)* |
|
|
89
|
+
|
|
90
|
+
*\*Unified base model (`deepseek-v4-flash`, `temperature=0`, Apache-2.0 `pypdfium2`). Retrieval/adjudication benchmarks reflect frozen milestone runs preserved backward-compatibly in `v0.9.x`, while `v0.9.0–v0.9.3` add zero-API-key claim verification and cross-reference sufficiency detection; complete ablations and cross-model (`DeepSeek-V4.1-Flash`) tables are in [benchmarks/LEADERBOARD.md](benchmarks/LEADERBOARD.md).*
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
## Independent Claim Verification: Verify ANY AI Answer (`v0.9.3`, Zero API Key)
|
|
95
|
+
|
|
96
|
+
Already using **RAG, PageIndex, or long-context LLMs**? Pass any PDF plus an external claim (quotes, operands bound to row/period/unit, and symbolic or numeric formula) to AnchorDoc **before signing off**. Every report states explicitly **what was verified** (`checks: {quotes, operands, computation, claim_alignment}` + `verified_scope`) with a 4-state verdict (`VERIFIED` | `PARTIALLY_VERIFIED` | `FAILED` | `UNVERIFIED`):
|
|
97
|
+
|
|
98
|
+
- **`QUOTE_SIGN_FLIP` / `SIGN_FLIP`**: `-12%` vs `12%`, accounting parentheses `$(145)` vs `$145` (even when `+12%` exists elsewhere on the same page)
|
|
99
|
+
- **`PERIOD_MISMATCH` / `CLAIM_PERIOD_MISMATCH`**: catches both cell-level column swaps (`378` is `FY2023` while claiming `FY2024`) and claim-level period swaps (valid `FY2024` operands & math `480`, but claim text mislabels it as `FY2023`)
|
|
100
|
+
- **`CLAIM_METRIC_MISMATCH`**: keeps `computation="verified"` while failing `claim_alignment` when valid `rev - cogs = 480` (Gross Profit) is mislabeled in `claim_text` as another statement row such as `Net Income` (actual `Net Income = 125` on the same page)
|
|
101
|
+
- **`UNIT_SCALE_MISMATCH`**: `in millions` vs `billions` confusion (while passing valid `$1,200M == $1.2B` conversions)
|
|
102
|
+
- **`PP_VS_PCT_CONFUSION`**: AST-aware check distinguishing single relative growth (`(a-b)/b*100` → `%`) from differences of two rates/ratios (`(gp24/rev24*100) - (gp23/rev23*100)` → `pp`)
|
|
103
|
+
- **`FORMULA_OPERAND_UNBOUND`** (anti-laundering): supports symbolic formulas (`rev - cogs`) and rejects **any** unbound variable or numeric literal — including mixed formulas like `rev - 700` where valid `rev=1200, cogs=720` operands were attached
|
|
104
|
+
- **`CLAIM_UNSUPPORTED_BY_EVIDENCE` / `CLAIM_ENTAILMENT_NOT_CHECKED`** (scoped honesty): in quote-only mode, a verbatim quote confirms the quote itself (`quotes=verified`), **never** the whole free-text claim — blocking both irrelevant quotes and same-number metric swaps (e.g. `"FY2024 Gross Margin is 65.0%"` + `"Debt-to-Asset <= 65.0%"`) from receiving `VERIFIED`
|
|
105
|
+
|
|
106
|
+
### Run the Clean-Clone Synthetic Regression Suite in 1 Second (0 Downloads, 0 API Keys)
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
python3 benchmarks/run_adversarial_verify.py
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
| Verifier (33 Synthetic Cases: 22 Adversarial Traps + 11 Valid Claims) | Non-Verified Traps (↑) | Verdict Split on Traps | False-Pass (↓) | False-Alarm (↓) | Exact Diagnosis |
|
|
113
|
+
|---|---:|---|---:|---:|---:|
|
|
114
|
+
| Naive Alphanumeric Citation & Loose Number Check | `27.3%` (6/22) | `6 FAILED` | `72.7%` (16/22) | `9.1%` (1/11) | — |
|
|
115
|
+
| **⚓ AnchorDoc `v0.9.3` (`verify_claim` / `doc_verify`)** | **`22 / 22`** | **`20 FAILED` + `2 PARTIALLY_VERIFIED`** | **`0 / 22`** | **`0 / 11`** | **`22 / 22`** |
|
|
116
|
+
|
|
117
|
+
*\*Note: This 33-case suite is a deterministic synthetic regression benchmark covering specific known failure classes (sign flip, year swap, unit swap, pp-vs-%, formula laundering, metric swap); it verifies those regressions cleanly rather than estimating a statistical false-pass rate on arbitrary unseen filings.*
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
# Verify any external AI claim with cell-bound symbolic formula (exit 0 = VERIFIED, exit 2 = FAILED / PARTIAL)
|
|
121
|
+
anchordoc verify-claim report.pdf \
|
|
122
|
+
--claim "FY2024 Gross profit is 480 million." \
|
|
123
|
+
--operands-json '[{"name":"rev","value":1200,"unit":"million","row_label":"Total Revenue","period":"FY2024","page":2},{"name":"cogs","value":720,"unit":"million","row_label":"Cost of Goods Sold","period":"FY2024","page":2}]' \
|
|
124
|
+
--formula "rev - cogs" --claimed-value 480 --claimed-unit "million"
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+

|
|
128
|
+
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
## Verified Public Benchmarks (401 Official Questions Across 3 Modalities)
|
|
132
|
+
|
|
133
|
+
- **FinanceBench (Full Official 150 / 150, 84 SEC PDFs, 12,013 pages)**
|
|
134
|
+
- **Indexing**: **27.6s total (`0.33s/PDF`), `0` LLM calls** (145× faster than CPU Dense BGE embedding, 91× faster than upfront LLM tree indexing).
|
|
135
|
+
- **Retrieval (`v0.6` Query-Aware Locator)**: **93.3% Hit@5 / 89.3% AllGold@5** (`+8.6pp` / `+8.0pp` over `v0.5`, MRR `0.8008`) vs Dense BGE **58.0% / 48.7%** and Chunk-BM25 **35.3% / 32.0%**, while cutting query prompt tokens by **23.8%** (`14,150 → 10,780`) and LLM calls by **22.4%** (`5.35 → 4.15`).
|
|
136
|
+
- **End-to-End QA**: **85.3% overall** (`+6.6pp` over `v0.5`, paired McNemar $p=0.0129$; **2.17×** Dense BGE `39.3%`). On all 50 hard numerical statement questions (`metrics-generated`), AnchorDoc achieves **100.0% AllGold@5** and **96.0% E2E accuracy (48/50)** via reasoning-first extraction + authoritative Python AST arithmetic (`safe_calc`, `100%` operand provenance). Replicated on official **`DeepSeek-V4.1-Flash`**: **92.0% Hit@5 / 82.7% E2E** (`92.0%` metrics, `101/101` operands grounded).
|
|
137
|
+
- **MMLongBench-Doc (NeurIPS 2024 — 101 questions, 7 non-financial multimodal PDFs incl. 3 pure-image slide decks)**
|
|
138
|
+
- **Selective `VisionPageCard`** skips 100% of text-rich digital pages (`78/170`) and indexes only low-text/scanned pages (`92/170`).
|
|
139
|
+
- **End-to-End (`v0.7` `full_pool` + Tri-State Gate + 2×2 High-Res Zoom)**: **68.3% overall** (`+15.8pp` over `v0.5`, vs `36.6%` ChunkRAG), **54.5%** Chart/Figure (**2.25×** `v0.5`'s `24.2%`), **65.6%** Text/Table, and **83.3%** Unanswerable Abstain accuracy (hallucination cut to `16.7%`).
|
|
140
|
+
- **Unseen Blind Generalization (`v0.6.0`, 10 unseen PDFs, 492 pages, 91q incl. 52 Chart/Figure)**: **91.0% Hit@5**, **50.0% Chart/Figure** (vs `1.9%` ChunkRAG), **50.5% overall** (`+19.7pp` over ChunkRAG).
|
|
141
|
+
- **ContractNLI-Long (EMNLP — 150 long legal NDAs `>= 10k` chars, balanced 50/50/50)**
|
|
142
|
+
- **Two-Stage Logic NLI + Adaptive `ReadAll` Router**: **81.3% Accuracy / 81.3% Macro-F1** (`10.0%` FER on `test-150`); on the strictly unseen held-out `dev-150` split (34 unseen contracts), `v0.7` `route=auto` maintains statistical parity in accuracy (`79.3%` vs `78.0%`, Macro-F1 `79.5%` vs `77.9%`) while cutting LLM calls by **41%** (`~3.0 → 1.77 calls/q`).
|
|
143
|
+
- **Persistent SQLite WAL `BundleStore` & Prefix-Stable `nav` Caching (`v0.8.0`)**
|
|
144
|
+
- Reloads all **84 SEC PDFs (12,013 pages)** in **`0.330s`** vs `22.13s` cold parse (**`67.1×` faster**, `3.9 ms/PDF`), and persists expensive `VisionPageCard` summaries for `0`-LLM / `0`-render reloads.
|
|
145
|
+
- **T7 3-Run Live Variance & Prefix KV Cache** (30 stratified FinanceBench questions across 5 multi-question SEC 10-Ks on official `DeepSeek-V4.1-Flash`, `temp=0`): **`85.55% ± 3.85%`** E2E Acc, **`100.0% ± 0.0%`** Quote Verified (90/90), **`54.7% ± 10.7%`** real server-side KV prefix cache hit rate (rising to **`65.1%`** warm via prefix-stable `anchordoc_nav`), and **`83.33% ± 0.00%`** deterministic SHA-cached offline replay.
|
|
146
|
+
|
|
147
|
+
> **Version Reproducibility Note**: `v0.6`, `v0.7`, and `v0.7.1` denote the frozen milestone runs for each benchmark suite above; **`v0.8.0`** preserves the exact retrieval/adjudication core backward-compatibly while adding `BundleStore`, prefix-stable `nav` KV caching, and the 4-tool MCP server (verified on `v0.8.0` live T7 replay at `85.55% ± 3.85%` E2E and `100.0%` quote verification).
|
|
148
|
+
|
|
149
|
+
See [benchmarks/LEADERBOARD.md](benchmarks/LEADERBOARD.md) for complete tables, ablations, cost-amortization notes, and one-command reproduction scripts.
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
## 60-Second Quickstart
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
git clone https://github.com/ryanvii/AnchorDoc.git
|
|
157
|
+
cd AnchorDoc
|
|
158
|
+
pip install -e .
|
|
159
|
+
|
|
160
|
+
# 1. Zero-LLM structural inspection (NO API key required — runs in ~0.2s!)
|
|
161
|
+
anchordoc outline annual_report_10k.pdf
|
|
162
|
+
|
|
163
|
+
# 2. Verifiable numerical/compliance QA + interactive two-pane HTML report (with embedded original PDF pages!)
|
|
164
|
+
export ANCHORDOC_LLM_BASE=https://api.openai.com/v1
|
|
165
|
+
export ANCHORDOC_LLM_MODEL=gpt-4o-mini
|
|
166
|
+
export ANCHORDOC_LLM_KEY=sk-...
|
|
167
|
+
|
|
168
|
+
anchordoc query annual_report_10k.pdf \
|
|
169
|
+
"What is FY2023 gross profit margin?" \
|
|
170
|
+
--html report.html
|
|
171
|
+
|
|
172
|
+
# 3. Audit a PDF against a JSON checklist (add --vision for scanned/chart PDFs)
|
|
173
|
+
anchordoc audit submission.pdf \
|
|
174
|
+
--checklist examples/sample_checklist.json \
|
|
175
|
+
--html audit_workbench.html
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### Python SDK (5 Lines)
|
|
179
|
+
|
|
180
|
+
```python
|
|
181
|
+
from anchordoc import AnchorRetriever, CachedLLM, FactAdjudicator, PdfDocument, export_html
|
|
182
|
+
|
|
183
|
+
doc = PdfDocument.from_pdf("annual_report_10k.pdf") # Apache-2.0 pypdfium2
|
|
184
|
+
llm = CachedLLM(cache_dir="./cache") # SHA256 cache + OFFLINE replay
|
|
185
|
+
hit = AnchorRetriever(doc, llm=llm).retrieve("FY2023 gross margin?", top_k=5)
|
|
186
|
+
verdict = FactAdjudicator(llm=llm).answer_numerical(doc, "FY2023 gross margin?", hit.pages)
|
|
187
|
+
export_html([verdict], "report.html", doc=doc) # Standalone two-pane HTML workbench
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
---
|
|
191
|
+
|
|
192
|
+
## MCP Server for AI Agents (Zero API Key)
|
|
193
|
+
|
|
194
|
+
Give **Claude Code, Cursor, Windsurf, Antigravity, or Devin** progressive, token-bounded, verifiable access to 500-page PDFs with **zero external LLM calls** (the host agent *is* the LLM — AnchorDoc acts as its zero-LLM index, token guardrail, and deterministic verifier):
|
|
195
|
+
|
|
196
|
+
### Same-Agent Benchmark: Native Full-PDF vs AnchorDoc Zero-LLM MCP (`DeepSeek-V4.1-Flash`, `temp=0`, 15 SEC 10-K/10-Q filings, avg 144.7p)
|
|
197
|
+
|
|
198
|
+
| Same Agent (`DeepSeek-V4.1-Flash`) | E2E Acc | Metrics / Novel | Quote Verified | Avg Prompt Tokens | Newly Billed Uncached Tokens |
|
|
199
|
+
|---|---:|---:|---:|---:|---:|
|
|
200
|
+
| Native Full-PDF Stuffing *(450k-char cap)* | `60.0%` | `80% / 60%` | `53.3%` | `90,824` | `72,827` |
|
|
201
|
+
| **⚓ Same Agent + AnchorDoc MCP** *(`0` server LLM calls)* | **`86.7%` (+26.7pp)** | **`100% / 100%`** | **`100.0%` (15/15)** | **`28,188` (-69.0%)** | **`5,430` (-92.5%, 13.4× cheaper)** |
|
|
202
|
+
|
|
203
|
+
*(Even on the 8 completely untruncated PDFs where 100% of text fit in Native Full-PDF, AnchorDoc MCP still won **`87.5%` vs `50.0%`** by preventing long-context attention decay.)*
|
|
204
|
+
|
|
205
|
+
### 1-Minute MCP Setup (Claude Code / Cursor / Windsurf / Devin)
|
|
206
|
+
|
|
207
|
+
```bash
|
|
208
|
+
git clone https://github.com/ryanvii/AnchorDoc.git
|
|
209
|
+
cd AnchorDoc
|
|
210
|
+
pip install -e '.[mcp]'
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
Add to your `mcp.json` (Cursor `.cursor/mcp.json`, Claude Desktop/Code `claude_desktop_config.json`, or Windsurf/Devin MCP config):
|
|
214
|
+
|
|
215
|
+
```json
|
|
216
|
+
{
|
|
217
|
+
"mcpServers": {
|
|
218
|
+
"anchordoc": {
|
|
219
|
+
"command": "anchordoc",
|
|
220
|
+
"args": ["mcp", "--root", "/path/to/your/pdfs"]
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
Exposes 4 orthogonal, read-only tools with strict `--root` allowlisting and hard token caps:
|
|
227
|
+
- `doc_outline` (L0 map, ≤1.5k tok): structure, printed page range, and `route_hint`.
|
|
228
|
+
- `doc_locate` (L1 zero-LLM locator, ≤1.2k tok): lexical BM25-RRF + page-ref + continuation snippets.
|
|
229
|
+
- `doc_read` (L2 anchored reader, ≤5 pages): stable `p12:L07` anchors + session duplicate-read ledger + native MCP `ImageContent`.
|
|
230
|
+
- `doc_verify`: deterministic quote verification, page localization, AST math (`safe_calc`), and operand provenance.
|
|
231
|
+
|
|
232
|
+
### Progressive Context Engineering Ecosystem (Documents + Videos)
|
|
233
|
+
|
|
234
|
+
AnchorDoc shares the exact same **zero-vector, zero-server-LLM, token-budgeted progressive disclosure protocol** with our companion video context MCP server:
|
|
235
|
+
|
|
236
|
+
| Modality | Repository | Progressive Protocol (`L0 → L1 → L2 → Verify`) | Dependencies |
|
|
237
|
+
|---|---|---|---|
|
|
238
|
+
| **Long PDFs & Filings** | **⚓ [AnchorDoc](https://github.com/ryanvii/AnchorDoc)** *(this repo)* | `doc_outline` → `doc_locate` → `doc_read` → `doc_verify` | Pure Python + Apache-2.0 `pypdfium2` |
|
|
239
|
+
| **Long Videos & Demos** | **🎬 [vidctx-mcp](https://github.com/ryanvii/vidctx-mcp)** | `peek_video_map` → `search_video_moments` → `inspect_frame_range` | `yt-dlp` / `ffmpeg` / `RapidOCR` |
|
|
240
|
+
|
|
241
|
+
---
|
|
242
|
+
|
|
243
|
+
## Core Modules & Domain Adapters
|
|
244
|
+
|
|
245
|
+
- `anchordoc/docbase`: Thread-safe Apache-2.0 1-based `PageModel` (`pypdfium2`) + `PageLabels` + 2×2 high-res tile rendering + stdlib OOXML merged-cell table grid (`w:vMerge` / `w:gridSpan` inheritance, zero false-blanks).
|
|
246
|
+
- `anchordoc/store`: Per-document stdlib SQLite WAL `BundleStore` (layered `doc_sha256` / `extractor_fp` / `vision_fp` invalidation, **67× faster** warm reload, zero-render `VisionPageCard` reuse).
|
|
247
|
+
- `anchordoc/context.py` & `anchordoc/mcp_server.py`: Zero-LLM progressive `DocContext` API and 4-tool stdio MCP server.
|
|
248
|
+
- `anchordoc/index`: `Zero-LLM AnchorMap`, Printed-TOC (`G7`) offset calibrator & soft demotion, printed-page/slide reference resolver, `SelectiveVisionIndexer` (`VisionPageCard`), and `SpanCardIndex`.
|
|
249
|
+
- `anchordoc/retrieve`: Cost-derived 3-tier adaptive router (`read_all` | `full_pool` | `navigate`) + Query-Aware Single-Shot Locator (`_pack_snippets`) + continuation-table expansion + conditional decorrelated `B42` gap-followup + full `RetrievalTrace`.
|
|
250
|
+
- `anchordoc/extract`: Line-anchored `[sNNNNN]` requirement extraction with automatic verbatim quote-repair loop.
|
|
251
|
+
- `anchordoc/adjudicate`: Reasoning-first verifiable adjudicator with deterministic `AnswerContract`, authoritative Python AST arithmetic (`safe_calc`), Grounding Scope Guard, tri-state visual evidence gate (`stated | derivable | absent`), and deterministic truth tables (`decide_compliance`, `decide_nli`).
|
|
252
|
+
- `anchordoc/report`: JSON, Markdown, and zero-server interactive two-pane HTML trace workbench with embedded original PDF page previews.
|
|
253
|
+
- `adapters/`: Thin domain plugins — `finance_10k`, `legal_nda`, `multimodal_doc`, `procurement_demo`.
|
|
254
|
+
|
|
255
|
+
---
|
|
256
|
+
|
|
257
|
+
## Roadmap: Evidence Sufficiency (`v1.0`)
|
|
258
|
+
|
|
259
|
+
Beyond zero-index retrieval and deterministic claim verification, AnchorDoc's long-term north-star research problem is **Evidence Sufficiency**:
|
|
260
|
+
|
|
261
|
+
> *How does a long-document system know when the context it retrieved is truly sufficient to answer — and when it must read one more page before committing?*
|
|
262
|
+
|
|
263
|
+
- **Current (`v0.9.3`)**: Zero-LLM AnchorMap, continuation-table expansion, conditional gap-followup (`B42`), zero-LLM cross-reference gap detection (`detect_unresolved_references`), tri-state visual gate (`stated | derivable | absent`), and cell-bound verification (`VERIFIED | PARTIALLY_VERIFIED | FAILED | UNVERIFIED`).
|
|
264
|
+
- **Next (`v1.0`)**: Closed-loop **sufficiency-driven reading** across arbitrary 100–600 page filings — unifying cross-reference gaps, missing comparative periods, and undefined adjusted-metric footnotes directly inside the core retriever + MCP tool loop.
|
|
265
|
+
|
|
266
|
+
---
|
|
267
|
+
|
|
268
|
+
## Citation
|
|
269
|
+
|
|
270
|
+
If you use AnchorDoc or its reproducible benchmarks in your work, please cite via [CITATION.cff](CITATION.cff):
|
|
271
|
+
|
|
272
|
+
```bibtex
|
|
273
|
+
@software{wu2026anchordoc,
|
|
274
|
+
author = {Wu, HuiYang},
|
|
275
|
+
title = {AnchorDoc: Zero-Index-Cost Long-Document Context Engineering with Falsifiable Evidence Chains},
|
|
276
|
+
year = {2026},
|
|
277
|
+
version = {0.9.3},
|
|
278
|
+
doi = {10.5281/zenodo.24453581},
|
|
279
|
+
url = {https://github.com/ryanvii/AnchorDoc},
|
|
280
|
+
license = {Apache-2.0}
|
|
281
|
+
}
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
<!-- mcp-name: io.github.ryanvii/anchordoc -->
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# ⚓ AnchorDoc
|
|
4
|
+
|
|
5
|
+
**Let AI Agents Read Long PDFs on Demand — Locate Cross-Page Evidence & Verify Every Claim**
|
|
6
|
+
|
|
7
|
+
[](LICENSE)
|
|
8
|
+
[](pyproject.toml)
|
|
9
|
+
[](https://doi.org/10.5281/zenodo.24453581)
|
|
10
|
+
[](#independent-claim-verification-verify-any-ai-answer-v093)
|
|
11
|
+
[](benchmarks/LEADERBOARD.md)
|
|
12
|
+
[](benchmarks/LEADERBOARD.md)
|
|
13
|
+
[](benchmarks/LEADERBOARD.md)
|
|
14
|
+
[](#mcp-server-for-ai-agents-zero-api-key)
|
|
15
|
+
[](https://glama.ai/mcp/servers/ryanvii/AnchorDoc)
|
|
16
|
+
|
|
17
|
+
**English** | [简体中文](README_ZH.md)
|
|
18
|
+
|
|
19
|
+
</div>
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
**AnchorDoc** lets AI agents read long documents on demand to find the exact evidence needed to answer a question. It uses document structure, printed page numbers, and tables to locate relevant passages, keeps digging when evidence is insufficient, and lets every quote and calculation be traced back to the source page.
|
|
24
|
+
|
|
25
|
+
> **The moment AnchorDoc is built for:** Ask *"Did FY2024 Gross Margin expand vs FY2023 after excluding one-time restructuring charges?"* On Round 1, reading the MD&A (Page 1) and Income Statement (Page 2) shows Reported GAAP Gross Margin rose from `36.0%` to `40.0%` (**`+4.0 pp` — looks like YES**). Instead of stopping there, AnchorDoc's Sufficiency Gate detects an unresolved cross-reference (`See Note 7`) needed to know how much restructuring was booked inside Cost of Goods Sold, issues a targeted Round-2 read of **Page 6 (`Note 7`)**, recomputes Adjusted Gross Margin across Page 2 + Page 6 cells (`46.0% → 41.0%`), and **flips the answer to NO (`-5.0 pp` contraction)** — or, if `Note 7` is removed from the PDF, explicitly returns `PARTIALLY_VERIFIED` (`GAAP +4.0 pp verified; adjusted margin has INSUFFICIENT_EVIDENCE`).
|
|
26
|
+
|
|
27
|
+
### Try That Exact Moment in 10 Seconds (Clean Clone, 0 Downloads, 0 API Keys)
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
git clone https://github.com/ryanvii/AnchorDoc.git && cd AnchorDoc
|
|
31
|
+
pip install -e .
|
|
32
|
+
python3 examples/quickstart_verify.py
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Runs both live cases back-to-back on a reproducible **6-page synthetic filing** with real `time.perf_counter()` timing, zero hardcoded page numbers, and zero hardcoded operands:
|
|
36
|
+
- **Run A (Complete 6-page filing with Note 7 on Page 6)**: Round 1 hits `[1, 2]` (`GAAP +4.0 pp`) → detects unresolved `Note 7` → Round 2 reads `[6]` → binds 6 cross-page cells (`p2:L03`, `p2:L04`, `p6:L05`) → flips answer to **`-5.0 pp` (`VERIFIED`)**.
|
|
37
|
+
- **Run B (Same filing with Note 7 removed)**: Round 2 searches unread pages, finds no Note 7, refuses to guess → **`PARTIALLY_VERIFIED`** (`INSUFFICIENT_EVIDENCE`).
|
|
38
|
+
|
|
39
|
+

|
|
40
|
+
|
|
41
|
+
> **Full 1080p HD Launch Demos**: [English 1080p MP4](https://github.com/ryanvii/AnchorDoc/releases/download/v0.9.1/anchordoc_launch_en.mp4) · [中文 1080p MP4](https://github.com/ryanvii/AnchorDoc/releases/download/v0.9.1/anchordoc_launch_zh.mp4)
|
|
42
|
+
|
|
43
|
+

|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Why AnchorDoc?
|
|
48
|
+
|
|
49
|
+
| Approach | Indexing Cost (250p PDF) | FinanceBench 150/150 E2E* | Multi-Page Recall (AllGold@5) | MMLongBench-Doc (101q) |
|
|
50
|
+
|---|---:|---:|---:|---|
|
|
51
|
+
| Flat Chunk BM25 | `0.3s` · `0` LLM calls | `33.3%` | `32.0%` | `36.6%` (`0.0%` Chart/Fig) |
|
|
52
|
+
| Dense Vector RAG (`BGE-small` ONNX) | `47.6s` (neural embed) | `39.3%` | `48.7%` | Blind to pure-image slides |
|
|
53
|
+
| LLM Tree-Index (PageIndex-style 12p win) | `11.4s` · `19.2` calls · `33k` tok | `35.9%` *(39q subset)* | `43.6%` *(39q subset)* | Requires VLM per page |
|
|
54
|
+
| **⚓ AnchorDoc (`v0.9.3`)** | **`0.33s` · `0` LLM calls** *(warm `3.9ms`)* | **`85.3%`** *(`96.0%` metrics · `84.7%` auto)* | **`89.3%`** *(`100%` metrics)* | **`68.3%`** *(`54.5%` Chart/Fig)* |
|
|
55
|
+
|
|
56
|
+
*\*Unified base model (`deepseek-v4-flash`, `temperature=0`, Apache-2.0 `pypdfium2`). Retrieval/adjudication benchmarks reflect frozen milestone runs preserved backward-compatibly in `v0.9.x`, while `v0.9.0–v0.9.3` add zero-API-key claim verification and cross-reference sufficiency detection; complete ablations and cross-model (`DeepSeek-V4.1-Flash`) tables are in [benchmarks/LEADERBOARD.md](benchmarks/LEADERBOARD.md).*
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## Independent Claim Verification: Verify ANY AI Answer (`v0.9.3`, Zero API Key)
|
|
61
|
+
|
|
62
|
+
Already using **RAG, PageIndex, or long-context LLMs**? Pass any PDF plus an external claim (quotes, operands bound to row/period/unit, and symbolic or numeric formula) to AnchorDoc **before signing off**. Every report states explicitly **what was verified** (`checks: {quotes, operands, computation, claim_alignment}` + `verified_scope`) with a 4-state verdict (`VERIFIED` | `PARTIALLY_VERIFIED` | `FAILED` | `UNVERIFIED`):
|
|
63
|
+
|
|
64
|
+
- **`QUOTE_SIGN_FLIP` / `SIGN_FLIP`**: `-12%` vs `12%`, accounting parentheses `$(145)` vs `$145` (even when `+12%` exists elsewhere on the same page)
|
|
65
|
+
- **`PERIOD_MISMATCH` / `CLAIM_PERIOD_MISMATCH`**: catches both cell-level column swaps (`378` is `FY2023` while claiming `FY2024`) and claim-level period swaps (valid `FY2024` operands & math `480`, but claim text mislabels it as `FY2023`)
|
|
66
|
+
- **`CLAIM_METRIC_MISMATCH`**: keeps `computation="verified"` while failing `claim_alignment` when valid `rev - cogs = 480` (Gross Profit) is mislabeled in `claim_text` as another statement row such as `Net Income` (actual `Net Income = 125` on the same page)
|
|
67
|
+
- **`UNIT_SCALE_MISMATCH`**: `in millions` vs `billions` confusion (while passing valid `$1,200M == $1.2B` conversions)
|
|
68
|
+
- **`PP_VS_PCT_CONFUSION`**: AST-aware check distinguishing single relative growth (`(a-b)/b*100` → `%`) from differences of two rates/ratios (`(gp24/rev24*100) - (gp23/rev23*100)` → `pp`)
|
|
69
|
+
- **`FORMULA_OPERAND_UNBOUND`** (anti-laundering): supports symbolic formulas (`rev - cogs`) and rejects **any** unbound variable or numeric literal — including mixed formulas like `rev - 700` where valid `rev=1200, cogs=720` operands were attached
|
|
70
|
+
- **`CLAIM_UNSUPPORTED_BY_EVIDENCE` / `CLAIM_ENTAILMENT_NOT_CHECKED`** (scoped honesty): in quote-only mode, a verbatim quote confirms the quote itself (`quotes=verified`), **never** the whole free-text claim — blocking both irrelevant quotes and same-number metric swaps (e.g. `"FY2024 Gross Margin is 65.0%"` + `"Debt-to-Asset <= 65.0%"`) from receiving `VERIFIED`
|
|
71
|
+
|
|
72
|
+
### Run the Clean-Clone Synthetic Regression Suite in 1 Second (0 Downloads, 0 API Keys)
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
python3 benchmarks/run_adversarial_verify.py
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
| Verifier (33 Synthetic Cases: 22 Adversarial Traps + 11 Valid Claims) | Non-Verified Traps (↑) | Verdict Split on Traps | False-Pass (↓) | False-Alarm (↓) | Exact Diagnosis |
|
|
79
|
+
|---|---:|---|---:|---:|---:|
|
|
80
|
+
| Naive Alphanumeric Citation & Loose Number Check | `27.3%` (6/22) | `6 FAILED` | `72.7%` (16/22) | `9.1%` (1/11) | — |
|
|
81
|
+
| **⚓ AnchorDoc `v0.9.3` (`verify_claim` / `doc_verify`)** | **`22 / 22`** | **`20 FAILED` + `2 PARTIALLY_VERIFIED`** | **`0 / 22`** | **`0 / 11`** | **`22 / 22`** |
|
|
82
|
+
|
|
83
|
+
*\*Note: This 33-case suite is a deterministic synthetic regression benchmark covering specific known failure classes (sign flip, year swap, unit swap, pp-vs-%, formula laundering, metric swap); it verifies those regressions cleanly rather than estimating a statistical false-pass rate on arbitrary unseen filings.*
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
# Verify any external AI claim with cell-bound symbolic formula (exit 0 = VERIFIED, exit 2 = FAILED / PARTIAL)
|
|
87
|
+
anchordoc verify-claim report.pdf \
|
|
88
|
+
--claim "FY2024 Gross profit is 480 million." \
|
|
89
|
+
--operands-json '[{"name":"rev","value":1200,"unit":"million","row_label":"Total Revenue","period":"FY2024","page":2},{"name":"cogs","value":720,"unit":"million","row_label":"Cost of Goods Sold","period":"FY2024","page":2}]' \
|
|
90
|
+
--formula "rev - cogs" --claimed-value 480 --claimed-unit "million"
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+

|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## Verified Public Benchmarks (401 Official Questions Across 3 Modalities)
|
|
98
|
+
|
|
99
|
+
- **FinanceBench (Full Official 150 / 150, 84 SEC PDFs, 12,013 pages)**
|
|
100
|
+
- **Indexing**: **27.6s total (`0.33s/PDF`), `0` LLM calls** (145× faster than CPU Dense BGE embedding, 91× faster than upfront LLM tree indexing).
|
|
101
|
+
- **Retrieval (`v0.6` Query-Aware Locator)**: **93.3% Hit@5 / 89.3% AllGold@5** (`+8.6pp` / `+8.0pp` over `v0.5`, MRR `0.8008`) vs Dense BGE **58.0% / 48.7%** and Chunk-BM25 **35.3% / 32.0%**, while cutting query prompt tokens by **23.8%** (`14,150 → 10,780`) and LLM calls by **22.4%** (`5.35 → 4.15`).
|
|
102
|
+
- **End-to-End QA**: **85.3% overall** (`+6.6pp` over `v0.5`, paired McNemar $p=0.0129$; **2.17×** Dense BGE `39.3%`). On all 50 hard numerical statement questions (`metrics-generated`), AnchorDoc achieves **100.0% AllGold@5** and **96.0% E2E accuracy (48/50)** via reasoning-first extraction + authoritative Python AST arithmetic (`safe_calc`, `100%` operand provenance). Replicated on official **`DeepSeek-V4.1-Flash`**: **92.0% Hit@5 / 82.7% E2E** (`92.0%` metrics, `101/101` operands grounded).
|
|
103
|
+
- **MMLongBench-Doc (NeurIPS 2024 — 101 questions, 7 non-financial multimodal PDFs incl. 3 pure-image slide decks)**
|
|
104
|
+
- **Selective `VisionPageCard`** skips 100% of text-rich digital pages (`78/170`) and indexes only low-text/scanned pages (`92/170`).
|
|
105
|
+
- **End-to-End (`v0.7` `full_pool` + Tri-State Gate + 2×2 High-Res Zoom)**: **68.3% overall** (`+15.8pp` over `v0.5`, vs `36.6%` ChunkRAG), **54.5%** Chart/Figure (**2.25×** `v0.5`'s `24.2%`), **65.6%** Text/Table, and **83.3%** Unanswerable Abstain accuracy (hallucination cut to `16.7%`).
|
|
106
|
+
- **Unseen Blind Generalization (`v0.6.0`, 10 unseen PDFs, 492 pages, 91q incl. 52 Chart/Figure)**: **91.0% Hit@5**, **50.0% Chart/Figure** (vs `1.9%` ChunkRAG), **50.5% overall** (`+19.7pp` over ChunkRAG).
|
|
107
|
+
- **ContractNLI-Long (EMNLP — 150 long legal NDAs `>= 10k` chars, balanced 50/50/50)**
|
|
108
|
+
- **Two-Stage Logic NLI + Adaptive `ReadAll` Router**: **81.3% Accuracy / 81.3% Macro-F1** (`10.0%` FER on `test-150`); on the strictly unseen held-out `dev-150` split (34 unseen contracts), `v0.7` `route=auto` maintains statistical parity in accuracy (`79.3%` vs `78.0%`, Macro-F1 `79.5%` vs `77.9%`) while cutting LLM calls by **41%** (`~3.0 → 1.77 calls/q`).
|
|
109
|
+
- **Persistent SQLite WAL `BundleStore` & Prefix-Stable `nav` Caching (`v0.8.0`)**
|
|
110
|
+
- Reloads all **84 SEC PDFs (12,013 pages)** in **`0.330s`** vs `22.13s` cold parse (**`67.1×` faster**, `3.9 ms/PDF`), and persists expensive `VisionPageCard` summaries for `0`-LLM / `0`-render reloads.
|
|
111
|
+
- **T7 3-Run Live Variance & Prefix KV Cache** (30 stratified FinanceBench questions across 5 multi-question SEC 10-Ks on official `DeepSeek-V4.1-Flash`, `temp=0`): **`85.55% ± 3.85%`** E2E Acc, **`100.0% ± 0.0%`** Quote Verified (90/90), **`54.7% ± 10.7%`** real server-side KV prefix cache hit rate (rising to **`65.1%`** warm via prefix-stable `anchordoc_nav`), and **`83.33% ± 0.00%`** deterministic SHA-cached offline replay.
|
|
112
|
+
|
|
113
|
+
> **Version Reproducibility Note**: `v0.6`, `v0.7`, and `v0.7.1` denote the frozen milestone runs for each benchmark suite above; **`v0.8.0`** preserves the exact retrieval/adjudication core backward-compatibly while adding `BundleStore`, prefix-stable `nav` KV caching, and the 4-tool MCP server (verified on `v0.8.0` live T7 replay at `85.55% ± 3.85%` E2E and `100.0%` quote verification).
|
|
114
|
+
|
|
115
|
+
See [benchmarks/LEADERBOARD.md](benchmarks/LEADERBOARD.md) for complete tables, ablations, cost-amortization notes, and one-command reproduction scripts.
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
## 60-Second Quickstart
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
git clone https://github.com/ryanvii/AnchorDoc.git
|
|
123
|
+
cd AnchorDoc
|
|
124
|
+
pip install -e .
|
|
125
|
+
|
|
126
|
+
# 1. Zero-LLM structural inspection (NO API key required — runs in ~0.2s!)
|
|
127
|
+
anchordoc outline annual_report_10k.pdf
|
|
128
|
+
|
|
129
|
+
# 2. Verifiable numerical/compliance QA + interactive two-pane HTML report (with embedded original PDF pages!)
|
|
130
|
+
export ANCHORDOC_LLM_BASE=https://api.openai.com/v1
|
|
131
|
+
export ANCHORDOC_LLM_MODEL=gpt-4o-mini
|
|
132
|
+
export ANCHORDOC_LLM_KEY=sk-...
|
|
133
|
+
|
|
134
|
+
anchordoc query annual_report_10k.pdf \
|
|
135
|
+
"What is FY2023 gross profit margin?" \
|
|
136
|
+
--html report.html
|
|
137
|
+
|
|
138
|
+
# 3. Audit a PDF against a JSON checklist (add --vision for scanned/chart PDFs)
|
|
139
|
+
anchordoc audit submission.pdf \
|
|
140
|
+
--checklist examples/sample_checklist.json \
|
|
141
|
+
--html audit_workbench.html
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Python SDK (5 Lines)
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
from anchordoc import AnchorRetriever, CachedLLM, FactAdjudicator, PdfDocument, export_html
|
|
148
|
+
|
|
149
|
+
doc = PdfDocument.from_pdf("annual_report_10k.pdf") # Apache-2.0 pypdfium2
|
|
150
|
+
llm = CachedLLM(cache_dir="./cache") # SHA256 cache + OFFLINE replay
|
|
151
|
+
hit = AnchorRetriever(doc, llm=llm).retrieve("FY2023 gross margin?", top_k=5)
|
|
152
|
+
verdict = FactAdjudicator(llm=llm).answer_numerical(doc, "FY2023 gross margin?", hit.pages)
|
|
153
|
+
export_html([verdict], "report.html", doc=doc) # Standalone two-pane HTML workbench
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
---
|
|
157
|
+
|
|
158
|
+
## MCP Server for AI Agents (Zero API Key)
|
|
159
|
+
|
|
160
|
+
Give **Claude Code, Cursor, Windsurf, Antigravity, or Devin** progressive, token-bounded, verifiable access to 500-page PDFs with **zero external LLM calls** (the host agent *is* the LLM — AnchorDoc acts as its zero-LLM index, token guardrail, and deterministic verifier):
|
|
161
|
+
|
|
162
|
+
### Same-Agent Benchmark: Native Full-PDF vs AnchorDoc Zero-LLM MCP (`DeepSeek-V4.1-Flash`, `temp=0`, 15 SEC 10-K/10-Q filings, avg 144.7p)
|
|
163
|
+
|
|
164
|
+
| Same Agent (`DeepSeek-V4.1-Flash`) | E2E Acc | Metrics / Novel | Quote Verified | Avg Prompt Tokens | Newly Billed Uncached Tokens |
|
|
165
|
+
|---|---:|---:|---:|---:|---:|
|
|
166
|
+
| Native Full-PDF Stuffing *(450k-char cap)* | `60.0%` | `80% / 60%` | `53.3%` | `90,824` | `72,827` |
|
|
167
|
+
| **⚓ Same Agent + AnchorDoc MCP** *(`0` server LLM calls)* | **`86.7%` (+26.7pp)** | **`100% / 100%`** | **`100.0%` (15/15)** | **`28,188` (-69.0%)** | **`5,430` (-92.5%, 13.4× cheaper)** |
|
|
168
|
+
|
|
169
|
+
*(Even on the 8 completely untruncated PDFs where 100% of text fit in Native Full-PDF, AnchorDoc MCP still won **`87.5%` vs `50.0%`** by preventing long-context attention decay.)*
|
|
170
|
+
|
|
171
|
+
### 1-Minute MCP Setup (Claude Code / Cursor / Windsurf / Devin)
|
|
172
|
+
|
|
173
|
+
```bash
|
|
174
|
+
git clone https://github.com/ryanvii/AnchorDoc.git
|
|
175
|
+
cd AnchorDoc
|
|
176
|
+
pip install -e '.[mcp]'
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Add to your `mcp.json` (Cursor `.cursor/mcp.json`, Claude Desktop/Code `claude_desktop_config.json`, or Windsurf/Devin MCP config):
|
|
180
|
+
|
|
181
|
+
```json
|
|
182
|
+
{
|
|
183
|
+
"mcpServers": {
|
|
184
|
+
"anchordoc": {
|
|
185
|
+
"command": "anchordoc",
|
|
186
|
+
"args": ["mcp", "--root", "/path/to/your/pdfs"]
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Exposes 4 orthogonal, read-only tools with strict `--root` allowlisting and hard token caps:
|
|
193
|
+
- `doc_outline` (L0 map, ≤1.5k tok): structure, printed page range, and `route_hint`.
|
|
194
|
+
- `doc_locate` (L1 zero-LLM locator, ≤1.2k tok): lexical BM25-RRF + page-ref + continuation snippets.
|
|
195
|
+
- `doc_read` (L2 anchored reader, ≤5 pages): stable `p12:L07` anchors + session duplicate-read ledger + native MCP `ImageContent`.
|
|
196
|
+
- `doc_verify`: deterministic quote verification, page localization, AST math (`safe_calc`), and operand provenance.
|
|
197
|
+
|
|
198
|
+
### Progressive Context Engineering Ecosystem (Documents + Videos)
|
|
199
|
+
|
|
200
|
+
AnchorDoc shares the exact same **zero-vector, zero-server-LLM, token-budgeted progressive disclosure protocol** with our companion video context MCP server:
|
|
201
|
+
|
|
202
|
+
| Modality | Repository | Progressive Protocol (`L0 → L1 → L2 → Verify`) | Dependencies |
|
|
203
|
+
|---|---|---|---|
|
|
204
|
+
| **Long PDFs & Filings** | **⚓ [AnchorDoc](https://github.com/ryanvii/AnchorDoc)** *(this repo)* | `doc_outline` → `doc_locate` → `doc_read` → `doc_verify` | Pure Python + Apache-2.0 `pypdfium2` |
|
|
205
|
+
| **Long Videos & Demos** | **🎬 [vidctx-mcp](https://github.com/ryanvii/vidctx-mcp)** | `peek_video_map` → `search_video_moments` → `inspect_frame_range` | `yt-dlp` / `ffmpeg` / `RapidOCR` |
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
209
|
+
## Core Modules & Domain Adapters
|
|
210
|
+
|
|
211
|
+
- `anchordoc/docbase`: Thread-safe Apache-2.0 1-based `PageModel` (`pypdfium2`) + `PageLabels` + 2×2 high-res tile rendering + stdlib OOXML merged-cell table grid (`w:vMerge` / `w:gridSpan` inheritance, zero false-blanks).
|
|
212
|
+
- `anchordoc/store`: Per-document stdlib SQLite WAL `BundleStore` (layered `doc_sha256` / `extractor_fp` / `vision_fp` invalidation, **67× faster** warm reload, zero-render `VisionPageCard` reuse).
|
|
213
|
+
- `anchordoc/context.py` & `anchordoc/mcp_server.py`: Zero-LLM progressive `DocContext` API and 4-tool stdio MCP server.
|
|
214
|
+
- `anchordoc/index`: `Zero-LLM AnchorMap`, Printed-TOC (`G7`) offset calibrator & soft demotion, printed-page/slide reference resolver, `SelectiveVisionIndexer` (`VisionPageCard`), and `SpanCardIndex`.
|
|
215
|
+
- `anchordoc/retrieve`: Cost-derived 3-tier adaptive router (`read_all` | `full_pool` | `navigate`) + Query-Aware Single-Shot Locator (`_pack_snippets`) + continuation-table expansion + conditional decorrelated `B42` gap-followup + full `RetrievalTrace`.
|
|
216
|
+
- `anchordoc/extract`: Line-anchored `[sNNNNN]` requirement extraction with automatic verbatim quote-repair loop.
|
|
217
|
+
- `anchordoc/adjudicate`: Reasoning-first verifiable adjudicator with deterministic `AnswerContract`, authoritative Python AST arithmetic (`safe_calc`), Grounding Scope Guard, tri-state visual evidence gate (`stated | derivable | absent`), and deterministic truth tables (`decide_compliance`, `decide_nli`).
|
|
218
|
+
- `anchordoc/report`: JSON, Markdown, and zero-server interactive two-pane HTML trace workbench with embedded original PDF page previews.
|
|
219
|
+
- `adapters/`: Thin domain plugins — `finance_10k`, `legal_nda`, `multimodal_doc`, `procurement_demo`.
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
## Roadmap: Evidence Sufficiency (`v1.0`)
|
|
224
|
+
|
|
225
|
+
Beyond zero-index retrieval and deterministic claim verification, AnchorDoc's long-term north-star research problem is **Evidence Sufficiency**:
|
|
226
|
+
|
|
227
|
+
> *How does a long-document system know when the context it retrieved is truly sufficient to answer — and when it must read one more page before committing?*
|
|
228
|
+
|
|
229
|
+
- **Current (`v0.9.3`)**: Zero-LLM AnchorMap, continuation-table expansion, conditional gap-followup (`B42`), zero-LLM cross-reference gap detection (`detect_unresolved_references`), tri-state visual gate (`stated | derivable | absent`), and cell-bound verification (`VERIFIED | PARTIALLY_VERIFIED | FAILED | UNVERIFIED`).
|
|
230
|
+
- **Next (`v1.0`)**: Closed-loop **sufficiency-driven reading** across arbitrary 100–600 page filings — unifying cross-reference gaps, missing comparative periods, and undefined adjusted-metric footnotes directly inside the core retriever + MCP tool loop.
|
|
231
|
+
|
|
232
|
+
---
|
|
233
|
+
|
|
234
|
+
## Citation
|
|
235
|
+
|
|
236
|
+
If you use AnchorDoc or its reproducible benchmarks in your work, please cite via [CITATION.cff](CITATION.cff):
|
|
237
|
+
|
|
238
|
+
```bibtex
|
|
239
|
+
@software{wu2026anchordoc,
|
|
240
|
+
author = {Wu, HuiYang},
|
|
241
|
+
title = {AnchorDoc: Zero-Index-Cost Long-Document Context Engineering with Falsifiable Evidence Chains},
|
|
242
|
+
year = {2026},
|
|
243
|
+
version = {0.9.3},
|
|
244
|
+
doi = {10.5281/zenodo.24453581},
|
|
245
|
+
url = {https://github.com/ryanvii/AnchorDoc},
|
|
246
|
+
license = {Apache-2.0}
|
|
247
|
+
}
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
<!-- mcp-name: io.github.ryanvii/anchordoc -->
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from .finance_10k import Finance10KAdapter
|
|
2
|
+
from .legal_nda import LegalNDAAdapter
|
|
3
|
+
from .multimodal_doc import MultimodalDocAdapter
|
|
4
|
+
from .procurement_demo import ProcurementAuditAdapter
|
|
5
|
+
|
|
6
|
+
__all__ = [
|
|
7
|
+
"Finance10KAdapter",
|
|
8
|
+
"LegalNDAAdapter",
|
|
9
|
+
"MultimodalDocAdapter",
|
|
10
|
+
"ProcurementAuditAdapter",
|
|
11
|
+
]
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""Thin adapter for SEC 10-K / financial filing auditing (FinanceBench)."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from dataclasses import asdict
|
|
5
|
+
|
|
6
|
+
from anchordoc.adjudicate import FactAdjudicator
|
|
7
|
+
from anchordoc.docbase import PdfDocument
|
|
8
|
+
from anchordoc.retrieve import AnchorRetriever
|
|
9
|
+
from anchordoc.runtime import LLMClient
|
|
10
|
+
from anchordoc.types import AuditVerdict
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Finance10KAdapter:
|
|
14
|
+
"""End-to-end verifiable financial filing auditor over AnchorDoc core."""
|
|
15
|
+
|
|
16
|
+
def __init__(self, doc: PdfDocument, llm: LLMClient) -> None:
|
|
17
|
+
self.doc = doc
|
|
18
|
+
self.retriever = AnchorRetriever(doc, llm=llm)
|
|
19
|
+
self.adjudicator = FactAdjudicator(llm=llm)
|
|
20
|
+
|
|
21
|
+
def query(
|
|
22
|
+
self,
|
|
23
|
+
question: str,
|
|
24
|
+
item_id: str = "fin_1",
|
|
25
|
+
top_k: int = 5,
|
|
26
|
+
*,
|
|
27
|
+
route: str = "auto",
|
|
28
|
+
) -> AuditVerdict:
|
|
29
|
+
trace = self.retriever.retrieve(
|
|
30
|
+
question, top_k=top_k, use_anchor_map=True, use_gap_followup=True, route=route, task="qa_text" # type: ignore[arg-type]
|
|
31
|
+
)
|
|
32
|
+
verdict = self.adjudicator.answer_numerical(
|
|
33
|
+
self.doc, question, trace.pages, item_id=item_id, route=trace.route # type: ignore[arg-type]
|
|
34
|
+
)
|
|
35
|
+
verdict.retrieval_trace = asdict(trace)
|
|
36
|
+
return verdict
|