documa 0.1.0__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.
- documa-0.1.0/LICENSE +21 -0
- documa-0.1.0/PKG-INFO +400 -0
- documa-0.1.0/README.md +370 -0
- documa-0.1.0/pyproject.toml +41 -0
- documa-0.1.0/setup.cfg +4 -0
- documa-0.1.0/src/documa/__init__.py +41 -0
- documa-0.1.0/src/documa/adapters/__init__.py +7 -0
- documa-0.1.0/src/documa/adapters/base.py +31 -0
- documa-0.1.0/src/documa/adapters/markdown_adapter.py +258 -0
- documa-0.1.0/src/documa/adapters/pymupdf_adapter.py +781 -0
- documa-0.1.0/src/documa/cli.py +245 -0
- documa-0.1.0/src/documa/core/__init__.py +2 -0
- documa-0.1.0/src/documa/core/encoding.py +66 -0
- documa-0.1.0/src/documa/core/errors.py +45 -0
- documa-0.1.0/src/documa/core/image_filtering.py +71 -0
- documa-0.1.0/src/documa/core/ir.py +254 -0
- documa-0.1.0/src/documa/core/language.py +53 -0
- documa-0.1.0/src/documa/core/serialization.py +201 -0
- documa-0.1.0/src/documa/core/text_normalization.py +46 -0
- documa-0.1.0/src/documa/demo/__init__.py +5 -0
- documa-0.1.0/src/documa/demo/block_reading.py +494 -0
- documa-0.1.0/src/documa/exporters/__init__.py +9 -0
- documa-0.1.0/src/documa/exporters/base.py +30 -0
- documa-0.1.0/src/documa/exporters/block_json.py +125 -0
- documa-0.1.0/src/documa/exporters/json_exporter.py +18 -0
- documa-0.1.0/src/documa/exporters/markdown.py +75 -0
- documa-0.1.0/src/documa/exporters/rag_json.py +50 -0
- documa-0.1.0/src/documa/interfaces/__init__.py +38 -0
- documa-0.1.0/src/documa/interfaces/mcp_server.py +190 -0
- documa-0.1.0/src/documa/interfaces/tool_schemas.py +300 -0
- documa-0.1.0/src/documa/interfaces/tools.py +773 -0
- documa-0.1.0/src/documa/pipeline/__init__.py +40 -0
- documa-0.1.0/src/documa/pipeline/base.py +35 -0
- documa-0.1.0/src/documa/pipeline/block_keywords.py +279 -0
- documa-0.1.0/src/documa/pipeline/block_tree.py +290 -0
- documa-0.1.0/src/documa/pipeline/captions.py +123 -0
- documa-0.1.0/src/documa/pipeline/chunking.py +382 -0
- documa-0.1.0/src/documa/pipeline/footnotes.py +97 -0
- documa-0.1.0/src/documa/pipeline/images.py +66 -0
- documa-0.1.0/src/documa/pipeline/inline_semantics.py +56 -0
- documa-0.1.0/src/documa/pipeline/layout.py +177 -0
- documa-0.1.0/src/documa/pipeline/page_refs.py +105 -0
- documa-0.1.0/src/documa/pipeline/paragraphs.py +161 -0
- documa-0.1.0/src/documa/pipeline/provenance.py +41 -0
- documa-0.1.0/src/documa/pipeline/reading_order.py +110 -0
- documa-0.1.0/src/documa/pipeline/relations.py +63 -0
- documa-0.1.0/src/documa/pipeline/runner.py +84 -0
- documa-0.1.0/src/documa/pipeline/tables.py +80 -0
- documa-0.1.0/src/documa/pipeline/toc.py +125 -0
- documa-0.1.0/src/documa/quality/__init__.py +16 -0
- documa-0.1.0/src/documa/quality/benchmark.py +121 -0
- documa-0.1.0/src/documa/quality/doctor.py +100 -0
- documa-0.1.0/src/documa/quality/fixture_manifest.py +68 -0
- documa-0.1.0/src/documa/storage/__init__.py +6 -0
- documa-0.1.0/src/documa/storage/assets.py +49 -0
- documa-0.1.0/src/documa.egg-info/PKG-INFO +400 -0
- documa-0.1.0/src/documa.egg-info/SOURCES.txt +77 -0
- documa-0.1.0/src/documa.egg-info/dependency_links.txt +1 -0
- documa-0.1.0/src/documa.egg-info/entry_points.txt +3 -0
- documa-0.1.0/src/documa.egg-info/requires.txt +13 -0
- documa-0.1.0/src/documa.egg-info/top_level.txt +1 -0
- documa-0.1.0/tests/test_encoding.py +39 -0
- documa-0.1.0/tests/test_fixture_manifest.py +24 -0
- documa-0.1.0/tests/test_interfaces_and_cli.py +105 -0
- documa-0.1.0/tests/test_ir_models.py +67 -0
- documa-0.1.0/tests/test_language_and_normalization.py +22 -0
- documa-0.1.0/tests/test_markdown_query_model.py +92 -0
- documa-0.1.0/tests/test_pdf_chat_like_example.py +640 -0
- documa-0.1.0/tests/test_pdf_chat_like_web_example.py +128 -0
- documa-0.1.0/tests/test_pipeline_stage3.py +325 -0
- documa-0.1.0/tests/test_pipeline_stage4.py +145 -0
- documa-0.1.0/tests/test_pipeline_stage5.py +214 -0
- documa-0.1.0/tests/test_pymupdf_adapter.py +218 -0
- documa-0.1.0/tests/test_stage10_block_reading.py +379 -0
- documa-0.1.0/tests/test_stage11_block_demo.py +76 -0
- documa-0.1.0/tests/test_stage6_tool_interfaces.py +105 -0
- documa-0.1.0/tests/test_stage7_benchmark.py +63 -0
- documa-0.1.0/tests/test_stage8_release_readiness.py +49 -0
- documa-0.1.0/tests/test_stage9_processing_pipeline.py +116 -0
documa-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Documa contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
documa-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: documa
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: LLM-ready document understanding package.
|
|
5
|
+
Author: Documa contributors
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Repository, https://github.com/AllanYiin/Documa
|
|
8
|
+
Project-URL: Documentation, https://github.com/AllanYiin/Documa#readme
|
|
9
|
+
Keywords: pdf,document-understanding,rag,mcp,llm
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Topic :: Text Processing :: Markup
|
|
17
|
+
Requires-Python: >=3.10
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: pytest>=8; extra == "dev"
|
|
22
|
+
Provides-Extra: pdf
|
|
23
|
+
Requires-Dist: PyMuPDF>=1.24; extra == "pdf"
|
|
24
|
+
Provides-Extra: mcp
|
|
25
|
+
Requires-Dist: mcp>=1.0; extra == "mcp"
|
|
26
|
+
Provides-Extra: demo
|
|
27
|
+
Requires-Dist: tiktoken>=0.7; extra == "demo"
|
|
28
|
+
Requires-Dist: openai>=1.0; extra == "demo"
|
|
29
|
+
Dynamic: license-file
|
|
30
|
+
|
|
31
|
+
# Documa
|
|
32
|
+
|
|
33
|
+
## 概覽
|
|
34
|
+
|
|
35
|
+
Documa 是一個以 Python 套件為核心、面向 LLM 的文件理解套件。它的目標不是取代底層 PDF parser,而是把 parser 產出的低階訊號整理成穩定、可追溯、可被 agent、RAG、MCP 與 tool-calling 工作流使用的文件中介表示。
|
|
36
|
+
|
|
37
|
+
目前定位是 Alpha 階段的開發者套件:
|
|
38
|
+
|
|
39
|
+
- 核心是 Python package,不在核心 repo 內建產品 UI。
|
|
40
|
+
- PDF parsing 透過 adapter 接入,目前包含 `PyMuPDFAdapter`;core 不直接依賴 parser-native object。
|
|
41
|
+
- 內部文字使用 Python Unicode `str`;JSON 與檔案輸出預設 UTF-8,JSON 使用 `ensure_ascii=False`。
|
|
42
|
+
- 保留原始文字與正規化文字,不靜默覆蓋原文。
|
|
43
|
+
- CLI、Python tool layer、OpenAI tool schema 與 MCP wrapper 共用同一組結構化結果契約。
|
|
44
|
+
|
|
45
|
+
## 為什麼需要 Documa
|
|
46
|
+
|
|
47
|
+
一般 PDF parser 多半負責「從 PDF 拿到文字、座標、圖片或表格候選」。這很重要,但對 LLM 應用通常還不夠:PDF 的自然閱讀順序、段落、頁首頁尾、表格脈絡、註腳、圖片 caption、RAG chunk provenance,以及 agent 需要的漸進式讀取介面,都需要額外的整理層。
|
|
48
|
+
|
|
49
|
+
Documa 補的是這一層。
|
|
50
|
+
|
|
51
|
+
```mermaid
|
|
52
|
+
flowchart LR
|
|
53
|
+
A["PDF / Markdown"] --> B["Parser adapter"]
|
|
54
|
+
B --> C["Parser-neutral Documa IR"]
|
|
55
|
+
C --> D["理解 pipeline"]
|
|
56
|
+
D --> E["輸出: JSON / Markdown / RAG JSON / block JSON"]
|
|
57
|
+
D --> F["介面: CLI / Python tools / OpenAI schemas / MCP"]
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## 與一般 PDF parser 的差異
|
|
61
|
+
|
|
62
|
+
| 面向 | 一般 PDF parser | Documa |
|
|
63
|
+
| --- | --- | --- |
|
|
64
|
+
| 邊界 | 直接暴露 parser API 或 parser-specific result | 透過 adapter 轉成 parser-neutral `DocumentIR` |
|
|
65
|
+
| 文字處理 | 常只輸出 parser text | 同時保留 `raw_text` 與 `normalized_text` |
|
|
66
|
+
| 版面理解 | 提供座標、block、span 等低階訊號 | 在 pipeline 裡建立 reading order、paragraph、layout class、table/image normalization |
|
|
67
|
+
| 可追溯性 | 下游自行追頁碼與座標 | IR 保存 page refs、bbox refs、source refs、relations、metadata |
|
|
68
|
+
| LLM ingestion | 通常輸出純文字或簡單 chunk | 產生 RAG chunk、block tree、keyword metadata、provenance |
|
|
69
|
+
| Agent 讀取 | 常需要一次餵完整文件 | 提供 `list/search/read block` 的 progressive reading tools |
|
|
70
|
+
| 整合方式 | 綁定特定 library | 同一能力可透過 CLI、Python tool layer、OpenAI function schema、MCP wrapper 使用 |
|
|
71
|
+
| 品質門檻 | 多靠手動測試 | 內建 `doctor`、fixture benchmark 與 regression tests |
|
|
72
|
+
|
|
73
|
+
Documa 對 PDF parser 的態度是「adapter-based composition」:底層 parser 專注 extraction,Documa 專注把 extraction result 變成穩定、可維護、可供 LLM 使用的文件理解資料層。
|
|
74
|
+
|
|
75
|
+
## 核心概念
|
|
76
|
+
|
|
77
|
+
- `DocumentIR`:Documa 的 parser-neutral source of truth,定義在 `src/documa/core/ir.py`。
|
|
78
|
+
- Adapter:把外部格式轉成 `DocumentIR`,例如 `PyMuPDFAdapter` 與 `MarkdownAdapter`。
|
|
79
|
+
- Pipeline stage:對 IR 做保守、可測試的轉換,例如 reading order、inline semantics、paragraph grouping、table normalization、relations、block tree、chunking。
|
|
80
|
+
- Relation:用來表達 footnote、TOC、caption、provenance 等可追溯連結;不確定時保留 unresolved evidence,而不是假裝成功。
|
|
81
|
+
- Document block:面向 agent 的 progressive disclosure tree。agent 可以先看 metadata,再只讀相關 section、paragraph、table 或 image block。
|
|
82
|
+
- Exporter:把 IR 輸出成 `json`、`markdown`、`rag-json` 或 `block-json`。
|
|
83
|
+
- Tool layer:`documa_parse`、`documa_process`、`documa_search_blocks` 等工具共用同一組 structured result。
|
|
84
|
+
|
|
85
|
+
## 快速開始
|
|
86
|
+
|
|
87
|
+
以下命令以 PowerShell 為例。若在 macOS/Linux,虛擬環境啟動指令可改成 `source .venv/bin/activate`。
|
|
88
|
+
|
|
89
|
+
### 前置條件(Prerequisites / Requirements)
|
|
90
|
+
|
|
91
|
+
- Python 3.10 或更新版本。
|
|
92
|
+
- 這個 repo 的 checkout,或已安裝的 `documa` package。
|
|
93
|
+
- 若要透過 `PyMuPDFAdapter` 處理 PDF,需安裝 `pdf` extra。
|
|
94
|
+
- 若要使用 MCP 或 demo 整合,需視情況安裝 `mcp` 與 `demo` extras。
|
|
95
|
+
|
|
96
|
+
### 1. 從 package index 安裝
|
|
97
|
+
|
|
98
|
+
發布到 PyPI 或相容的 private index 後,可直接安裝 runtime package:
|
|
99
|
+
|
|
100
|
+
```powershell
|
|
101
|
+
python -m pip install "documa[pdf]"
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
若只需要 Markdown adapter、IR、pipeline、exporters 與 tool schema,可省略 `pdf` extra:
|
|
105
|
+
|
|
106
|
+
```powershell
|
|
107
|
+
python -m pip install documa
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### 2. 從 Git repo 安裝
|
|
111
|
+
|
|
112
|
+
尚未發布到 package index,或要安裝特定 Git revision 時,可透過 pip 的 direct URL 安裝:
|
|
113
|
+
|
|
114
|
+
```powershell
|
|
115
|
+
python -m pip install "documa[pdf] @ git+https://github.com/AllanYiin/Documa.git"
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### 3. 從本地 wheel 安裝
|
|
119
|
+
|
|
120
|
+
在 repo checkout 內先建立 distribution artifacts:
|
|
121
|
+
|
|
122
|
+
```powershell
|
|
123
|
+
python -m pip install --upgrade build
|
|
124
|
+
python -m build
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
接著安裝產出的 wheel:
|
|
128
|
+
|
|
129
|
+
```powershell
|
|
130
|
+
python -m pip install ".\dist\documa-0.1.0-py3-none-any.whl[pdf]"
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### 4. 從 repo checkout 做開發安裝
|
|
134
|
+
|
|
135
|
+
```powershell
|
|
136
|
+
git clone https://github.com/AllanYiin/Documa.git
|
|
137
|
+
cd Documa
|
|
138
|
+
|
|
139
|
+
python -m venv .venv
|
|
140
|
+
.\.venv\Scripts\Activate.ps1
|
|
141
|
+
python -m pip install --upgrade pip
|
|
142
|
+
python -m pip install -e ".[dev,pdf]"
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
若要一次安裝完整整合面:
|
|
146
|
+
|
|
147
|
+
```powershell
|
|
148
|
+
python -m pip install -e ".[dev,pdf,mcp,demo]"
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
可選 extras:
|
|
152
|
+
|
|
153
|
+
| Extra | 用途 |
|
|
154
|
+
| --- | --- |
|
|
155
|
+
| `pdf` | 安裝 PyMuPDF,讓 `PyMuPDFAdapter` 可處理 PDF。 |
|
|
156
|
+
| `mcp` | 安裝 `documa-mcp` 需要的 MCP 依賴。 |
|
|
157
|
+
| `demo` | 安裝 demo 需要的選用依賴,例如 token counting 與 OpenAI SDK support。 |
|
|
158
|
+
| `dev` | 安裝測試依賴。 |
|
|
159
|
+
|
|
160
|
+
### 5. 驗證環境
|
|
161
|
+
|
|
162
|
+
```powershell
|
|
163
|
+
documa doctor
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
預期結果:回傳結構化 JSON 診斷。缺少選用整合時會以 warning 呈現,與 core package failure 分開。
|
|
167
|
+
|
|
168
|
+
如果 console script 還不能使用,可改用 module 入口:
|
|
169
|
+
|
|
170
|
+
```powershell
|
|
171
|
+
$env:PYTHONPATH="src"
|
|
172
|
+
python -m documa.cli doctor
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### 6. 處理 PDF
|
|
176
|
+
|
|
177
|
+
```powershell
|
|
178
|
+
documa process "<path-to-report.pdf>" `
|
|
179
|
+
--out ".\out\report" `
|
|
180
|
+
--lang zh-Hant,en `
|
|
181
|
+
--export-format block-json
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
預期輸出:
|
|
185
|
+
|
|
186
|
+
- `.\out\report\documa.ir.json`:完整 Documa IR。
|
|
187
|
+
- `.\out\report\documa.rag.json`:帶有來源 metadata 的 retrieval chunks。
|
|
188
|
+
- `.\out\report\documa.blocks.json`:漸進式讀取用的 block tree。
|
|
189
|
+
- `.\out\report\assets\...`:可用時輸出 page preview 或抽取出的 assets。
|
|
190
|
+
|
|
191
|
+
`process` 是高階 ingestion 指令,會執行 parse 加上預設理解 pipeline。若只需要 adapter boundary,可使用 `parse`:
|
|
192
|
+
|
|
193
|
+
```powershell
|
|
194
|
+
documa parse "<path-to-report.pdf>" --out ".\out\parsed" --lang zh-Hant,en
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### 7. 像 agent 一樣讀文件
|
|
198
|
+
|
|
199
|
+
完成 `process` 後,可以先檢查與搜尋 logical blocks,再只讀相關內容:
|
|
200
|
+
|
|
201
|
+
```powershell
|
|
202
|
+
documa blocks ".\out\report\documa.ir.json"
|
|
203
|
+
documa search-blocks ".\out\report\documa.ir.json" --query "主要風險"
|
|
204
|
+
documa block ".\out\report\documa.ir.json" --id "<block-id>"
|
|
205
|
+
documa block ".\out\report\documa.ir.json" --id "<block-id>" --read
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
這是主要的 LLM-facing 使用模式:先 list 或 search metadata,再載入選定 block body 與 provenance。
|
|
209
|
+
|
|
210
|
+
### 8. 輸出給下游系統
|
|
211
|
+
|
|
212
|
+
```powershell
|
|
213
|
+
documa export ".\out\report\documa.ir.json" --format markdown --out ".\out\report\documa.md"
|
|
214
|
+
documa export ".\out\report\documa.ir.json" --format rag-json --out ".\out\report\documa.rag.json"
|
|
215
|
+
documa export ".\out\report\documa.ir.json" --format block-json --out ".\out\report\documa.blocks.json"
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
支援格式:
|
|
219
|
+
|
|
220
|
+
- `json`:完整 Documa IR。
|
|
221
|
+
- `markdown`:帶 page markers 的可讀 Markdown。
|
|
222
|
+
- `rag-json`:供 retrieval ingestion 使用的 chunk records。
|
|
223
|
+
- `block-json`:供 progressive reading workflow 使用的 document block tree。
|
|
224
|
+
|
|
225
|
+
### 9. 使用 tool schemas 或 MCP
|
|
226
|
+
|
|
227
|
+
列出 Documa tools:
|
|
228
|
+
|
|
229
|
+
```powershell
|
|
230
|
+
documa tools
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
在 Python 中取得 OpenAI-compatible function tool descriptors:
|
|
234
|
+
|
|
235
|
+
```python
|
|
236
|
+
from documa.interfaces import openai_tool_schemas
|
|
237
|
+
|
|
238
|
+
tools = openai_tool_schemas(strict=True)
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
直接呼叫共用 tool layer:
|
|
242
|
+
|
|
243
|
+
```python
|
|
244
|
+
from documa.interfaces import call_documa_tool
|
|
245
|
+
|
|
246
|
+
result = call_documa_tool(
|
|
247
|
+
"documa_process",
|
|
248
|
+
{
|
|
249
|
+
"source": "report.pdf",
|
|
250
|
+
"out": "out/report",
|
|
251
|
+
"languages": ["zh-Hant", "en"],
|
|
252
|
+
"export_formats": ["rag-json", "block-json"],
|
|
253
|
+
},
|
|
254
|
+
)
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
啟動選用 MCP server:
|
|
258
|
+
|
|
259
|
+
```powershell
|
|
260
|
+
python -m pip install -e ".[mcp]"
|
|
261
|
+
documa-mcp
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
### 10. 執行測試
|
|
265
|
+
|
|
266
|
+
```powershell
|
|
267
|
+
python -m pytest
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
若沒有安裝 pytest,也可使用 unittest:
|
|
271
|
+
|
|
272
|
+
```powershell
|
|
273
|
+
python -m unittest discover -s tests
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
## 使用方式
|
|
277
|
+
|
|
278
|
+
- CLI ingestion:使用 `documa process` 走預設 parser-plus-pipeline 路徑。
|
|
279
|
+
- Python library usage:把 Documa 嵌入其他 package 時,可直接呼叫 adapters 與 pipeline stages。
|
|
280
|
+
- Tool-calling usage:由 agent runtime 負責執行工具時,使用 `call_documa_tool()` 或 `openai_tool_schemas()`。
|
|
281
|
+
- MCP usage:需要讓 MCP host discover Documa tools 時,執行 `documa-mcp`。
|
|
282
|
+
- Example usage:要建立下游 app 時,可從 `examples/pdf_chat_like/` 或 `examples/pdf_chat_like_web/` 開始。
|
|
283
|
+
|
|
284
|
+
## 專案結構
|
|
285
|
+
|
|
286
|
+
| 路徑 | 用途 |
|
|
287
|
+
| --- | --- |
|
|
288
|
+
| `src/documa/core/` | IR models、serialization、encoding、language 與 text normalization utilities。 |
|
|
289
|
+
| `src/documa/adapters/` | Parser adapters。外部 parser objects 不應跨出此邊界。 |
|
|
290
|
+
| `src/documa/pipeline/` | Parser-neutral understanding stages 與 default pipeline orchestration。 |
|
|
291
|
+
| `src/documa/exporters/` | JSON、Markdown、RAG JSON 與 block JSON exporters。 |
|
|
292
|
+
| `src/documa/interfaces/` | Shared tool functions、JSON schemas、OpenAI schema wrapper 與 MCP server wrapper。 |
|
|
293
|
+
| `src/documa/quality/` | Doctor checks 與 fixture benchmark support。 |
|
|
294
|
+
| `examples/` | 使用 public package surface 建立的可執行 integration examples。這些是 examples,不是核心 UI。 |
|
|
295
|
+
| `fixtures/pdf/` | PDF parsing risk coverage 的 fixture manifest。 |
|
|
296
|
+
| `docs/documa/` | Architecture notes 與 expert review log。 |
|
|
297
|
+
| `tests/` | IR、pipeline、adapters、CLI 與 tool interfaces 的 unit / regression tests。 |
|
|
298
|
+
|
|
299
|
+
## 以開發者方式使用 Documa
|
|
300
|
+
|
|
301
|
+
### 使用 Python API
|
|
302
|
+
|
|
303
|
+
```python
|
|
304
|
+
from documa.adapters.base import ParseOptions
|
|
305
|
+
from documa.adapters.pymupdf_adapter import PyMuPDFAdapter
|
|
306
|
+
from documa.pipeline.runner import run_default_pipeline
|
|
307
|
+
|
|
308
|
+
document = PyMuPDFAdapter().parse(
|
|
309
|
+
"report.pdf",
|
|
310
|
+
ParseOptions(languages=["zh-Hant", "en"]),
|
|
311
|
+
)
|
|
312
|
+
pipeline_run = run_default_pipeline(document)
|
|
313
|
+
processed_document = pipeline_run.document
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
### 新增 parser adapter
|
|
317
|
+
|
|
318
|
+
新增 parser integration 時:
|
|
319
|
+
|
|
320
|
+
1. 實作 `ParserAdapter`。
|
|
321
|
+
2. Adapter 只回傳 Documa IR objects。
|
|
322
|
+
3. Parser-native objects 必須留在 adapter boundary 內。
|
|
323
|
+
4. 原始文字與正規化文字要分開保留。
|
|
324
|
+
5. 補上 repair loops 需要的 page refs、bbox refs、source refs 與 metadata。
|
|
325
|
+
6. 為 adapter 與它啟用的新 pipeline behavior 加測試。
|
|
326
|
+
|
|
327
|
+
### 新增 public tool 或物件
|
|
328
|
+
|
|
329
|
+
新增公開能力時,需同步考慮完整 lifecycle:
|
|
330
|
+
|
|
331
|
+
- create/update/delete/state behavior,若該能力適用;
|
|
332
|
+
- CLI surface;
|
|
333
|
+
- Python tool function;
|
|
334
|
+
- MCP wrapper;
|
|
335
|
+
- tool-calling schema;
|
|
336
|
+
- structured success 與 error payloads;
|
|
337
|
+
- regression tests。
|
|
338
|
+
|
|
339
|
+
## 範例
|
|
340
|
+
|
|
341
|
+
`examples/pdf_chat_like/` 示範 CLI-first 的 PDF progressive reading workflow:
|
|
342
|
+
|
|
343
|
+
```powershell
|
|
344
|
+
$env:PYTHONPATH="src"
|
|
345
|
+
python examples\pdf_chat_like\pdf_chat_example.py "<path-to-report.pdf>" `
|
|
346
|
+
--question "這份文件的主要風險是什麼?" `
|
|
347
|
+
--out ".\out\documa-pdf-chat"
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
`examples/pdf_chat_like_web/` 示範 local browser UI 如何呼叫 Documa,同時讓 Documa core 維持 UI-free:
|
|
351
|
+
|
|
352
|
+
```powershell
|
|
353
|
+
$env:PYTHONPATH="src"
|
|
354
|
+
python examples\pdf_chat_like_web\server.py --port 8765
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
接著開啟 `http://127.0.0.1:8765`。
|
|
358
|
+
|
|
359
|
+
## 品質門檻
|
|
360
|
+
|
|
361
|
+
發布或改動 public behavior 前,建議先跑:
|
|
362
|
+
|
|
363
|
+
```powershell
|
|
364
|
+
documa doctor
|
|
365
|
+
documa benchmark
|
|
366
|
+
python -m pytest
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
如果 release gate 要求每個宣告的 fixture PDF 都必須存在,使用 `--require-files`:
|
|
370
|
+
|
|
371
|
+
```powershell
|
|
372
|
+
documa benchmark --require-files --out ".\out\documa-benchmark.json"
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
## 已知限制
|
|
376
|
+
|
|
377
|
+
- Documa core 不是 UI product。UI code 應留在 examples 或 downstream applications。
|
|
378
|
+
- Documa 不從零實作 PDF parser。PDF extraction 透過 adapters 委派給底層 parser。
|
|
379
|
+
- 目前 pipeline stages 是保守 baseline,不是完美的 document understanding model。
|
|
380
|
+
- OCR-only 或 image-only PDFs 需要 core 假設以外的 parser/OCR 支援。
|
|
381
|
+
- 選用 LLM usage 只出現在 demos 或 downstream integrations。Core parsing 與 processing 是 deterministic,可離線執行。
|
|
382
|
+
|
|
383
|
+
## 疑難排解
|
|
384
|
+
|
|
385
|
+
| 症狀 | 可能原因 | 修正方式 |
|
|
386
|
+
| --- | --- | --- |
|
|
387
|
+
| `PyMuPDF is required for PyMuPDFAdapter.` | 尚未安裝 `pdf` extra。 | 在 repo checkout 內執行 `python -m pip install -e ".[pdf]"`。 |
|
|
388
|
+
| 找不到 `documa` 指令。 | Virtual environment 未啟用,或 package 尚未 editable install。 | 啟用 `.venv`,重新執行 `python -m pip install -e ".[dev,pdf]"`,或改跑 `$env:PYTHONPATH="src"; python -m documa.cli ...`。 |
|
|
389
|
+
| `documa-mcp` 無法 import MCP modules。 | 尚未安裝 `mcp` extra。 | 執行 `python -m pip install -e ".[mcp]"`。 |
|
|
390
|
+
| PDF 結果的 reading order 不如預期。 | PDF text order 受來源 PDF 的產生方式影響。 | 檢查 `documa.ir.json`,使用 block search/read tools,並在改 pipeline heuristics 前補 fixture coverage。 |
|
|
391
|
+
|
|
392
|
+
## 上游背景
|
|
393
|
+
|
|
394
|
+
以下上游概念是 Documa 邊界設計的依據:
|
|
395
|
+
|
|
396
|
+
- PyMuPDF 文件說明,plain PDF text extraction 不一定保留 natural reading order,而 block/word extraction 會帶有可用於 layout repair 的 position information:[PyMuPDF text recipes](https://pymupdf.readthedocs.io/en/latest/recipes-text.html)。
|
|
397
|
+
- OpenAI function calling 使用 JSON-schema-defined tools,並由 application side 執行 tool call:[OpenAI function calling guide](https://developers.openai.com/api/docs/guides/function-calling)。
|
|
398
|
+
- MCP tools 回傳 content 與 structured results,並支援 structured output schemas:[MCP tools specification](https://modelcontextprotocol.io/specification/2025-06-18/server/tools)。
|
|
399
|
+
|
|
400
|
+
如果你要改 Documa 的 integration layer,請先重新核對目前的 upstream docs,再變更 schemas 或 protocol behavior。
|