llm-large-files 0.8.2 → 0.9.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 +535 -0
- package/README.md +9 -10
- package/dist/access.d.ts +86 -0
- package/dist/access.d.ts.map +1 -0
- package/dist/access.js +131 -0
- package/dist/access.js.map +1 -0
- package/dist/ask.d.ts +45 -1
- package/dist/ask.d.ts.map +1 -1
- package/dist/ask.js +181 -20
- package/dist/ask.js.map +1 -1
- package/dist/cache.d.ts +12 -3
- package/dist/cache.d.ts.map +1 -1
- package/dist/cache.js +18 -5
- package/dist/cache.js.map +1 -1
- package/dist/encoding.js +5 -3
- package/dist/encoding.js.map +1 -1
- package/dist/error-info.d.ts +57 -0
- package/dist/error-info.d.ts.map +1 -0
- package/dist/error-info.js +217 -0
- package/dist/error-info.js.map +1 -0
- package/dist/errors.d.ts +72 -8
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +102 -15
- package/dist/errors.js.map +1 -1
- package/dist/events.d.ts +35 -9
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js +42 -11
- package/dist/events.js.map +1 -1
- package/dist/evidence.d.ts +4 -2
- package/dist/evidence.d.ts.map +1 -1
- package/dist/evidence.js +14 -3
- package/dist/evidence.js.map +1 -1
- package/dist/extract.d.ts +32 -8
- package/dist/extract.d.ts.map +1 -1
- package/dist/extract.js +46 -35
- package/dist/extract.js.map +1 -1
- package/dist/gemini/adapter.d.ts +19 -3
- package/dist/gemini/adapter.d.ts.map +1 -1
- package/dist/gemini/adapter.js +84 -12
- package/dist/gemini/adapter.js.map +1 -1
- package/dist/gemini/chat.d.ts +18 -6
- package/dist/gemini/chat.d.ts.map +1 -1
- package/dist/gemini/chat.js +2 -0
- package/dist/gemini/chat.js.map +1 -1
- package/dist/gemini/index.d.ts +1 -1
- package/dist/gemini/index.d.ts.map +1 -1
- package/dist/index.d.ts +9 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/ingest.d.ts +70 -0
- package/dist/ingest.d.ts.map +1 -0
- package/dist/ingest.js +868 -0
- package/dist/ingest.js.map +1 -0
- package/dist/library.d.ts +169 -34
- package/dist/library.d.ts.map +1 -1
- package/dist/library.js +324 -591
- package/dist/library.js.map +1 -1
- package/dist/map-reduce.js +3 -3
- package/dist/map-reduce.js.map +1 -1
- package/dist/model.d.ts +43 -2
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +63 -3
- package/dist/model.js.map +1 -1
- package/dist/node/handlers.d.ts +226 -14
- package/dist/node/handlers.d.ts.map +1 -1
- package/dist/node/handlers.js +423 -181
- package/dist/node/handlers.js.map +1 -1
- package/dist/node/index.d.ts +3 -2
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +2 -1
- package/dist/node/index.js.map +1 -1
- package/dist/node/ingest-child.d.ts +7 -1
- package/dist/node/ingest-child.d.ts.map +1 -1
- package/dist/node/ingest-child.js +18 -3
- package/dist/node/ingest-child.js.map +1 -1
- package/dist/node/isolated.d.ts +3 -1
- package/dist/node/isolated.d.ts.map +1 -1
- package/dist/node/isolated.js +11 -4
- package/dist/node/isolated.js.map +1 -1
- package/dist/node/originals.d.ts +19 -0
- package/dist/node/originals.d.ts.map +1 -0
- package/dist/node/originals.js +82 -0
- package/dist/node/originals.js.map +1 -0
- package/dist/node/spool.d.ts +102 -0
- package/dist/node/spool.d.ts.map +1 -0
- package/dist/node/spool.js +365 -0
- package/dist/node/spool.js.map +1 -0
- package/dist/node/sse.d.ts +4 -3
- package/dist/node/sse.d.ts.map +1 -1
- package/dist/node/sse.js +7 -6
- package/dist/node/sse.js.map +1 -1
- package/dist/pdf-layout.d.ts.map +1 -1
- package/dist/pdf-layout.js +71 -12
- package/dist/pdf-layout.js.map +1 -1
- package/dist/ranged.d.ts +15 -11
- package/dist/ranged.d.ts.map +1 -1
- package/dist/ranged.js +42 -11
- package/dist/ranged.js.map +1 -1
- package/dist/retry.d.ts +3 -1
- package/dist/retry.d.ts.map +1 -1
- package/dist/retry.js +6 -2
- package/dist/retry.js.map +1 -1
- package/dist/scope.d.ts +25 -0
- package/dist/scope.d.ts.map +1 -0
- package/dist/scope.js +60 -0
- package/dist/scope.js.map +1 -0
- package/dist/source.d.ts +60 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +170 -0
- package/dist/source.js.map +1 -0
- package/dist/stores.d.ts +120 -31
- package/dist/stores.d.ts.map +1 -1
- package/dist/stores.js +582 -139
- package/dist/stores.js.map +1 -1
- package/dist/testing/fake-fetch.d.ts +31 -0
- package/dist/testing/fake-fetch.d.ts.map +1 -0
- package/dist/testing/fake-fetch.js +66 -0
- package/dist/testing/fake-fetch.js.map +1 -0
- package/dist/testing/fake-model.d.ts +38 -0
- package/dist/testing/fake-model.d.ts.map +1 -0
- package/dist/testing/fake-model.js +62 -0
- package/dist/testing/fake-model.js.map +1 -0
- package/dist/testing/index.d.ts +7 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +9 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/store-checks.d.ts +72 -0
- package/dist/testing/store-checks.d.ts.map +1 -0
- package/dist/testing/store-checks.js +619 -0
- package/dist/testing/store-checks.js.map +1 -0
- package/dist/tools.d.ts +25 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +135 -13
- package/dist/tools.js.map +1 -1
- package/dist/types.d.ts +116 -13
- package/dist/types.d.ts.map +1 -1
- package/docs/README.md +17 -8
- package/docs/getting-started.md +18 -13
- package/docs/guides/anthropic.md +12 -8
- package/docs/guides/conversations.md +112 -47
- package/docs/guides/costs-and-limits.md +16 -7
- package/docs/guides/deployment.md +284 -83
- package/docs/guides/errors-and-reliability.md +161 -23
- package/docs/guides/own-adapter-and-loop.md +27 -10
- package/docs/guides/pdfs.md +123 -29
- package/docs/guides/storage.md +633 -145
- package/docs/guides/testing.md +78 -129
- package/docs/guides/vertex-and-gemini.md +10 -7
- package/docs/guides/web-app.md +312 -78
- package/docs/how-it-works.md +35 -8
- package/docs/measurements.md +43 -8
- package/docs/publishing.md +112 -36
- package/docs/reference/ask.md +154 -13
- package/docs/reference/errors-and-retries.md +166 -14
- package/docs/reference/events.md +53 -20
- package/docs/reference/gemini.md +70 -21
- package/docs/reference/library.md +706 -165
- package/docs/reference/node.md +624 -110
- package/docs/reference/testing.md +311 -0
- package/docs/reference/tools.md +15 -5
- package/docs/security.md +304 -67
- package/docs/troubleshooting.md +144 -65
- package/docs/upgrading-0.9.md +890 -0
- package/package.json +12 -3
- package/src/access.ts +144 -0
- package/src/ask.ts +230 -19
- package/src/cache.ts +20 -5
- package/src/encoding.ts +6 -4
- package/src/error-info.ts +255 -0
- package/src/errors.ts +129 -15
- package/src/events.ts +86 -21
- package/src/evidence.ts +14 -3
- package/src/extract.ts +50 -33
- package/src/gemini/adapter.ts +97 -11
- package/src/gemini/chat.ts +20 -6
- package/src/gemini/index.ts +1 -1
- package/src/index.ts +9 -6
- package/src/ingest.ts +927 -0
- package/src/library.ts +467 -601
- package/src/map-reduce.ts +3 -3
- package/src/model.ts +69 -5
- package/src/node/handlers.ts +630 -183
- package/src/node/index.ts +13 -2
- package/src/node/ingest-child.ts +23 -5
- package/src/node/isolated.ts +11 -4
- package/src/node/originals.ts +78 -0
- package/src/node/spool.ts +365 -0
- package/src/node/sse.ts +7 -6
- package/src/pdf-layout.ts +69 -11
- package/src/ranged.ts +34 -13
- package/src/retry.ts +6 -2
- package/src/scope.ts +63 -0
- package/src/source.ts +178 -0
- package/src/stores.ts +635 -140
- package/src/testing/fake-fetch.ts +80 -0
- package/src/testing/fake-model.ts +78 -0
- package/src/testing/index.ts +11 -0
- package/src/testing/store-checks.ts +660 -0
- package/src/tools.ts +150 -14
- package/src/types.ts +106 -13
package/docs/getting-started.md
CHANGED
|
@@ -9,17 +9,15 @@ adding the library to an app. Each step is short and links to the guide that exp
|
|
|
9
9
|
npm install llm-large-files
|
|
10
10
|
```
|
|
11
11
|
|
|
12
|
-
The library needs Node.js
|
|
13
|
-
it. From CommonJS code, `require("llm-large-files")` works
|
|
14
|
-
modules with `require()` by default
|
|
15
|
-
On Node 21 and on 22.0 to 22.11, load it with `import("llm-large-files")`.
|
|
12
|
+
The library needs Node.js 22.12 or later (20.19 until 0.9.0). It is an ES module, and its
|
|
13
|
+
TypeScript types come with it. From CommonJS code, `require("llm-large-files")` works: Node 22.12
|
|
14
|
+
and later load ES modules with `require()` by default.
|
|
16
15
|
|
|
17
16
|
Text, Markdown, logs, JSON and CSV need nothing more. Three optional packages add the rest:
|
|
18
17
|
|
|
19
18
|
```bash
|
|
20
19
|
npm install jszip unpdf @cantoo/pdf-lib # Node 24 and later
|
|
21
20
|
npm install jszip unpdf@1 @cantoo/pdf-lib # Node 22
|
|
22
|
-
npm install jszip unpdf@1.7.0 @cantoo/pdf-lib # Node 20
|
|
23
21
|
```
|
|
24
22
|
|
|
25
23
|
| Package | What it enables |
|
|
@@ -34,9 +32,9 @@ turn on `keepOriginals`: without it, every look at a page fails. The details are
|
|
|
34
32
|
[PDFs](./guides/pdfs.md).
|
|
35
33
|
|
|
36
34
|
On Node 24 and later, `npm install unpdf` installs unpdf 2, which asks for Node 24. On Node 22,
|
|
37
|
-
install `unpdf@1
|
|
38
|
-
|
|
39
|
-
`The unpdf installed
|
|
35
|
+
install `unpdf@1`. An unpdf that would stop the server on your version of Node is refused at the
|
|
36
|
+
first PDF, with a message that says what to install:
|
|
37
|
+
`The unpdf installed would stop the whole server at the first PDF file on Node 22, so PDF files are not read. Install unpdf 1.6.1 or later, or run Node 24 or later.`
|
|
40
38
|
|
|
41
39
|
The Vertex AI samples on this page also use Google's auth library, which is not a dependency of
|
|
42
40
|
this package:
|
|
@@ -274,7 +272,9 @@ Keep the `id` with the chat session. Not on a Node HTTP server, or taking multip
|
|
|
274
272
|
|
|
275
273
|
`getOwner` returns the signed-in user's ID, or `undefined` for an anonymous upload. An empty
|
|
276
274
|
string, a string of spaces or `null` would be one owner shared by every caller without a user, so
|
|
277
|
-
the upload is refused: `500` with `{ "error": "Upload failed (empty owner)." }`.
|
|
275
|
+
the upload is refused: `500` with `{ "error": "Upload failed (empty owner)." }`. (With
|
|
276
|
+
`requireOwner: true`, `undefined` and `null` are answered `401` "Sign in first.", new in 0.9.0.)
|
|
277
|
+
Return the ID
|
|
278
278
|
itself, not the user object (`{ "error": "Upload failed (owner is not a string)." }`). Both come
|
|
279
279
|
before the body is read.
|
|
280
280
|
|
|
@@ -451,15 +451,20 @@ git clone https://github.com/renanbazinin/llm-large-files
|
|
|
451
451
|
cd llm-large-files
|
|
452
452
|
npm install
|
|
453
453
|
npm run build
|
|
454
|
-
GEMINI_API_KEY=... node examples/server.ts # then open http://
|
|
454
|
+
GEMINI_API_KEY=... node examples/server.ts # then open http://127.0.0.1:3000
|
|
455
455
|
GEMINI_API_KEY=... node examples/ask-cli.ts ./big.log "What failed first, and when?"
|
|
456
456
|
```
|
|
457
457
|
|
|
458
458
|
- `examples/server.ts` is a chat server in one file, on `node:http`: an upload route
|
|
459
459
|
(`createDocumentHandlers`), answers streamed as Server-Sent Events, and a small web page. It
|
|
460
|
-
stores uploads in `.data/large-files` and keeps PDFs, so the model can look at their pages.
|
|
461
|
-
|
|
462
|
-
|
|
460
|
+
stores uploads in `.data/large-files` and keeps PDFs, so the model can look at their pages. It
|
|
461
|
+
reads PDF and Office files in child processes (`createIsolatedExtractors`). `GEMINI_MODEL` picks
|
|
462
|
+
the model (default `gemini-3.8-flash`), and `GEMINI_EXPLORER_MODEL` adds an explorer model.
|
|
463
|
+
- It is a demo, not for production as is. It listens on `127.0.0.1` only (`HOST` and `PORT` change
|
|
464
|
+
that). It has no sign-in: the page gives each browser a random owner in an `HttpOnly`,
|
|
465
|
+
`SameSite=Strict` cookie, so two browsers cannot read each other's files or chats, but whoever
|
|
466
|
+
has the cookie is that owner. Since 0.9.0; before, every upload was anonymous and the server
|
|
467
|
+
listened on every interface.
|
|
463
468
|
- `examples/ask-cli.ts` asks one question about one file from the command line. Two more
|
|
464
469
|
arguments are optional: the model, and an explorer model.
|
|
465
470
|
|
package/docs/guides/anthropic.md
CHANGED
|
@@ -296,10 +296,13 @@ the conversation they were written in, so an earlier turn that is rewritten make
|
|
|
296
296
|
invalid. The loop therefore never rewrites a turn of a question in which a model that is
|
|
297
297
|
append-only takes part: Claude alone, Claude answering, or Claude exploring. Three things change.
|
|
298
298
|
|
|
299
|
-
**Old results are never replaced by stand-ins.** With other models, when the
|
|
300
|
-
runs short, the loop replaces the results of older tool rounds with short
|
|
301
|
-
says "Older tool results were shortened to make room in the request". With
|
|
302
|
-
|
|
299
|
+
**Old results are never replaced by stand-ins during a question.** With other models, when the
|
|
300
|
+
room in the request runs short, the loop replaces the results of older tool rounds with short
|
|
301
|
+
stand-ins, and `steps` says "Older tool results were shortened to make room in the request". With
|
|
302
|
+
Claude every result of the question stays as it was. The stored rounds of earlier questions are
|
|
303
|
+
the exception (new in 0.9.0): when they make the first request of a question larger than its
|
|
304
|
+
room, their results are replaced before that request is sent. They hold no thinking blocks, and
|
|
305
|
+
nothing of the question was sent yet. Before, such a question was refused and failed. When the calls of a turn do not all fit, the last ones are answered "Not run:
|
|
303
306
|
there is no room left in this conversation for another tool result. Answer now, from what the
|
|
304
307
|
tools returned so far.", and the next request is the answer. This holds for the whole question:
|
|
305
308
|
with Claude answering and Gemini exploring, the explorer's rounds are not shortened either.
|
|
@@ -334,9 +337,10 @@ What this means for your application:
|
|
|
334
337
|
- **Set `contextTokens`** to your model's window, and keep `maxTokens` no larger than you need. A
|
|
335
338
|
right budget is what keeps Claude's requests inside its window.
|
|
336
339
|
- **Keep the history in check between questions.** The library shortens no plain turn of the
|
|
337
|
-
history, for any model
|
|
338
|
-
|
|
339
|
-
|
|
340
|
+
history, for any model. With Claude it shortens the stored tool rounds only before the first
|
|
341
|
+
request, and only as far as the request needs (since 0.9.0). A long history leaves less room
|
|
342
|
+
for each new question: its documents become cards sooner, fewer results fit, the stored rounds
|
|
343
|
+
are sent as stand-ins, and at last its plain turns alone are too large. Between two questions the history you pass is yours
|
|
340
344
|
to shorten: it holds no thinking blocks, and the rule above is about the turns of one question.
|
|
341
345
|
Keeping the text of every turn and the `toolTurns` of the last few questions is a simple rule.
|
|
342
346
|
The prompt cache misses once from the first turn you change. See
|
|
@@ -571,7 +575,7 @@ What the pair changes:
|
|
|
571
575
|
Gemini's scale, Claude's by Claude's, so the estimate rises when either model counted more than
|
|
572
576
|
its scale says. Before 0.6.0 every count was read by the largest scale, and a Gemini explorer's
|
|
573
577
|
could not raise it.
|
|
574
|
-
- **No result is shortened.** Claude is append-only, so the rule
|
|
578
|
+
- **No result of the question is shortened.** Claude is append-only, so the rule
|
|
575
579
|
[above](#a-conversation-that-may-only-grow) holds for the explorer's rounds too.
|
|
576
580
|
- **Claude gets the calls and what they returned**, in its own form: an assistant turn of
|
|
577
581
|
`tool_use` blocks and a user turn of `tool_result` blocks. A call that came without an ID is
|
|
@@ -32,8 +32,8 @@ The rules:
|
|
|
32
32
|
- **A `native` part goes unchanged to every model of the question**: a Gemini part to `gemini()`, a
|
|
33
33
|
Chat Completions content part to `openAiCompatible()`, a Messages API block to `anthropic()`.
|
|
34
34
|
With an [explorer model](./explorer-model.md) of another provider, both models get it, so both
|
|
35
|
-
must read it. A native part that also has `text` is
|
|
36
|
-
the length of its JSON, as media.
|
|
35
|
+
must read it. A native part that also has `text` is counted in tokens by that text, and in bytes
|
|
36
|
+
by the larger of that text and its JSON; one without is sized by the length of its JSON, as media.
|
|
37
37
|
|
|
38
38
|
Annotate the array as `ChatMessage[]`. Without the annotation TypeScript widens `role` to
|
|
39
39
|
`string`, and the array is not accepted as `messages`.
|
|
@@ -66,12 +66,11 @@ const messages: ChatMessage[] = [
|
|
|
66
66
|
const second = await askWithFiles({ files, model, documentIds, messages });
|
|
67
67
|
```
|
|
68
68
|
|
|
69
|
-
`askGemini` takes the conversation in one of two options. `messages` takes
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
model is then passed as `explorer` ([Explorer model](./explorer-model.md)).
|
|
69
|
+
`askGemini` takes the conversation in one of two options. `messages` takes what `askWithFiles`
|
|
70
|
+
takes (`ChatMessage[]`): plain turns, stored tool rounds and, since 0.9.0, turns with native parts,
|
|
71
|
+
such as the chart's turn above. Before 0.9.0 it took only `Array<ChatTurn | ToolTurn>`. `contents` takes
|
|
72
|
+
Gemini `contents`, native parts included, and converts them with `fromGeminiContents`
|
|
73
|
+
([below](#gemini-contents)). When both are given, `contents` is used.
|
|
75
74
|
|
|
76
75
|
Every field of these types is in the reference: [`askWithFiles(options)`](../reference/ask.md#askwithfilesoptions)
|
|
77
76
|
and the rest of [ask.md](../reference/ask.md).
|
|
@@ -103,12 +102,18 @@ tool calls of one model turn, each with what its tool returned:
|
|
|
103
102
|
`tool_turns_left`, the tool turns the question had left then. A stand-in does not.
|
|
104
103
|
- **`tool_turns_left` is stored, and not sent again.** When you pass the rounds back in a later
|
|
105
104
|
question, the results go to the model without it: its count was of the turns their own question
|
|
106
|
-
had left, and a model could take a 0 there for its own.
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
105
|
+
had left, and a model could take a 0 there for its own.
|
|
106
|
+
- **So the first request of a follow-up differs from the requests of the question before it**,
|
|
107
|
+
from that question's first tool round on, for two reasons. The results are sent without
|
|
108
|
+
`tool_turns_left`. And the model turns of the round are sent as their calls alone: within its own
|
|
109
|
+
question a round went back with the model's own form of its turn (Gemini's thought signatures,
|
|
110
|
+
Claude's thinking blocks), and a stored round has none ([below](#what-toolturns-holds)). Either
|
|
111
|
+
one alone makes the provider's prompt cache miss from there, once in each follow-up question, in
|
|
112
|
+
its first request. The rounds of older questions were already sent that way and still match, and
|
|
110
113
|
the documents and cards before the rounds are still read from the cache
|
|
111
|
-
([Costs and limits](./costs-and-limits.md#prompt-caching)).
|
|
114
|
+
([Costs and limits](./costs-and-limits.md#prompt-caching)). Before 0.9.0 these pages named
|
|
115
|
+
`tool_turns_left` alone; keeping the count in the results would not save the miss for a model
|
|
116
|
+
that thinks. Keeping the raw turns is planned for 0.10.0.
|
|
112
117
|
- Calls that were not run are there too, with an `error`: for example refused for lack of room,
|
|
113
118
|
past `limits.callsPerTurn`, or whose arguments could not be read. So are pointers to results already
|
|
114
119
|
shown ([Repeated calls](#repeated-calls-in-a-follow-up)).
|
|
@@ -118,6 +123,7 @@ tool calls of one model turn, each with what its tool returned:
|
|
|
118
123
|
them." }`.
|
|
119
124
|
- **No provider's own form.** A stored round has no thought signatures, thinking blocks or other
|
|
120
125
|
raw parts of the model turn. Any adapter can be given it, and builds the turn from the calls.
|
|
126
|
+
That is one of the two reasons the prompt cache misses once in a follow-up (above).
|
|
121
127
|
- Only the rounds of this question. The stored rounds you passed in `messages` are not repeated.
|
|
122
128
|
- With an explorer model they are the explorer's rounds. Its words are not kept.
|
|
123
129
|
- They are there when `answered` is false, and in `partialResultOf(error)` when the question failed.
|
|
@@ -261,7 +267,11 @@ its own in some ways, and not in others.
|
|
|
261
267
|
whole, which the model reaches only through the tools.
|
|
262
268
|
- **It shortens old tool results.** When the results of a new turn do not fit, the results of all
|
|
263
269
|
but the last three tool rounds are replaced by short stand-ins, then all but the last two, then
|
|
264
|
-
all but the last one. Stored rounds of earlier questions are the oldest, so they go first.
|
|
270
|
+
all but the last one. Stored rounds of earlier questions are the oldest, so they go first.
|
|
271
|
+
Since 0.9.0 this is done before the first request too, when the stored rounds make it larger
|
|
272
|
+
than its room in tokens (the loop's estimate, which the recovery from a refused request uses
|
|
273
|
+
too; a request is not shortened for its bytes alone): the first request goes out shortened,
|
|
274
|
+
instead of being refused by the provider and sent again. A
|
|
265
275
|
stand-in keeps the document and the lines or pages that were shown, and tells the model to read
|
|
266
276
|
them again if it needs them:
|
|
267
277
|
|
|
@@ -270,7 +280,9 @@ its own in some ways, and not in others.
|
|
|
270
280
|
```
|
|
271
281
|
|
|
272
282
|
Errors and results under 700 characters are left as they are, except a `view_pages` result
|
|
273
|
-
that showed pages: it is always replaced, and its pages go with it.
|
|
283
|
+
that showed pages: it is always replaced, and its pages go with it. Since 0.9.0 a result that
|
|
284
|
+
its stand-in would not make smaller is left as it is too, such as a search counted by group with
|
|
285
|
+
a few short groups. A whole-file read keeps the
|
|
274
286
|
first 6,000 characters of its answer. `steps` then holds "Older tool results were shortened to
|
|
275
287
|
make room in the request".
|
|
276
288
|
- **It stops running tools when nothing fits.** A call that cannot get 1,500 characters of room is
|
|
@@ -285,10 +297,12 @@ its own in some ways, and not in others.
|
|
|
285
297
|
What it does not do:
|
|
286
298
|
|
|
287
299
|
- **It never shortens a text turn**, a question or an answer, and never touches native parts.
|
|
288
|
-
- **It never shortens anything for an append-only model.** When a model of the
|
|
289
|
-
`appendOnly` (Claude does: [Claude](./anthropic.md)), no turn is rewritten
|
|
290
|
-
|
|
291
|
-
question ([below](#repeated-calls-in-a-follow-up)) still runs.
|
|
300
|
+
- **It never shortens anything of the question for an append-only model.** When a model of the
|
|
301
|
+
question declares `appendOnly` (Claude does: [Claude](./anthropic.md)), no turn is rewritten
|
|
302
|
+
during the question, and its results are not replaced by stand-ins. The check of stored pointers
|
|
303
|
+
at the start of a question ([below](#repeated-calls-in-a-follow-up)) still runs, and since 0.9.0
|
|
304
|
+
so does the shortening of stored rounds before the first request: they hold no thinking blocks,
|
|
305
|
+
and nothing of the question was sent yet. Before, such a question failed on Claude.
|
|
292
306
|
- **It does not change your stored rounds.** Stand-ins exist in the requests of one question. The
|
|
293
307
|
`toolTurns` it returns are the new rounds as the model last had them.
|
|
294
308
|
|
|
@@ -435,17 +449,36 @@ export async function next(files: LargeFiles, endpoint: GeminiEndpoint, document
|
|
|
435
449
|
- `geminiToolTurns` gives, for each round, a `model` turn of `functionCall` parts and a `user` turn
|
|
436
450
|
of `functionResponse` parts. A call's `id` is in both when it has one. Each result is given
|
|
437
451
|
without its `tool_turns_left`, as the loop sends the rounds of an earlier question. `contents`
|
|
438
|
-
that 0.5.0 converted keep it
|
|
439
|
-
- `fromGeminiContents` makes a `"user"` turn a user turn and a `"model"` turn an assistant turn
|
|
440
|
-
|
|
441
|
-
part
|
|
442
|
-
and the
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
452
|
+
that 0.5.0 converted keep it; from 0.9.0 their rounds are sent without it (below).
|
|
453
|
+
- `fromGeminiContents` makes a `"user"` turn a user turn and a `"model"` turn an assistant turn,
|
|
454
|
+
and the rounds that `geminiToolTurns` wrote stored rounds again (below). A text part is text. A
|
|
455
|
+
text part with more fields (a `thoughtSignature`) is kept whole as a native part: its tokens are
|
|
456
|
+
counted from its text, and its bytes from the whole part. Every other part is a native part, as
|
|
457
|
+
it is: an image, an inline PDF, and the `functionCall` and `functionResponse` parts of your own
|
|
458
|
+
functions.
|
|
459
|
+
|
|
460
|
+
Since 0.9.0, `contents` and `messages` with `ToolTurn`s behave alike. `fromGeminiContents` turns
|
|
461
|
+
the rounds `geminiToolTurns` wrote back into stored rounds: a `model` turn of `functionCall` parts
|
|
462
|
+
of the library's tools, followed by the `user` turn of their `functionResponse` parts. For the
|
|
463
|
+
rounds that `geminiToolTurns` wrote, the request is the same, byte for byte, as before. But now
|
|
464
|
+
the loop can shorten them to make room, point a
|
|
465
|
+
repeated call to them, and size them as it sizes `toolTurns`. Before, it saw them as plain parts:
|
|
466
|
+
a repeated search was run again, nothing could be shortened, and they were sized as media, about
|
|
467
|
+
15,000 tokens short for a round of two calls.
|
|
468
|
+
|
|
469
|
+
- A turn of your own functions, or one with a thought signature or a file in a response, stays as
|
|
470
|
+
it is. Its tokens are counted from the JSON of its calls and responses, and its bytes from the
|
|
471
|
+
whole parts, a thought signature included.
|
|
472
|
+
- A round at the end of `contents`, with no turn after it, stays as it is too. Its responses are
|
|
473
|
+
the turn the model answers, as when your application ran the tools in a loop of its own and
|
|
474
|
+
hands the conversation over.
|
|
475
|
+
- `fromGeminiContents(contents, { rounds: false })` keeps every turn as its parts, as 0.8 did.
|
|
476
|
+
With `askGemini`, pass it as `messages`.
|
|
477
|
+
- The stored rounds come back without `tool_turns_left`, as stored `ToolTurn`s are sent. The
|
|
478
|
+
rounds that `geminiToolTurns` wrote never had it. Rounds that 0.5.0 converted have it in their
|
|
479
|
+
responses: from 0.9.0 they are lifted too, and sent without it, and a call without `args` is
|
|
480
|
+
sent with `args: {}`. Such a conversation misses the prompt cache once, in its first request
|
|
481
|
+
after the upgrade, from that round on.
|
|
449
482
|
|
|
450
483
|
### Chat Completions messages
|
|
451
484
|
|
|
@@ -581,11 +614,20 @@ the explorer's calls and results, built in its own form ([Explorer model](./expl
|
|
|
581
614
|
|
|
582
615
|
- **Pass the same `owner` with every question.** A document ingested with another owner is treated
|
|
583
616
|
as missing. The owner is a string that is not empty: `ingest` refuses `""`, which
|
|
584
|
-
`user?.id ?? ""` gives a caller who is not signed in. How to choose the owner
|
|
585
|
-
|
|
617
|
+
`user?.id ?? ""` gives a caller who is not signed in. How to choose the owner is in
|
|
618
|
+
[Storage](./storage.md#how-ownership-works).
|
|
619
|
+
- **Pass the chat as `scope`**, new in 0.9.0: `scope: [userId, sessionId]` at ingest and with every
|
|
620
|
+
question. A document of another chat is then treated as missing, even one of the same user
|
|
621
|
+
([Scopes](./storage.md#scopes)).
|
|
622
|
+
- **Give the documents IDs of your own** (`IngestInput.id`, new in 0.9.0), such as the ID of the
|
|
623
|
+
upload. The stored rounds (`toolTurns`) name the documents by ID. A file ingested again under the
|
|
624
|
+
same ID (from the upload you kept, once it expired) is found by those rounds and by the session
|
|
625
|
+
record as it is: nothing to rewrite. With the IDs the library makes, each ingest gives a new ID,
|
|
626
|
+
and the rounds keep naming the old one.
|
|
586
627
|
- **Keep the documents with the conversation, not with one message.** Pass all of them in
|
|
587
628
|
`documentIds` on every question. They are planned again each time.
|
|
588
|
-
- **A document that is gone
|
|
629
|
+
- **A document that is gone is settled before any model request**, by `onMissingDocuments`
|
|
630
|
+
([below](#documents-that-are-gone)). By default the question stops: one that expired, was
|
|
589
631
|
deleted, or belongs to another owner raises `DocumentsNotFoundError`, with the IDs in `.ids` and
|
|
590
632
|
the message "A document is no longer available: doc_…. Upload it again." `partialResultOf` gives
|
|
591
633
|
undefined for it: nothing was asked.
|
|
@@ -598,26 +640,49 @@ the explorer's calls and results, built in its own form ([Explorer model](./expl
|
|
|
598
640
|
document: its expiry (`ttlMs` of `createLargeFiles`) and `files.delete`. Your rules for keeping and
|
|
599
641
|
deleting data must cover them ([Security](../security.md), [Storage](./storage.md)).
|
|
600
642
|
|
|
601
|
-
|
|
602
|
-
|
|
643
|
+
### Documents that are gone
|
|
644
|
+
|
|
645
|
+
A chat often lives longer than its documents: they expire after `ttlMs` (72 hours by default), or
|
|
646
|
+
are deleted. Before 0.9.0 the next question then failed whole with `DocumentsNotFoundError`, and
|
|
647
|
+
the application caught it, took the IDs off the chat and asked again. Since 0.9.0
|
|
648
|
+
`onMissingDocuments` does it in the question ([missing documents](../reference/ask.md#missing-documents)):
|
|
649
|
+
|
|
650
|
+
- `"drop"` answers without them. The model is told, in one line of the system instruction, how many
|
|
651
|
+
files are no longer available, so it can say so when the question needs them. `result.missing`
|
|
652
|
+
lists their IDs, and a `documents_missing` notice names them, for the page.
|
|
653
|
+
- A function can ingest them again, from the uploads you kept, under the same IDs, and return
|
|
654
|
+
`{ retry: true }`; or under new IDs, and return `{ replace: { [oldId]: newId } }`. The question is
|
|
655
|
+
then planned again, once, with them.
|
|
656
|
+
|
|
657
|
+
With the session sample above, the files that are gone are taken off the chat, and the user is told:
|
|
603
658
|
|
|
604
659
|
```ts
|
|
605
|
-
import {
|
|
660
|
+
import { askGemini } from "llm-large-files/gemini";
|
|
606
661
|
|
|
607
|
-
// `
|
|
608
|
-
/** Asks the question
|
|
609
|
-
export async function
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
662
|
+
// `files`, `endpoint`, `sessions`, `turns` and `historyOf` are those of the session sample above.
|
|
663
|
+
/** Asks the question without the files that are gone. Returns what to tell the user about them. */
|
|
664
|
+
export async function askWithoutGone(sessionId: string, userId: string, question: string, onText: (delta: string) => void, signal: AbortSignal): Promise<string | undefined> {
|
|
665
|
+
const session = await sessions.findOne({ _id: sessionId, userId });
|
|
666
|
+
if (!session) throw new Error("No such chat.");
|
|
667
|
+
const earlier = await turns.find({ sessionId }).sort({ index: 1 }).toArray();
|
|
668
|
+
const result = await askGemini({
|
|
669
|
+
files, endpoint, owner: userId, documentIds: session.documentIds, messages: historyOf(earlier, question),
|
|
670
|
+
onMissingDocuments: "drop", onText, signal,
|
|
671
|
+
});
|
|
672
|
+
if (!result.missing) return undefined;
|
|
673
|
+
await sessions.updateOne({ _id: sessionId, userId }, { $pull: { documentIds: { $in: result.missing } } });
|
|
674
|
+
return result.missing.length === 1 ? "A file of this chat is no longer available. Please attach it again." : `${result.missing.length} files of this chat are no longer available. Please attach them again.`;
|
|
618
675
|
}
|
|
619
676
|
```
|
|
620
677
|
|
|
678
|
+
- Keep its turn as the sample above does: `result.toolTurns` and the answer.
|
|
679
|
+
- With your own IDs ([above](#owners-and-documents-across-turns)), the uploads you keep can bring
|
|
680
|
+
a file back instead: the sample under [missing documents](../reference/ask.md#missing-documents)
|
|
681
|
+
ingests each one again from its upload and returns `{ retry: true }`.
|
|
682
|
+
- A tool call that finds a document gone during the question (deleted while the question ran)
|
|
683
|
+
answers the model that it is no longer available, and the question gets a `document_gone`
|
|
684
|
+
notice ([events](../reference/events.md#noticecode-and-noticedisplay)).
|
|
685
|
+
|
|
621
686
|
## Related pages
|
|
622
687
|
|
|
623
688
|
- [Web app](./web-app.md): uploads, streaming the answer, cancelling.
|
|
@@ -687,13 +687,22 @@ What the library changes:
|
|
|
687
687
|
names the files that are searched, changes with it.
|
|
688
688
|
- Within a question, when room runs short, older tool results are replaced by short stand-ins. The
|
|
689
689
|
request changes from the first round replaced. A model whose conversation may only grow (Claude)
|
|
690
|
-
has nothing replaced.
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
690
|
+
has nothing of the question replaced. Since 0.9.0 the stored rounds of earlier questions are
|
|
691
|
+
replaced before the first request when they make it larger than its room in tokens (by the
|
|
692
|
+
library's estimate, as after a refused request; not for its bytes alone), for every model: the
|
|
693
|
+
request changes from the first stored round replaced, as it did in 0.8 after the provider refused
|
|
694
|
+
the request, but without the refused request.
|
|
695
|
+
- The tool rounds of an earlier question are not sent as they were in their own question, for two
|
|
696
|
+
reasons. Their results go without `tool_turns_left`. And their model turns go as the calls alone:
|
|
697
|
+
a stored round keeps no thought signatures (Gemini) or thinking blocks (Claude), which the
|
|
698
|
+
requests of its own question sent back. So the first request of a follow-up question differs
|
|
699
|
+
from the requests of the question before it from that question's first tool round on, and the
|
|
700
|
+
cache misses from there, once in each follow-up question: those rounds are billed at the full
|
|
701
|
+
price in that request (with Claude's `cache_control`, as a cache write). The rounds of older
|
|
702
|
+
questions were already sent that way, and still match. The documents and cards before them are
|
|
703
|
+
still read from the cache. Pages up to 0.8.2 named `tool_turns_left` alone; with a model that
|
|
704
|
+
thinks, the signatures make the miss on their own. Keeping the model's own turns is planned for
|
|
705
|
+
0.10.0.
|
|
697
706
|
- An upgrade that changes the library's prompts misses the cache once in each conversation. 0.6.0
|
|
698
707
|
changed the tool guide, the descriptions of `search_document` and `analyze_whole_document`, and
|
|
699
708
|
the frame lines of a file sent whole. The first request after the upgrade shares with what 0.5.0
|