llm-large-files 0.5.0 → 0.8.0
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.
- package/CHANGELOG.md +1719 -466
- package/README.md +190 -60
- package/dist/analyze.d.ts +8 -1
- package/dist/analyze.d.ts.map +1 -1
- package/dist/analyze.js +198 -54
- package/dist/analyze.js.map +1 -1
- package/dist/anthropic/adapter.d.ts +14 -3
- package/dist/anthropic/adapter.d.ts.map +1 -1
- package/dist/anthropic/adapter.js +194 -40
- package/dist/anthropic/adapter.js.map +1 -1
- package/dist/ask.d.ts +63 -7
- package/dist/ask.d.ts.map +1 -1
- package/dist/ask.js +1131 -214
- package/dist/ask.js.map +1 -1
- package/dist/decode.d.ts +19 -0
- package/dist/decode.d.ts.map +1 -0
- package/dist/decode.js +97 -0
- package/dist/decode.js.map +1 -0
- package/dist/encoding.d.ts +17 -0
- package/dist/encoding.d.ts.map +1 -0
- package/dist/encoding.js +852 -0
- package/dist/encoding.js.map +1 -0
- package/dist/errors.d.ts +59 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +80 -0
- package/dist/errors.js.map +1 -1
- package/dist/events.d.ts +339 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +163 -0
- package/dist/events.js.map +1 -0
- package/dist/evidence.d.ts +2 -0
- package/dist/evidence.d.ts.map +1 -1
- package/dist/evidence.js +9 -2
- package/dist/evidence.js.map +1 -1
- package/dist/extract.d.ts +26 -7
- package/dist/extract.d.ts.map +1 -1
- package/dist/extract.js +180 -314
- package/dist/extract.js.map +1 -1
- package/dist/gemini/adapter.d.ts +17 -8
- package/dist/gemini/adapter.d.ts.map +1 -1
- package/dist/gemini/adapter.js +117 -66
- package/dist/gemini/adapter.js.map +1 -1
- package/dist/gemini/chat.d.ts +23 -3
- package/dist/gemini/chat.d.ts.map +1 -1
- package/dist/gemini/chat.js +6 -2
- package/dist/gemini/chat.js.map +1 -1
- package/dist/gemini/endpoints.d.ts +5 -1
- package/dist/gemini/endpoints.d.ts.map +1 -1
- package/dist/gemini/endpoints.js +11 -2
- package/dist/gemini/endpoints.js.map +1 -1
- package/dist/gemini/errors.d.ts +9 -4
- package/dist/gemini/errors.d.ts.map +1 -1
- package/dist/gemini/errors.js +32 -0
- package/dist/gemini/errors.js.map +1 -1
- package/dist/gemini/generate.d.ts +10 -2
- package/dist/gemini/generate.d.ts.map +1 -1
- package/dist/gemini/generate.js +33 -10
- package/dist/gemini/generate.js.map +1 -1
- package/dist/gemini/index.d.ts +3 -3
- package/dist/gemini/index.d.ts.map +1 -1
- package/dist/gemini/index.js +2 -2
- package/dist/gemini/index.js.map +1 -1
- package/dist/gemini/sse.d.ts +1 -1
- package/dist/gemini/sse.d.ts.map +1 -1
- package/dist/gemini/sse.js +1 -1
- package/dist/gemini/sse.js.map +1 -1
- package/dist/groups.d.ts +54 -0
- package/dist/groups.d.ts.map +1 -0
- package/dist/groups.js +136 -0
- package/dist/groups.js.map +1 -0
- package/dist/html.d.ts +11 -0
- package/dist/html.d.ts.map +1 -0
- package/dist/html.js +1112 -0
- package/dist/html.js.map +1 -0
- package/dist/http.d.ts +116 -0
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +347 -1
- package/dist/http.js.map +1 -1
- package/dist/index.d.ts +13 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -5
- package/dist/index.js.map +1 -1
- package/dist/library.d.ts +67 -5
- package/dist/library.d.ts.map +1 -1
- package/dist/library.js +449 -93
- package/dist/library.js.map +1 -1
- package/dist/limiter.d.ts +2 -1
- package/dist/limiter.d.ts.map +1 -1
- package/dist/limiter.js +3 -2
- package/dist/limiter.js.map +1 -1
- package/dist/map-reduce.d.ts +28 -1
- package/dist/map-reduce.d.ts.map +1 -1
- package/dist/map-reduce.js +240 -52
- package/dist/map-reduce.js.map +1 -1
- package/dist/model.d.ts +84 -2
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +190 -2
- package/dist/model.js.map +1 -1
- package/dist/node/handlers.d.ts +20 -4
- package/dist/node/handlers.d.ts.map +1 -1
- package/dist/node/handlers.js +184 -28
- package/dist/node/handlers.js.map +1 -1
- package/dist/node/index.d.ts +4 -0
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +2 -0
- package/dist/node/index.js.map +1 -1
- package/dist/node/ingest-child.d.ts +38 -0
- package/dist/node/ingest-child.d.ts.map +1 -0
- package/dist/node/ingest-child.js +42 -0
- package/dist/node/ingest-child.js.map +1 -0
- package/dist/node/isolated.d.ts +88 -0
- package/dist/node/isolated.d.ts.map +1 -0
- package/dist/node/isolated.js +381 -0
- package/dist/node/isolated.js.map +1 -0
- package/dist/node/sse.d.ts +59 -0
- package/dist/node/sse.d.ts.map +1 -0
- package/dist/node/sse.js +174 -0
- package/dist/node/sse.js.map +1 -0
- package/dist/office.d.ts +105 -0
- package/dist/office.d.ts.map +1 -0
- package/dist/office.js +1092 -0
- package/dist/office.js.map +1 -0
- package/dist/openai/adapter.d.ts +29 -5
- package/dist/openai/adapter.d.ts.map +1 -1
- package/dist/openai/adapter.js +325 -37
- package/dist/openai/adapter.js.map +1 -1
- package/dist/output-limit.d.ts +14 -2
- package/dist/output-limit.d.ts.map +1 -1
- package/dist/output-limit.js +39 -10
- package/dist/output-limit.js.map +1 -1
- package/dist/pause.d.ts +24 -0
- package/dist/pause.d.ts.map +1 -0
- package/dist/pause.js +38 -0
- package/dist/pause.js.map +1 -0
- package/dist/pdf-pages.d.ts +32 -2
- package/dist/pdf-pages.d.ts.map +1 -1
- package/dist/pdf-pages.js +277 -20
- package/dist/pdf-pages.js.map +1 -1
- package/dist/pdf-pictures.d.ts +30 -3
- package/dist/pdf-pictures.d.ts.map +1 -1
- package/dist/pdf-pictures.js +50 -8
- package/dist/pdf-pictures.js.map +1 -1
- package/dist/pdf-work.d.ts +40 -0
- package/dist/pdf-work.d.ts.map +1 -0
- package/dist/pdf-work.js +734 -0
- package/dist/pdf-work.js.map +1 -0
- package/dist/peers.d.ts +8 -0
- package/dist/peers.d.ts.map +1 -0
- package/dist/peers.js +32 -0
- package/dist/peers.js.map +1 -0
- package/dist/plan.d.ts +22 -4
- package/dist/plan.d.ts.map +1 -1
- package/dist/plan.js +61 -12
- package/dist/plan.js.map +1 -1
- package/dist/ranged.d.ts.map +1 -1
- package/dist/ranged.js +4 -2
- package/dist/ranged.js.map +1 -1
- package/dist/retry.d.ts +23 -1
- package/dist/retry.d.ts.map +1 -1
- package/dist/retry.js +33 -3
- package/dist/retry.js.map +1 -1
- package/dist/search.d.ts +107 -24
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +803 -149
- package/dist/search.js.map +1 -1
- package/dist/sse.d.ts +35 -2
- package/dist/sse.d.ts.map +1 -1
- package/dist/sse.js +145 -31
- package/dist/sse.js.map +1 -1
- package/dist/stores.d.ts.map +1 -1
- package/dist/stores.js +5 -2
- package/dist/stores.js.map +1 -1
- package/dist/text-index.d.ts +9 -1
- package/dist/text-index.d.ts.map +1 -1
- package/dist/text-index.js +28 -9
- package/dist/text-index.js.map +1 -1
- package/dist/text.d.ts +87 -3
- package/dist/text.d.ts.map +1 -1
- package/dist/text.js +526 -24
- package/dist/text.js.map +1 -1
- package/dist/tools.d.ts +61 -1
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +461 -46
- package/dist/tools.js.map +1 -1
- package/dist/types.d.ts +103 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/word-styles.d.ts +102 -0
- package/dist/word-styles.d.ts.map +1 -0
- package/dist/word-styles.js +419 -0
- package/dist/word-styles.js.map +1 -0
- package/dist/xlsx.d.ts +23 -0
- package/dist/xlsx.d.ts.map +1 -0
- package/dist/xlsx.js +511 -0
- package/dist/xlsx.js.map +1 -0
- package/dist/xml-stream.d.ts +55 -0
- package/dist/xml-stream.d.ts.map +1 -0
- package/dist/xml-stream.js +235 -0
- package/dist/xml-stream.js.map +1 -0
- package/dist/zip-directory.d.ts +43 -0
- package/dist/zip-directory.d.ts.map +1 -0
- package/dist/zip-directory.js +130 -0
- package/dist/zip-directory.js.map +1 -0
- package/docs/README.md +288 -0
- package/docs/api.md +37 -381
- package/docs/configuration.md +28 -558
- package/docs/getting-started.md +407 -223
- package/docs/guides/anthropic.md +590 -0
- package/docs/guides/conversations.md +629 -0
- package/docs/guides/costs-and-limits.md +728 -0
- package/docs/guides/deployment.md +720 -0
- package/docs/guides/errors-and-reliability.md +748 -0
- package/docs/guides/explorer-model.md +399 -0
- package/docs/guides/openai-compatible.md +602 -0
- package/docs/guides/own-adapter-and-loop.md +1169 -0
- package/docs/guides/pdfs.md +1024 -0
- package/docs/guides/storage.md +731 -0
- package/docs/guides/testing.md +1203 -0
- package/docs/guides/vertex-and-gemini.md +717 -0
- package/docs/guides/web-app.md +1590 -0
- package/docs/how-it-works.md +1035 -164
- package/docs/measurements.md +881 -0
- package/docs/publishing.md +274 -46
- package/docs/reference/anthropic.md +427 -0
- package/docs/reference/ask.md +910 -0
- package/docs/reference/errors-and-retries.md +760 -0
- package/docs/reference/events.md +1001 -0
- package/docs/reference/gemini.md +827 -0
- package/docs/reference/library.md +1598 -0
- package/docs/reference/node.md +706 -0
- package/docs/reference/openai.md +385 -0
- package/docs/reference/tools.md +1031 -0
- package/docs/security.md +940 -101
- package/docs/troubleshooting.md +1410 -0
- package/package.json +7 -3
- package/src/analyze.ts +204 -49
- package/src/anthropic/adapter.ts +200 -40
- package/src/ask.ts +1151 -210
- package/src/decode.ts +89 -0
- package/src/encoding.ts +850 -0
- package/src/errors.ts +125 -0
- package/src/events.ts +451 -0
- package/src/evidence.ts +11 -2
- package/src/extract.ts +191 -308
- package/src/gemini/adapter.ts +127 -70
- package/src/gemini/chat.ts +28 -4
- package/src/gemini/endpoints.ts +12 -2
- package/src/gemini/errors.ts +34 -1
- package/src/gemini/generate.ts +42 -13
- package/src/gemini/index.ts +3 -3
- package/src/gemini/sse.ts +1 -1
- package/src/groups.ts +162 -0
- package/src/html.ts +1071 -0
- package/src/http.ts +376 -1
- package/src/index.ts +29 -6
- package/src/library.ts +498 -93
- package/src/limiter.ts +6 -1
- package/src/map-reduce.ts +252 -51
- package/src/model.ts +244 -4
- package/src/node/handlers.ts +196 -31
- package/src/node/index.ts +4 -0
- package/src/node/ingest-child.ts +61 -0
- package/src/node/isolated.ts +464 -0
- package/src/node/sse.ts +198 -0
- package/src/office.ts +1047 -0
- package/src/openai/adapter.ts +319 -38
- package/src/output-limit.ts +44 -8
- package/src/pause.ts +41 -0
- package/src/pdf-pages.ts +270 -20
- package/src/pdf-pictures.ts +56 -9
- package/src/pdf-work.ts +722 -0
- package/src/peers.ts +29 -0
- package/src/plan.ts +70 -12
- package/src/ranged.ts +4 -2
- package/src/retry.ts +49 -5
- package/src/search.ts +800 -155
- package/src/sse.ts +153 -28
- package/src/stores.ts +5 -2
- package/src/text-index.ts +30 -9
- package/src/text.ts +524 -23
- package/src/tools.ts +445 -46
- package/src/types.ts +108 -5
- package/src/word-styles.ts +420 -0
- package/src/xlsx.ts +478 -0
- package/src/xml-stream.ts +224 -0
- package/src/zip-directory.ts +136 -0
package/docs/configuration.md
CHANGED
|
@@ -1,628 +1,98 @@
|
|
|
1
1
|
# Configuration
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
| Setting | Default | What it controls |
|
|
9
|
-
|---|---|---|
|
|
10
|
-
| `contextTokens` | 1,000,000 | The model's context window. |
|
|
11
|
-
| `maxRequestBytes` | 10,000,000 | The largest request body your connector accepts. Checked separately from tokens: non-English text and JSON escaping make bytes run ahead of tokens. |
|
|
12
|
-
| `inlineTokens` | half the window, 200,000 at most | The most document text sent whole in one request. Lower it to cut cost and latency; raise it to rely on the long context more. |
|
|
13
|
-
| `reservedTokens` | a quarter of the window, 64,000 at most | Kept free for the answer, tool results, and growing history. |
|
|
14
|
-
| `mapChunkTokens` | half the window, 100,000 at most | Part size when a whole file is read in parts. Smaller parts give more detailed notes but more model calls. |
|
|
15
|
-
| `maxMapChunks` | 30 | Refuse whole-file reads needing more model calls than this. 30 parts × 100K tokens = 3M tokens of input. |
|
|
16
|
-
| `toolResultCharacters` | 12% of the window, 24,000 at most | Cap on one tool result, in characters. For a log that can be 12,000 tokens. |
|
|
3
|
+
The options are no longer on one page. Every option, with its default, is now in the reference,
|
|
4
|
+
one page per module of the package, and how to use them for a task is in the guides. This page
|
|
5
|
+
keeps the headings of the old page, so that links to them still work, and each one points to its
|
|
6
|
+
new home. All pages are listed in the [documentation index](./README.md).
|
|
17
7
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
```ts
|
|
21
|
-
createLargeFiles({ store, budget: { contextTokens: 128_000, maxRequestBytes: 4_000_000 } });
|
|
22
|
-
// → inlineTokens 64,000 · reservedTokens 32,000 · mapChunkTokens 64,000 · toolResultCharacters 15,360
|
|
23
|
-
```
|
|
8
|
+
## Budget
|
|
24
9
|
|
|
25
|
-
|
|
26
|
-
library is created: `reservedTokens` or `mapChunkTokens` at or above `contextTokens`.
|
|
27
|
-
`resolveBudget(partial)` gives the whole budget for any set of fields.
|
|
10
|
+
How to set the budget for your model is now in [Costs and limits](./guides/costs-and-limits.md); every field and its default is in [`Budget`](./reference/library.md#budget).
|
|
28
11
|
|
|
29
12
|
### One library, several models
|
|
30
13
|
|
|
31
|
-
|
|
32
|
-
what its model takes:
|
|
33
|
-
|
|
34
|
-
```ts
|
|
35
|
-
const small = openAiCompatible({ baseUrl, apiKey, model: "small-model", contextTokens: 128_000 });
|
|
36
|
-
const result = await askWithFiles({ files, model: small, documentIds, messages });
|
|
37
|
-
result.budget; // the library's budget, fitted to a window of 128,000 tokens
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
- The smaller window holds: the library's `contextTokens`, or the model's. With an explorer, the
|
|
41
|
-
smallest of the three. The same for `maxRequestBytes`.
|
|
42
|
-
- Fields the library left open follow that window. Fields it set are kept, and lowered if they are
|
|
43
|
-
too large for it.
|
|
44
|
-
- `outputTokens` of a model is kept free on top: Claude counts `max_tokens` against the window.
|
|
45
|
-
- `tokenScale` is how many of the model's tokens one token of the library's estimate comes to. The
|
|
46
|
-
estimate is fitted to Gemini, so that is 1 there. For a tokenizer that counts more (the Claude
|
|
47
|
-
adapter says 1.35), the window is that much smaller in the library's terms.
|
|
48
|
-
- `budget` in `askWithFiles` (and in `prepare` and `tools`) changes fields for one question:
|
|
49
|
-
`budget: { inlineTokens: 20_000 }`.
|
|
14
|
+
Now in [Costs and limits](./guides/costs-and-limits.md); what an adapter says about its model is [`ModelCapabilities`](./reference/ask.md#modelcapabilities).
|
|
50
15
|
|
|
51
16
|
### Limits and retries
|
|
52
17
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
| Limit | Default | |
|
|
56
|
-
|---|---|---|
|
|
57
|
-
| `toolTurns` | 12 | Chat requests for one question, counting the answer. |
|
|
58
|
-
| `callsPerTurn` | 8 | Tool calls of one model turn that are run. The others are answered "send it again if you still need it". |
|
|
59
|
-
| `wholeDocumentReads` | 1 | How often each document may be read whole. One read is up to `maxMapChunks` requests. 0 refuses them all. |
|
|
60
|
-
| `inputTokens` | none | Once the question has used this many input tokens, over all its requests, the next request is the answer. A whole-file read that would pass it is refused with the numbers. |
|
|
61
|
-
| `requests` | none | The same, for the number of requests: the chat's and the part reads' together. |
|
|
62
|
-
|
|
63
|
-
```ts
|
|
64
|
-
await askWithFiles({ files, model, documentIds, messages, limits: { toolTurns: 8, inputTokens: 2_000_000 } });
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
A request that failed is sent again when another try can help: HTTP 408, 409, 429 and 5xx, and a
|
|
68
|
-
connection that failed. It waits 0.25 to 0.5 seconds, then twice and four times that, or as long as
|
|
69
|
-
the provider asks (`Retry-After`, or the `retryDelay` of Gemini's `RetryInfo`). It is not sent
|
|
70
|
-
again once text of the reply was shown, after you stopped the question, or when the provider asks
|
|
71
|
-
for a longer wait than `maxDelayMs`. The part reads of a whole-file read follow the same rule.
|
|
72
|
-
|
|
73
|
-
```ts
|
|
74
|
-
retry: { retries: 3, baseDelayMs: 500, maxDelayMs: 60_000 } // the defaults; retries: 0 turns it off
|
|
75
|
-
```
|
|
18
|
+
The limits of a question are now in [Costs and limits](./guides/costs-and-limits.md) and [`AskLimits`](./reference/ask.md#asklimits); retries are in [Errors and reliability](./guides/errors-and-reliability.md) and [`RetryOptions`](./reference/errors-and-retries.md#retryoptions), and the waits on a silent model (`firstByteTimeoutMs`, `idleTimeoutMs`) are in the same guide and [`IdleWaits`](./reference/errors-and-retries.md#idlewaits).
|
|
76
19
|
|
|
77
20
|
### Room in the request
|
|
78
21
|
|
|
79
|
-
|
|
80
|
-
each later request. The loop counts the request as it grows, by the library's estimate and, once
|
|
81
|
-
the provider has answered, by the provider's own count:
|
|
82
|
-
|
|
83
|
-
- Before the calls of a turn run, each is given the room that is left, up to
|
|
84
|
-
`toolResultCharacters`. A result is cut to its room and says where to go on.
|
|
85
|
-
- When the room runs short, the results of all but the last three tool rounds are replaced by
|
|
86
|
-
stand-ins: `{ document, dropped: true, lines: "1-212", note: "… Read them again with
|
|
87
|
-
read_document if you need them." }`. `steps` then holds "Older tool results were shortened to
|
|
88
|
-
make room in the request". Claude's conversation is never rewritten, so this step is skipped for
|
|
89
|
-
it.
|
|
90
|
-
- A call that still finds no room is answered "no room left, answer now", and the next request is
|
|
91
|
-
the answer.
|
|
92
|
-
|
|
93
|
-
Text tools may fill the window up to half of `reservedTokens`; the other half is the answer's.
|
|
22
|
+
Now in [How it works](./how-it-works.md).
|
|
94
23
|
|
|
95
24
|
### How the plan is made
|
|
96
25
|
|
|
97
|
-
|
|
98
|
-
`base.bytes`) and fills the room left, smallest documents first:
|
|
99
|
-
|
|
100
|
-
- token room = `min(inlineTokens, contextTokens − reservedTokens − base.tokens)`
|
|
101
|
-
- byte room = `maxRequestBytes − 256 KB − base.bytes`
|
|
102
|
-
|
|
103
|
-
A document is sent whole if it fits both. Otherwise it becomes searchable.
|
|
26
|
+
Now in [How it works](./how-it-works.md).
|
|
104
27
|
|
|
105
28
|
### Token estimates
|
|
106
29
|
|
|
107
|
-
|
|
108
|
-
of text. As counted by Gemini:
|
|
109
|
-
|
|
110
|
-
| Text | Characters per token |
|
|
111
|
-
|---|---|
|
|
112
|
-
| English prose | 4.3 to 5.0 |
|
|
113
|
-
| Source code | 3.5 to 4.1 |
|
|
114
|
-
| Hebrew, Arabic, Russian prose | 2.1 to 3.3 |
|
|
115
|
-
| Chinese, Japanese, Korean prose | 1.4 to 1.7 |
|
|
116
|
-
| Logs (timestamps, IDs, IP addresses) | 1.4 to 2.8 |
|
|
117
|
-
| CSV of numbers, hex dumps, base64 | 1.0 to 1.4 |
|
|
118
|
-
|
|
119
|
-
Digits cost about one token each, which is why a log is three times denser than a novel of the
|
|
120
|
-
same size. The estimate was fitted to Gemini's `countTokens` on 60 texts (Gemini 2.5, 3.5, and 3.8
|
|
121
|
-
count identically) and lands between 0.98 and 1.7 times the real count. It leans high on purpose:
|
|
122
|
-
overestimating only makes a file searchable a little sooner. The one kind of text it still counts
|
|
123
|
-
low, at about 70%, is a long run of random letters in one case with no digits.
|
|
124
|
-
|
|
125
|
-
Tokenizers differ between providers. These weights are fitted to Gemini, which counts digits one
|
|
126
|
-
by one. A tokenizer that groups digits will count fewer tokens than this estimate for logs and
|
|
127
|
-
data, which is the safe direction.
|
|
30
|
+
Now in [How it works](./how-it-works.md).
|
|
128
31
|
|
|
129
32
|
## An explorer model
|
|
130
33
|
|
|
131
|
-
|
|
132
|
-
of your own endpoint. The package has no default. That one model does both halves of the work on a
|
|
133
|
-
large file: it explores the file (searches, reads, looks at pages), and it answers.
|
|
134
|
-
`explorerModel` names a second model for the first half:
|
|
135
|
-
|
|
136
|
-
```ts
|
|
137
|
-
const endpoint = geminiApi({
|
|
138
|
-
apiKey,
|
|
139
|
-
model: "gemini-3.8-flash", // writes the answer
|
|
140
|
-
explorerModel: process.env.GEMINI_EXPLORER_MODEL, // optional: searches and reads
|
|
141
|
-
});
|
|
142
|
-
// vertexAi({ project, model, explorerModel, accessToken }) takes it the same way.
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
Left out, empty, or the same as `model`, there is no explorer, and `askGemini` sends exactly the
|
|
146
|
-
requests it sent before. So a setting that may be unset can be passed as it is.
|
|
147
|
-
|
|
148
|
-
With URLs of your own, give the explorer's as `explorerUrl`. It is called with the same headers:
|
|
149
|
-
|
|
150
|
-
```ts
|
|
151
|
-
const endpoint = {
|
|
152
|
-
url: "https://llm-gateway.corp.example/gemini/models/gemini-3.8-flash:generateContent",
|
|
153
|
-
streamUrl: "https://llm-gateway.corp.example/gemini/models/gemini-3.8-flash:streamGenerateContent?alt=sse",
|
|
154
|
-
explorerUrl: "https://llm-gateway.corp.example/gemini/models/gemini-3.5-flash-lite:generateContent",
|
|
155
|
-
headers,
|
|
156
|
-
};
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
The reason to use one: a question about a large log can take ten requests of searching and
|
|
160
|
-
reading, each larger than the one before, and a whole-file read adds up to 30 part reads. With an
|
|
161
|
-
explorer those go to a cheaper or faster model, and the model you chose for its answers is called
|
|
162
|
-
once.
|
|
34
|
+
Now in [An explorer model](./guides/explorer-model.md).
|
|
163
35
|
|
|
164
36
|
### Who does what
|
|
165
37
|
|
|
166
|
-
|
|
167
|
-
|---|---|---|
|
|
168
|
-
| Requests with tools: `search_document`, `read_document`, `get_document_overview`, `view_pages` | All | None |
|
|
169
|
-
| The part reads of `analyze_whole_document` | All | None |
|
|
170
|
-
| The answer | | One request, without tools |
|
|
171
|
-
| A question about files that are sent whole, with no pages to look at | Not called | One request, as before |
|
|
172
|
-
|
|
173
|
-
The answering model gets the conversation, the explorer's tool calls, and what each call
|
|
174
|
-
returned, pages included. It does not get the explorer's thoughts, its thought signatures, or
|
|
175
|
-
anything it wrote: the answer is written from what the files say, not from what the explorer made
|
|
176
|
-
of them. So the answering model can use only what the explorer fetched. It has no tools of its own
|
|
177
|
-
to fetch more.
|
|
178
|
-
|
|
179
|
-
The explorer is told its part, in a line added to its system instruction (`EXPLORER_INSTRUCTION`):
|
|
180
|
-
gather what the answer needs, then reply `DONE`. `gemini-3.5-flash-lite` did exactly that, at one
|
|
181
|
-
output token. A model that writes an answer anyway (`gemini-2.5-flash-lite` did) costs those output
|
|
182
|
-
tokens, and its answer is dropped.
|
|
38
|
+
Now in [An explorer model](./guides/explorer-model.md).
|
|
183
39
|
|
|
184
40
|
### What changes for your application
|
|
185
41
|
|
|
186
|
-
|
|
187
|
-
settles in three requests takes four. `maxToolTurns` still counts them all: the explorer has
|
|
188
|
-
`maxToolTurns − 1` requests, and the last one is the answer.
|
|
189
|
-
- **`text` and `onText` carry only the answer.** Without an explorer, a remark that the model makes
|
|
190
|
-
before a tool call ("Let me search…") is part of `text`. The explorer's words are not shown.
|
|
191
|
-
`onActivity` and `onStep` report its tool calls as before, and `turns` counts its requests too.
|
|
192
|
-
`usage` is the total of both models, with an entry for each in `usage.byModel`; `finishReason`
|
|
193
|
-
and `lastUsage` are those of the answer.
|
|
194
|
-
- **The explorer need not be a Gemini model.** With `askWithFiles`, `explorer` is any adapter:
|
|
195
|
-
`askWithFiles({ model: gemini(…), explorer: openAiCompatible(…) })`. The answering model gets
|
|
196
|
-
the explorer's calls and results in its own provider's form.
|
|
197
|
-
- **The explorer's requests are not streamed.** Nothing it writes is shown, so each goes to
|
|
198
|
-
`explorerUrl` in one piece. The answer streams as before.
|
|
199
|
-
- **Settings are per model.** `explorerGenerationConfig` is the explorer's `generationConfig`, and
|
|
200
|
-
defaults to yours. Set it when the two models do not take the same settings: given a
|
|
201
|
-
`thinkingLevel`, `gemini-2.5-flash-lite` on Vertex AI answers HTTP 400, "thinking_level is not
|
|
202
|
-
supported by this model". `analysisConfig` is for the part reads, which the explorer does, so
|
|
203
|
-
write it for the explorer.
|
|
204
|
-
- **The budget has to fit both models.** Both get the same conversation and the same tool
|
|
205
|
-
results. Set `contextTokens` and `maxRequestBytes` to the smaller of the two.
|
|
206
|
-
- **Pages have to be readable by both models.** `toolFilePlacement: "inside"` is read by Gemini 3
|
|
207
|
-
models only. If either model is Gemini 2.5, use `"beside"`, which both read
|
|
208
|
-
([below](#where-the-pages-go-toolfileplacement)).
|
|
209
|
-
- **An explorer that fails is an error.** A model name that does not exist, or a quota that is
|
|
210
|
-
used up, throws `GeminiRequestError`, as it does for `model`. The question is not passed to
|
|
211
|
-
`model` instead: that would hide the mistake, and send the searching to the model you wanted to
|
|
212
|
-
spare.
|
|
213
|
-
- **A PDF whose text is in the prompt**, and whose pages can be looked at, goes to the explorer
|
|
214
|
-
first: it decides whether to look. The text of such a PDF is then sent twice, once to each model.
|
|
215
|
-
|
|
216
|
-
```ts
|
|
217
|
-
await askGemini({
|
|
218
|
-
files, endpoint, messages, documentIds,
|
|
219
|
-
generationConfig: { thinkingConfig: { thinkingLevel: "high" } }, // for `model`
|
|
220
|
-
explorerGenerationConfig: { thinkingConfig: { thinkingBudget: 0 } }, // for a Gemini 2.5 explorer
|
|
221
|
-
toolFilePlacement: "beside", // one of the two is Gemini 2.5
|
|
222
|
-
});
|
|
223
|
-
```
|
|
42
|
+
Now in [An explorer model](./guides/explorer-model.md).
|
|
224
43
|
|
|
225
44
|
### Live results with an explorer
|
|
226
45
|
|
|
227
|
-
|
|
228
|
-
deadlocks and one fatal error. The PDF is the one with a chart and a photo from
|
|
229
|
-
[Pictures in PDFs](#live-results).
|
|
230
|
-
|
|
231
|
-
| Explorer → `model` | Question | Result |
|
|
232
|
-
|---|---|---|
|
|
233
|
-
| `gemini-3.5-flash-lite` → `gemini-3.1-flash-lite`, Gemini API | How many deadlocks, and the line of the first | Two searches, then `DONE`. Answer right. Four requests, 3 s. |
|
|
234
|
-
| | What shut the gateway down, its error code, the warning before it | One search, then `DONE`. Four of four facts, with the right lines. |
|
|
235
|
-
| | The name of the file and its line count, which the card shows | `DONE` at once. Answered from the card, in two requests. |
|
|
236
|
-
| | Every kind of error, from a whole-file read | The explorer read the four parts and wrote up the notes, then searched three times. Answer right. |
|
|
237
|
-
| | The chart and the photo, with the text sent whole | The explorer looked at two pages. The other model got them and had five of five facts right. |
|
|
238
|
-
| | The same, with the PDF searched | A read and a look in one turn. Five of five. |
|
|
239
|
-
| | The gateway and the chart again, both models thinking (`thinkingLevel: "high"`) | Both right. |
|
|
240
|
-
| `gemini-3.1-flash-lite` → `gemini-3.5-flash-lite`, Gemini API | The deadlocks | Answer right. The explorer repeated one search until its eleven requests were used up, as it does when it works alone. |
|
|
241
|
-
| `gemini-2.5-flash-lite` → `gemini-3.5-flash-lite`, Vertex AI | The deadlocks, the gateway, and the chart with pages `"beside"` | All right. This explorer wrote an answer each time instead of `DONE`. |
|
|
242
|
-
| `gemini-3.5-flash-lite` → `gemini-2.5-flash-lite`, Vertex AI | The deadlocks, the chart with pages `"beside"`, and the card | All right. |
|
|
243
|
-
| `gemini-3.1-flash-lite` → `gemini-3.5-flash-lite`, Vertex AI, both thinking | The gateway | Right. |
|
|
244
|
-
|
|
245
|
-
What made the form of the handoff: a Gemini 3 model refuses a request that ends with the result of
|
|
246
|
-
a function call it did not sign (HTTP 400, "Function call is missing a thought_signature"), and a
|
|
247
|
-
thought signature belongs to the model that made the call. When a user message follows the
|
|
248
|
-
results, the same calls are accepted without a signature: by Gemini 3.1 and 3.5 Flash-Lite on the
|
|
249
|
-
Gemini API and on Vertex AI, and by Gemini 2.5 Flash-Lite on Vertex AI. So the answering model's
|
|
250
|
-
request always ends with a user message, and holds no signatures.
|
|
251
|
-
|
|
252
|
-
Not checked: models larger than Flash-Lite, and Gemini 4. The two models have to be reachable with
|
|
253
|
-
the same headers, so one cannot be on the Gemini API and the other on Vertex AI.
|
|
46
|
+
Now in [Measurements](./measurements.md); what they mean for your setup is in [An explorer model](./guides/explorer-model.md).
|
|
254
47
|
|
|
255
48
|
## Stores
|
|
256
49
|
|
|
257
|
-
|
|
258
|
-
|---|---|
|
|
259
|
-
| `MemoryDocumentStore` | Tests, scripts, one-instance demos. Lost on restart. |
|
|
260
|
-
| `FileDocumentStore(dir)` | One server or a shared volume. Survives restarts. |
|
|
261
|
-
| Your own | Several instances: implement the interface below over Redis, S3, or a database. |
|
|
262
|
-
|
|
263
|
-
```ts
|
|
264
|
-
interface DocumentStore {
|
|
265
|
-
put(document: { meta: DocumentMeta; text: string }): Promise<void>;
|
|
266
|
-
get(id: string): Promise<{ meta: DocumentMeta; text: string } | undefined>; // undefined if expired
|
|
267
|
-
getMeta(id: string): Promise<DocumentMeta | undefined>;
|
|
268
|
-
delete(id: string): Promise<boolean>; // the original file and the index too
|
|
269
|
-
sweep?(now?: Date): Promise<number>;
|
|
270
|
-
putOriginal?(id: string, bytes: Uint8Array): Promise<void>; // optional: see below
|
|
271
|
-
getOriginal?(id: string): Promise<Uint8Array | undefined>; // undefined if not kept, or expired
|
|
272
|
-
// Optional: read large documents by range instead of loading them whole (see below).
|
|
273
|
-
readTextRange?(id: string, start: number, end: number): Promise<Uint8Array | undefined>; // UTF-8 bytes
|
|
274
|
-
putIndex?(id: string, index: Uint8Array): Promise<void>;
|
|
275
|
-
getIndex?(id: string): Promise<Uint8Array | undefined>;
|
|
276
|
-
}
|
|
277
|
-
```
|
|
278
|
-
|
|
279
|
-
Keep `getMeta` cheap (store the metadata separately from the text): it is called on every tool
|
|
280
|
-
call. A document ready to search is kept in memory between tool calls, within `cacheBytes`
|
|
281
|
-
(default 256 MB, the least recently used dropped first; `cacheSize` caps the number of documents too). Every
|
|
282
|
-
use of a kept document asks the store first, so a document deleted or expired anywhere is gone
|
|
283
|
-
everywhere at once.
|
|
50
|
+
Now in [Storage](./guides/storage.md) and, for several servers, in [Deployment](./guides/deployment.md); the interface is [`DocumentStore`](./reference/library.md#documentstore).
|
|
284
51
|
|
|
285
52
|
### Large files, many users, several servers
|
|
286
53
|
|
|
287
|
-
|
|
288
|
-
than `wholeDocumentBytes` (default 2 MB) is never loaded whole:
|
|
289
|
-
|
|
290
|
-
- At ingest, a line index is written next to the text (`<id>.idx`, four bytes a line: 3 MB for a
|
|
291
|
-
file of 730,000 lines). `read_document`, the card and the overview read only the lines they show.
|
|
292
|
-
- A search reads the text 1 MB at a time (`scanChunkBytes`), reading the next piece while it searches
|
|
293
|
-
the current one, so a search holds a few MB whatever the size of the file. At most
|
|
294
|
-
`maxConcurrentScans` searches (default 4) run at once in a process; the others wait their turn,
|
|
295
|
-
and those waiting for the same file share one pass over it: it is read, decoded and lowercased
|
|
296
|
-
once for all of them. Results are the same as from the whole text: the tests compare them call by call.
|
|
297
|
-
- Decoded pieces of a file are kept in the same `cacheBytes` budget, so the next search of a file in
|
|
298
|
-
use reads nothing. A file whose decoded text would take more than a quarter of the budget is never
|
|
299
|
-
kept: it would push out everything else. A larger `cacheBytes` spends memory to save CPU.
|
|
300
|
-
- A document from an older version, without an index, gets one the first time it is used: it is
|
|
301
|
-
built from the text in one pass and saved for the other servers. A damaged index, or one that
|
|
302
|
-
does not match its document, is noticed and built again.
|
|
303
|
-
|
|
304
|
-
Smaller documents are loaded whole and kept in the cache: for them that is faster, and they are small.
|
|
305
|
-
`wholeDocumentBytes: Infinity` loads every document whole, as before 0.4; `0` reads every one by range.
|
|
306
|
-
|
|
307
|
-
`FileDocumentStore` on a volume that several servers share:
|
|
308
|
-
|
|
309
|
-
- Every file is written under a temporary name and then renamed, so no server reads half a file.
|
|
310
|
-
- `sweep` removes files of no document (an upload that failed, a server that stopped while writing)
|
|
311
|
-
and temporary files once they are an hour old (`orphanAgeMs`), never younger ones: they may be an
|
|
312
|
-
upload in progress on another server. Run `sweep` from one place, such as a scheduled job.
|
|
313
|
-
- A document whose metadata could not be read during a sweep (the volume did not answer) is left
|
|
314
|
-
alone, whatever its age. Only metadata that is not valid is taken for a broken document.
|
|
315
|
-
- A read that fails for another reason than "no such file" is thrown, and the library passes it
|
|
316
|
-
on as `DocumentStoreError`: a busy volume is not a deleted document. A store of your own should
|
|
317
|
-
do the same: return `undefined` only for a document that is not there.
|
|
318
|
-
- Servers of an older version on the same volume (during a rolling update) read the same files and
|
|
319
|
-
ignore the index. An index left behind when one of them deletes a document is swept an hour later.
|
|
320
|
-
|
|
321
|
-
A store of your own can do the same over S3 (`Range` requests), Redis (`GETRANGE`) or a database:
|
|
322
|
-
add `readTextRange`, and `putIndex` with `getIndex` to keep the index. Without `putIndex` the index
|
|
323
|
-
is built in memory each time the document is opened.
|
|
54
|
+
Reading large documents by range and the cache are now in [Storage](./guides/storage.md); several servers, memory and `sweep` are in [Deployment](./guides/deployment.md).
|
|
324
55
|
|
|
325
56
|
### Keeping the uploaded file
|
|
326
57
|
|
|
327
|
-
|
|
328
|
-
which is what [showing pages of a PDF](#pictures-in-pdfs) needs:
|
|
329
|
-
|
|
330
|
-
```ts
|
|
331
|
-
createLargeFiles({ store: new FileDocumentStore(dir), keepOriginals: ["pdf"] });
|
|
332
|
-
```
|
|
333
|
-
|
|
334
|
-
- It is off by default because it costs disk: a kept file is as large as the upload (up to
|
|
335
|
-
`maxUploadBytes`, 100 MB) and stays until the document expires or is deleted.
|
|
336
|
-
- `FileDocumentStore` writes it as `<id>.bin` next to `<id>.txt` and `<id>.json`.
|
|
337
|
-
`MemoryDocumentStore` keeps a copy in memory.
|
|
338
|
-
- A store of your own keeps originals when it has `putOriginal` and `getOriginal`. `putOriginal` is
|
|
339
|
-
called before `put` for the same ID, and `delete` and `sweep` must remove the file. A store
|
|
340
|
-
without the two methods works as before: nothing is kept, and `view_pages` is not offered.
|
|
341
|
-
- `DocumentMeta.originalKept` says whether a document's file is kept. `files.getOriginal(id, { owner })`
|
|
342
|
-
returns it.
|
|
343
|
-
- Documents ingested before you turned it on have no original. Upload them again to get one.
|
|
58
|
+
Now in [Storage](./guides/storage.md); what `view_pages` does with the kept file is in [PDFs and other formats](./guides/pdfs.md).
|
|
344
59
|
|
|
345
60
|
## Formats
|
|
346
61
|
|
|
347
|
-
|
|
348
|
-
|---|---|---|
|
|
349
|
-
| `log` | `.log`, `.out`, `.err`, or text whose early lines mostly start with a timestamp or level | Gets a log profile |
|
|
350
|
-
| `markdown` | `.md`, `.markdown`, `text/markdown` | Headings form the outline |
|
|
351
|
-
| `json` | `.json`, `.jsonl`, `.ndjson` | Minified JSON is wrapped into addressable lines |
|
|
352
|
-
| `csv` | `.csv`, `.tsv` | |
|
|
353
|
-
| `pdf` | `.pdf` | Needs `unpdf`. Pages are marked `--- Page N ---`, tables are rebuilt (below), and a page without text says so. With `@cantoo/pdf-lib`, a page that shows a picture says so too, and the model can be shown the page itself ([Pictures in PDFs](#pictures-in-pdfs)). A scanned PDF (no text layer at all) is rejected, unless its original is kept and it has at most 30 pages. |
|
|
354
|
-
| `docx`, `pptx` | extension or MIME type | Needs `jszip`. Slides are marked `## Slide N`. A text box is read once, after the paragraph it is anchored in. A file that unpacks to more than 100 times its size (past the first 16 MB), or to more than 128 MB of XML, is rejected as a possible zip bomb. |
|
|
355
|
-
| `text` | anything else that decodes as text | UTF-8 or UTF-16 (with or without BOM); binary is rejected |
|
|
356
|
-
|
|
357
|
-
Lines longer than 2,000 characters are split, so every line can be read by number.
|
|
62
|
+
Now in [PDFs and other formats](./guides/pdfs.md), which also says how a text file that is not in UTF-8 is read; the fields that record it are `encoding` and `replacedCharacters` of [`DocumentMeta`](./reference/library.md#documentmeta).
|
|
358
63
|
|
|
359
64
|
### PDF tables
|
|
360
65
|
|
|
361
|
-
|
|
362
|
-
like a row without that column: `EMEA 80 95 101` under the headings `Q1 Q2 Q3 Q4`, and a model
|
|
363
|
-
reads 95 as Q2. The PDF extractor finds tables by the position of their text and writes each row
|
|
364
|
-
with its cells marked, so the same row becomes:
|
|
365
|
-
|
|
366
|
-
```
|
|
367
|
-
| Region | Q1 | Q2 | Q3 | Q4 |
|
|
368
|
-
| EMEA | 80 | | 95 | 101 |
|
|
369
|
-
```
|
|
370
|
-
|
|
371
|
-
In a live test (`gemini-3.5-flash-lite`, 110 questions about single cells of seven PDFs made by
|
|
372
|
-
Word and Chrome, half of them about empty cells), 44 answers were right from the text in file
|
|
373
|
-
order and 110 from the rebuilt rows.
|
|
374
|
-
|
|
375
|
-
What it handles: empty cells, tables inside one column of a two-column page, right-to-left tables
|
|
376
|
-
(the first column is the rightmost), tables printed sideways, rotated pages, footnote marks,
|
|
377
|
-
headings that span columns, and cells whose text wraps onto several lines when the rows are
|
|
378
|
-
visibly spaced apart. Text that is not a table (paragraphs, lists, columns of prose, justified
|
|
379
|
-
text) is left as pdf.js gives it.
|
|
380
|
-
|
|
381
|
-
It is cautious: where the evidence for a table is weak, the text stays as it was. So:
|
|
382
|
-
|
|
383
|
-
- Two-column tables (key and value, a glossary) are left as lines of text. They read correctly
|
|
384
|
-
that way.
|
|
385
|
-
- When a cell wraps and the rows have no extra space between them, each line of the cell is a row
|
|
386
|
-
of its own, with the other cells empty.
|
|
387
|
-
- Columns that are closer together than a word space on every line of the table stay together in
|
|
388
|
-
one cell, on every row alike.
|
|
389
|
-
- A table whose text is stored column by column in the file is not rebuilt.
|
|
390
|
-
- Pictures and charts are not in the text. A page with no text at all is written as
|
|
391
|
-
`[No text on this page. It may be a scan, a photo, or a chart.]`, so the model can say that it
|
|
392
|
-
cannot see it. To let the model look at such pages, see [Pictures in PDFs](#pictures-in-pdfs).
|
|
393
|
-
|
|
394
|
-
The cell marks cost tokens: about one per cell. A bank statement came to 27% more tokens than
|
|
395
|
-
its text in file order, a report with one table in it to 9% more. Reading 600 pages with 24,600
|
|
396
|
-
table rows took 1.6 seconds instead of 1.3.
|
|
397
|
-
|
|
398
|
-
The rules were tuned on PDFs made by Chrome and by Word. To get the text of version 0.1.0 instead:
|
|
399
|
-
|
|
400
|
-
```ts
|
|
401
|
-
import { createLargeFiles, createPdfExtractor } from "llm-large-files";
|
|
402
|
-
|
|
403
|
-
createLargeFiles({ store, extractors: { pdf: createPdfExtractor({ tables: false }) } });
|
|
404
|
-
```
|
|
405
|
-
|
|
406
|
-
Small PDFs are still better sent to Gemini as the file itself, when your payload limit allows:
|
|
407
|
-
it then sees every page, including pictures. For a PDF that is too large for that, the next section
|
|
408
|
-
gives the model the pages it asks for.
|
|
66
|
+
Now in [PDFs and other formats](./guides/pdfs.md).
|
|
409
67
|
|
|
410
68
|
## Pictures in PDFs
|
|
411
69
|
|
|
412
|
-
|
|
413
|
-
text alone, a model can only say that it cannot see it, or guess. Two things, both optional and
|
|
414
|
-
both for PDFs only, close that gap:
|
|
415
|
-
|
|
416
|
-
1. **A line in the text that says where the pictures are.**
|
|
417
|
-
2. **A tool, `view_pages`, that hands the model the real pages** as a small PDF, when it asks.
|
|
418
|
-
|
|
419
|
-
```bash
|
|
420
|
-
npm install @cantoo/pdf-lib # optional dependency, needed for both
|
|
421
|
-
```
|
|
422
|
-
|
|
423
|
-
```ts
|
|
424
|
-
const files = createLargeFiles({
|
|
425
|
-
store: new FileDocumentStore("./data/large-files"),
|
|
426
|
-
keepOriginals: ["pdf"], // needed for view_pages: it cuts pages out of the uploaded file
|
|
427
|
-
});
|
|
428
|
-
// askGemini then offers view_pages by itself. Nothing else changes in your code.
|
|
429
|
-
```
|
|
70
|
+
Now in [PDFs and other formats](./guides/pdfs.md).
|
|
430
71
|
|
|
431
72
|
### The line that marks a page
|
|
432
73
|
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
```
|
|
436
|
-
--- Page 12 ---
|
|
437
|
-
[Picture on this page. This text cannot show it.]
|
|
438
|
-
Figure 3: sales by region.
|
|
439
|
-
```
|
|
440
|
-
|
|
441
|
-
The words hold whether or not the model can look at the page later. A search for
|
|
442
|
-
`[Picture on this page` lists every such page.
|
|
443
|
-
|
|
444
|
-
What counts:
|
|
445
|
-
|
|
446
|
-
- **A picture:** the images on the page cover 3% of it or more, measured by the size they are
|
|
447
|
-
shown at, not by their pixels. A logo covers about 1%. An image that is on 30% of the pages (and
|
|
448
|
-
on three at least) is taken for a logo, a letterhead or a watermark, and does not count,
|
|
449
|
-
whatever its size.
|
|
450
|
-
- **A drawing:** shapes filled in three or more colours, outside the tables. Greys, rules, dots
|
|
451
|
-
and page backgrounds do not count, and neither does the shading of a table's cells.
|
|
452
|
-
- A page whose drawing instructions are larger than 2 MB: that is a map or a technical drawing.
|
|
453
|
-
So is a page that paints more than 20,000 images, or whose instructions come to more than 16 MB
|
|
454
|
-
once each form (a drawing kept apart and painted by name) is counted every time it is painted.
|
|
455
|
-
Reading such a page stops there, so a few kilobytes of forms painted inside forms cannot hold
|
|
456
|
-
the server.
|
|
457
|
-
|
|
458
|
-
What it gets wrong:
|
|
459
|
-
|
|
460
|
-
- Missed: a chart in one or two colours, and a chart or diagram drawn only with lines.
|
|
461
|
-
- Marked by mistake: a table with cells shaded in three colours, when the table itself was not
|
|
462
|
-
recognised (a table of two columns, for example).
|
|
463
|
-
|
|
464
|
-
The mark is a hint. `view_pages` works on any page, marked or not, and in a live test the model
|
|
465
|
-
looked at a one-colour chart that had only a caption (below).
|
|
466
|
-
|
|
467
|
-
The pages are found from the structure of the file, without decoding any image, so it is cheap:
|
|
468
|
-
a PDF of 200 photos (85 MB) took 0.3 s to ingest without marks and 0.8 s with them. No page is
|
|
469
|
-
marked, and the text is exactly that of version 0.2.0, when:
|
|
470
|
-
|
|
471
|
-
- `@cantoo/pdf-lib` is not installed;
|
|
472
|
-
- you turn it off: `createPdfExtractor({ pictures: false })`;
|
|
473
|
-
- the file declares more than 150,000 objects (reading it would take over two seconds), reading
|
|
474
|
-
took more than five seconds, or the file does not have the pages the text was read from.
|
|
475
|
-
|
|
476
|
-
One cost to know: a PDF whose streams do not state their length up front (Ghostscript writes them
|
|
477
|
-
so) is scanned byte by byte, about 30 ms per MB. A file of 100 MB written that way took 3 s to
|
|
478
|
-
ingest instead of 0.1 s.
|
|
74
|
+
Now in [PDFs and other formats](./guides/pdfs.md).
|
|
479
75
|
|
|
480
76
|
### `view_pages`
|
|
481
77
|
|
|
482
|
-
|
|
483
|
-
returns them as one PDF. `askGemini` puts it inside the function response, and Gemini reads it
|
|
484
|
-
like any PDF: text, pictures and layout.
|
|
485
|
-
|
|
486
|
-
It is offered for every PDF whose original is kept, also for one whose text is already in the
|
|
487
|
-
prompt: a slide deck of photos has little text, so its text fits, and without the tool the model
|
|
488
|
-
would see none of its slides. Such a PDF gets `view_pages` and no search tools.
|
|
489
|
-
|
|
490
|
-
For a PDF that the model can look at, search and read results also say which page a line is on
|
|
491
|
-
(`"page": 12`), since a page number is what `view_pages` takes.
|
|
492
|
-
|
|
493
|
-
Limits:
|
|
494
|
-
|
|
495
|
-
| Limit | Value | When it is reached |
|
|
496
|
-
|---|---|---|
|
|
497
|
-
| Pages per call | 5 | The result says where to continue (`next_page`). |
|
|
498
|
-
| Bytes | What is left of `maxRequestBytes`, less a tenth kept free (256 KB at least), times 3/4 for base64 | Pages are left off the end of the call. A page that does not fit by itself gives an error, and the model answers from what it has. |
|
|
499
|
-
| Pages per question | `reservedTokens / 2 / 530`: 60 with the default budget | The tool says that no more pages can be looked at. |
|
|
500
|
-
|
|
501
|
-
Pages stay in the request until the question is answered, so every look makes the later requests
|
|
502
|
-
of that question larger. They are not sent again with the next question.
|
|
503
|
-
|
|
504
|
-
A page costs Gemini about 530 tokens, whatever is on it: 532 for each of 1, 2, 3 and 5 A4 pages,
|
|
505
|
-
and 527 for a 16:9 slide (`gemini-3.5-flash-lite`).
|
|
506
|
-
|
|
507
|
-
A cut holds the pages asked for and little else. Copying pages the simple way does not: a contents
|
|
508
|
-
page links to every section, and those pages came along, so the cut was 89% of the file. Here every
|
|
509
|
-
other page is emptied before copying, and a page that shares its resources with the whole file
|
|
510
|
-
(FPDF, TCPDF and jsPDF write PDFs that way) gets a list of its own.
|
|
511
|
-
|
|
512
|
-
| File | Pages cut | Size of the cut |
|
|
513
|
-
|---|---|---|
|
|
514
|
-
| Chrome, 72 photos, 100 MB | 5 | 7.0 MB (7%) |
|
|
515
|
-
| PowerPoint deck, 40 photos, 9.9 MB | 5 | 1.2 MB (13%) |
|
|
516
|
-
| Word report with a contents page, 7.5 MB | the contents page | 55 KB (0.7%) |
|
|
517
|
-
| All images in one shared list, 12 MB | 2 | 0.6 MB (5%) |
|
|
518
|
-
|
|
519
|
-
What a cut costs the server: the whole file is read into memory on every call.
|
|
520
|
-
|
|
521
|
-
| File | Time | Memory at the peak |
|
|
522
|
-
|---|---|---|
|
|
523
|
-
| 100 MB of photos, 900 objects | 0.06 s | about 300 MB |
|
|
524
|
-
| 100 MB written the way Ghostscript writes | 3 s | about 300 MB |
|
|
525
|
-
| 54 MB, 330,000 objects (a tagged table of 591 pages) | 4 to 5 s | about 700 MB |
|
|
526
|
-
|
|
527
|
-
A file that declares more than a million objects, or more than the free memory can hold, is not
|
|
528
|
-
cut: the tool tells the model that the pages cannot be shown.
|
|
529
|
-
|
|
530
|
-
Also handled: PDFs encrypted only to restrict printing or copying (they open without a password),
|
|
531
|
-
hidden layers (they stay hidden), rotated pages, and links. Not shown: a PDF that needs a password
|
|
532
|
-
to open, a damaged file, and a file in which this reader counts other pages than the text
|
|
533
|
-
extractor did. In each case the model gets a message and answers from the text.
|
|
78
|
+
Now in [PDFs and other formats](./guides/pdfs.md); the tool's arguments and limits are in [Tools](./reference/tools.md).
|
|
534
79
|
|
|
535
80
|
### Scanned PDFs
|
|
536
81
|
|
|
537
|
-
|
|
538
|
-
its original is kept, the model can read it by looking at its pages, so a scan of **up to 30
|
|
539
|
-
pages** is accepted. Every page of its text says that it has no text, and the model gets
|
|
540
|
-
`view_pages`. A longer scan is still refused: it could only be read five pages at a time, and it
|
|
541
|
-
could not be searched.
|
|
542
|
-
|
|
543
|
-
```ts
|
|
544
|
-
createLargeFiles({ store, keepOriginals: ["pdf"], scannedPdfPages: 30 }); // 30 is the default; 0 refuses all scans
|
|
545
|
-
```
|
|
546
|
-
|
|
547
|
-
A PDF with text on any page was always accepted and still is, however many of its pages are scans.
|
|
82
|
+
Now in [PDFs and other formats](./guides/pdfs.md).
|
|
548
83
|
|
|
549
84
|
### Live results
|
|
550
85
|
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
| Check | Result |
|
|
554
|
-
|---|---|
|
|
555
|
-
| Which bar of a chart is tallest and shortest, and their colours; a number on a sign in a photo. Text sent whole. | Looked at 2 pages unasked. All five facts right. From the text alone, the model said it could not see the chart. |
|
|
556
|
-
| The same, with the PDF searched instead | One look, all five right. |
|
|
557
|
-
| Ten questions that the text answers, about a PDF with a photo on each of its 60 pages | 0 of 10 looked at a page, with the text sent whole and with the PDF searched. 10 of 10 right both times. |
|
|
558
|
-
| A chart in one colour: a caption, but no mark | The model looked at the page and read the chart correctly. |
|
|
559
|
-
| Five pages in one look (five numbers on five signs) | All five right. 2,660 tokens for the pages. |
|
|
560
|
-
| A slide deck of 41 slides: a number on slide 8, a bar on slide 1 | Two looks, both right. |
|
|
561
|
-
| A PDF encrypted to restrict printing | One look, all right. |
|
|
562
|
-
| A scan of three pages (pictures of text) | One look at the three pages. Four of four facts right. |
|
|
563
|
-
| The request without tools that ends a question, after a look | Accepted, answered correctly, with the pages still in it. |
|
|
564
|
-
|
|
565
|
-
**Vertex AI**, with the same model (locations `global` and `eu`, November 2026): one look, two
|
|
566
|
-
looks in one model turn, one look that decides the next, the request without tools that ends a
|
|
567
|
-
question, and a PDF that is searched. All were accepted and answered correctly, with `vertexAi()`
|
|
568
|
-
and through an application's own URLs, streaming and not. Real files went through too: a number
|
|
569
|
-
on a photo in a 7.5 MB report written by Word, and five numbers from one look at five pages of a
|
|
570
|
-
25 MB file (a 2.2 MB PDF in the request). A page costs 532 tokens there as well.
|
|
86
|
+
Now in [Measurements](./measurements.md).
|
|
571
87
|
|
|
572
88
|
### Where the pages go: `toolFilePlacement`
|
|
573
89
|
|
|
574
|
-
|
|
575
|
-
|---|---|---|---|
|
|
576
|
-
| `"inside"` (default) | The PDF in the `parts` of its function response, which names it as `{ "$ref": "pages-12-14.pdf" }` | Works | Accepted and counted, but the model does not see the pages |
|
|
577
|
-
| `"beside"` | The function responses in one `user` turn, then the PDFs in a `user` turn of their own, each after a line that names it | Works | Works |
|
|
578
|
-
|
|
579
|
-
```ts
|
|
580
|
-
await askGemini({ ..., toolFilePlacement: "beside" }); // for Gemini 2.5
|
|
581
|
-
```
|
|
582
|
-
|
|
583
|
-
Keep the default for Gemini 3 models. Use `"beside"` for Gemini 2.5: it takes a file inside a
|
|
584
|
-
function response without an error, counts its tokens, and then answers that it cannot see the
|
|
585
|
-
page. Gemini 2.5 on the Gemini API was not checked (that model is closed to new users there), and
|
|
586
|
-
neither was Gemini 4.
|
|
587
|
-
|
|
588
|
-
With `"beside"` the files have a turn of their own because Vertex AI refuses a turn in which
|
|
589
|
-
anything follows a function response (HTTP 400, "Requests ending with a model turn are not
|
|
590
|
-
supported", seen with Gemini 3.5).
|
|
90
|
+
Now in [PDFs and other formats](./guides/pdfs.md); the option is in the [`llm-large-files/gemini`](./reference/gemini.md) and [`llm-large-files/anthropic`](./reference/anthropic.md) references.
|
|
591
91
|
|
|
592
92
|
### Your own loop
|
|
593
93
|
|
|
594
|
-
|
|
595
|
-
with nothing attached. So `view_pages` is declared only when you say that your loop delivers files:
|
|
596
|
-
|
|
597
|
-
```ts
|
|
598
|
-
const prepared = await files.prepare(documentIds, base, { owner, viewPages: true });
|
|
599
|
-
const tools = files.tools(prepared.plan.searchable, { viewPages: prepared.viewable });
|
|
600
|
-
|
|
601
|
-
const { result, files: attached } = await tools.executeWithFiles(call.name, call.args, {
|
|
602
|
-
maxFileBytes: Math.floor(roomLeftInYourRequest * 3 / 4), // base64 turns three bytes into four
|
|
603
|
-
maxPages: pagesYouStillAllowForThisQuestion,
|
|
604
|
-
});
|
|
605
|
-
// attached: [{ name: "pages-12-14.pdf", mimeType: "application/pdf", bytes }]
|
|
606
|
-
// Send `result` as the tool result and each file as a PDF with it, in your provider's form.
|
|
607
|
-
// For Gemini, once every call of the model's turn is answered:
|
|
608
|
-
contents.push(...geminiFunctionResponseTurns(answers)); // answers: [{ call, result, files: attached }]
|
|
609
|
-
```
|
|
610
|
-
|
|
611
|
-
`tools.execute("view_pages", ...)` returns only JSON, so it answers with an error instead of
|
|
612
|
-
pretending. `prepared.viewable` also lists PDFs that are not searched, because their text is in
|
|
613
|
-
the prompt: pass it as it is.
|
|
94
|
+
Now in [Your own adapter and loop](./guides/own-adapter-and-loop.md).
|
|
614
95
|
|
|
615
96
|
### Custom extractors
|
|
616
97
|
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
```ts
|
|
620
|
-
createLargeFiles({
|
|
621
|
-
store,
|
|
622
|
-
extractors: {
|
|
623
|
-
pdf: async (bytes, { name }) => ({ text: await myOcrService(bytes), pages: undefined }),
|
|
624
|
-
},
|
|
625
|
-
});
|
|
626
|
-
```
|
|
627
|
-
|
|
628
|
-
Formats come from `detectFormat(name, mimeType)`; unknown extensions use the `text` extractor.
|
|
98
|
+
Now in [PDFs and other formats](./guides/pdfs.md); the type is [`Extractor`](./reference/library.md#extractor), and what it returns is [`ExtractedText`](./reference/library.md#extractedtext).
|