|
| 1 | +# Dynamic workflow: extract → query → act |
| 2 | + |
| 3 | +This example shows how an AI agent chains the Data Extraction tools with the |
| 4 | +existing document tools to process an invoice **without ever loading the full |
| 5 | +extraction into context**. It is the pattern dynamic workflows are built on: |
| 6 | +extract structured data, branch on it, then act. |
| 7 | + |
| 8 | +**Prerequisites** |
| 9 | + |
| 10 | +- `NUTRIENT_EXTRACTION_API_KEY` (Data Extraction API key, starts with `pdf_live_`) for `data_extractor`. |
| 11 | +- `NUTRIENT_DWS_API_KEY` (or OAuth) for the `ai_redactor` / `document_signer` "act" steps. |
| 12 | +- `SANDBOX_PATH` set to a directory containing `invoice.pdf`. |
| 13 | + |
| 14 | +## Step 1 — Extract structured elements to a file |
| 15 | + |
| 16 | +The agent calls `data_extractor` in `understand` mode with spatial output. The |
| 17 | +element list (with coordinates and confidence) is written to a file; only a |
| 18 | +compact summary comes back. |
| 19 | + |
| 20 | +```jsonc |
| 21 | +// tool: data_extractor |
| 22 | +{ "filePath": "invoice.pdf", "mode": "understand", "format": "spatial", "outputPath": "invoice.elements.json" } |
| 23 | +``` |
| 24 | + |
| 25 | +``` |
| 26 | +Extracted 142 elements across 2 page(s) and wrote the full spatial JSON to invoice.elements.json (38217 bytes). |
| 27 | +Element types: paragraph: 96, table: 2, keyValueRegion: 18, picture: 1. |
| 28 | +Low-confidence elements (confidence < 0.6): 7. |
| 29 | +Retrieve specific elements with query_extraction ... |
| 30 | +``` |
| 31 | + |
| 32 | +The agent now knows the shape of the document — and that **7 fields are |
| 33 | +low-confidence** — without 142 elements entering the conversation. |
| 34 | + |
| 35 | +## Step 2 — Branch on the result with `query_extraction` |
| 36 | + |
| 37 | +The summary flagged low-confidence elements, so the agent pulls just those to |
| 38 | +decide whether the document needs human review: |
| 39 | + |
| 40 | +```jsonc |
| 41 | +// tool: query_extraction |
| 42 | +{ "filePath": "invoice.elements.json", "minConfidence": 0, "elementTypes": ["keyValueRegion"], "limit": 50 } |
| 43 | +``` |
| 44 | + |
| 45 | +It can also grab a specific region — e.g. the totals box in the bottom-right of |
| 46 | +page 2 — to read the amount due: |
| 47 | + |
| 48 | +```jsonc |
| 49 | +// tool: query_extraction |
| 50 | +{ "filePath": "invoice.elements.json", "pages": [1], "region": { "x": 1200, "y": 2000, "width": 600, "height": 400 } } |
| 51 | +``` |
| 52 | + |
| 53 | +Only the handful of elements the agent actually needs — with their text and |
| 54 | +coordinates — enter context. |
| 55 | + |
| 56 | +## Step 3 — Act with the existing tools |
| 57 | + |
| 58 | +Branching on what it found, the agent acts: |
| 59 | + |
| 60 | +- **Low-confidence or sensitive fields →** redact before sharing: |
| 61 | + |
| 62 | + ```jsonc |
| 63 | + // tool: ai_redactor |
| 64 | + { "filePath": "invoice.pdf", "criteria": "Bank account and routing numbers", "outputPath": "invoice-redacted.pdf" } |
| 65 | + ``` |
| 66 | + |
| 67 | +- **Clean and approved →** sign it: |
| 68 | + |
| 69 | + ```jsonc |
| 70 | + // tool: document_signer |
| 71 | + { "filePath": "invoice.pdf", "outputPath": "invoice-signed.pdf", "signatureOptions": { "signatureType": "cms" } } |
| 72 | + ``` |
| 73 | + |
| 74 | +## Why this is the workflow primitive |
| 75 | + |
| 76 | +The agent reasons over **structure and coordinates** (counts, confidence, |
| 77 | +regions) rather than a wall of text, retrieves only the slices it needs, and |
| 78 | +hands off to deterministic document operations. The large, sensitive payload |
| 79 | +stays on disk; the conversation stays small and auditable. |
0 commit comments