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/guides/testing.md
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
This guide shows how to test code that uses llm-large-files without calling a real model: tests
|
|
4
4
|
that run in milliseconds, cost nothing and give the same result every time. It is for application
|
|
5
|
-
developers. The samples use [Vitest](https://vitest.dev); the
|
|
6
|
-
depend on it.
|
|
5
|
+
developers. The samples use [Vitest](https://vitest.dev); the fakes they import from
|
|
6
|
+
[`llm-large-files/testing`](../reference/testing.md) do not depend on it.
|
|
7
7
|
|
|
8
8
|
## What to fake
|
|
9
9
|
|
|
@@ -16,10 +16,10 @@ Fake the layer below the code you test, and nothing above it.
|
|
|
16
16
|
| Expiry, owners, deleting | The clock: `now`, with `MemoryDocumentStore` | [Expiry, owners and the clock](#expiry-owners-and-the-clock) |
|
|
17
17
|
| Your upload and answer routes | Nothing for uploads, the model for answers: a real server on a free port, called with `fetch` | [Your routes](#your-routes) |
|
|
18
18
|
| Whether a real model finds the answer in a real file | Nothing: a few live tests | [Live tests, few and cheap](#live-tests-few-and-cheap) |
|
|
19
|
+
| A `DocumentStore` of your own | Nothing: `documentStoreChecks` runs the store against the contract | [Checking your store](./storage.md#checking-your-store) |
|
|
19
20
|
|
|
20
|
-
The library's own tests work the same way, with
|
|
21
|
-
|
|
22
|
-
one you copy into your own tests.
|
|
21
|
+
The library's own tests work the same way, with the same fakes. Since 0.9.0 they ship in the
|
|
22
|
+
package as `llm-large-files/testing`: import them instead of copying them.
|
|
23
23
|
|
|
24
24
|
## Test documents
|
|
25
25
|
|
|
@@ -36,73 +36,12 @@ becomes a card that the model reaches through the tools, as a 50 MB log would. S
|
|
|
36
36
|
`createLargeFiles`, or for one question with `budget` in `askWithFiles`. How the plan is made is in
|
|
37
37
|
[How it works](../how-it-works.md).
|
|
38
38
|
|
|
39
|
-
The
|
|
40
|
-
|
|
41
|
-
lines, which hold 8 failed payments.
|
|
42
|
-
|
|
43
|
-
## A scripted model
|
|
44
|
-
|
|
45
|
-
`askWithFiles` talks to a model only through a [`ModelAdapter`](../reference/ask.md#modeladapter):
|
|
46
|
-
an object with an `id`, its `capabilities` and a `send` method. A fake one answers each request from
|
|
47
|
-
a script and records what it was sent. Save this as `test/fake-model.ts`:
|
|
39
|
+
The samples encode text with `utf8()` from `llm-large-files/testing`, and use `appLog(count)`, a
|
|
40
|
+
log of `count` lines with a failed payment on every 50th, from the file below. From 19 lines on it
|
|
41
|
+
is over 1,000 estimated tokens. The samples use 400 lines, which hold 8 failed payments.
|
|
48
42
|
|
|
49
43
|
```ts
|
|
50
|
-
// test/
|
|
51
|
-
import type { GenerateText, ModelAdapter, ModelCapabilities, ModelReply, ModelRequest, SendHooks, ToolCall } from "llm-large-files";
|
|
52
|
-
|
|
53
|
-
/** One entry of a script: a reply, an error to throw, or a function that picks one from the request. */
|
|
54
|
-
export type Scripted =
|
|
55
|
-
| Partial<ModelReply>
|
|
56
|
-
| Error
|
|
57
|
-
| ((request: ModelRequest, call: number, hooks: SendHooks) => Partial<ModelReply> | Error | Promise<Partial<ModelReply> | Error>);
|
|
58
|
-
|
|
59
|
-
export type FakeModel = ModelAdapter & {
|
|
60
|
-
/** Every request the loop sent, in order, failed ones included. */
|
|
61
|
-
readonly requests: ModelRequest[];
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
/** A reply in text. */
|
|
65
|
-
export const says = (text: string, more: Partial<ModelReply> = {}): Partial<ModelReply> => ({ text, ...more });
|
|
66
|
-
|
|
67
|
-
/** A reply that calls tools. */
|
|
68
|
-
export const calls = (list: ToolCall[], more: Partial<ModelReply> = {}): Partial<ModelReply> => ({ calls: list, ...more });
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* A model that answers each request with the next entry of its script; the last entry repeats.
|
|
72
|
-
* An entry that is an Error is thrown, as an adapter throws a request that failed.
|
|
73
|
-
*/
|
|
74
|
-
export function fakeModel(
|
|
75
|
-
script: Scripted[],
|
|
76
|
-
options: Partial<ModelCapabilities> & { id?: string; generateText?: GenerateText } = {},
|
|
77
|
-
): FakeModel {
|
|
78
|
-
const { id = "fake-model", generateText, ...capabilities } = options;
|
|
79
|
-
const requests: ModelRequest[] = [];
|
|
80
|
-
return {
|
|
81
|
-
id,
|
|
82
|
-
// "none": this model takes no files, so view_pages is not offered. Pass toolFiles: "inside" to test it.
|
|
83
|
-
capabilities: { toolFiles: "none", pageTokens: 500, ...capabilities },
|
|
84
|
-
requests,
|
|
85
|
-
...(generateText ? { generateText } : {}),
|
|
86
|
-
async send(request, hooks = {}) {
|
|
87
|
-
// A copy of the list, so that what was recorded cannot change later.
|
|
88
|
-
requests.push({ ...request, messages: [...request.messages] });
|
|
89
|
-
const entry = script[Math.min(requests.length - 1, script.length - 1)]!;
|
|
90
|
-
const reply = typeof entry === "function" ? await entry(request, requests.length, hooks) : entry;
|
|
91
|
-
if (reply instanceof Error) throw reply;
|
|
92
|
-
const text = reply.text ?? "";
|
|
93
|
-
// When the loop listens for text, pass it on in two pieces, as a streaming adapter would.
|
|
94
|
-
if (text && hooks.onText) {
|
|
95
|
-
const half = Math.ceil(text.length / 2);
|
|
96
|
-
hooks.onText(text.slice(0, half));
|
|
97
|
-
if (half < text.length) hooks.onText(text.slice(half));
|
|
98
|
-
}
|
|
99
|
-
return { calls: [], ...reply, text };
|
|
100
|
-
},
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export const bytes = (text: string): Uint8Array => new TextEncoder().encode(text);
|
|
105
|
-
|
|
44
|
+
// test/documents.ts: test documents for code that calls askWithFiles.
|
|
106
45
|
/** A service log of `count` lines, with a failed payment on every 50th line. */
|
|
107
46
|
export function appLog(count: number): string {
|
|
108
47
|
const lines: string[] = [];
|
|
@@ -116,6 +55,26 @@ export function appLog(count: number): string {
|
|
|
116
55
|
}
|
|
117
56
|
```
|
|
118
57
|
|
|
58
|
+
`serviceLog(lines)` from `llm-large-files/testing` is another: a log with an `ERROR` line every 97
|
|
59
|
+
lines, the one the library's own tests use.
|
|
60
|
+
|
|
61
|
+
## A scripted model
|
|
62
|
+
|
|
63
|
+
`askWithFiles` talks to a model only through a [`ModelAdapter`](../reference/ask.md#modeladapter):
|
|
64
|
+
an object with an `id`, its `capabilities` and a `send` method. `fakeModel` is one that answers
|
|
65
|
+
each request from a script and records what it was sent:
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { calls, fakeModel, says } from "llm-large-files/testing";
|
|
69
|
+
|
|
70
|
+
// Assumes `meta`, a log ingested as in the tests below.
|
|
71
|
+
const model = fakeModel([
|
|
72
|
+
calls([{ name: "search_document", args: { document_id: meta.id, query: "ERROR", mode: "exact" } }]),
|
|
73
|
+
says("8 payments failed."),
|
|
74
|
+
]);
|
|
75
|
+
// After the question, model.requests holds each request the loop sent, in order.
|
|
76
|
+
```
|
|
77
|
+
|
|
119
78
|
What the parts do:
|
|
120
79
|
|
|
121
80
|
- **The script.** One entry for each request, in order. The last entry repeats, so a model that
|
|
@@ -129,13 +88,19 @@ What the parts do:
|
|
|
129
88
|
`text` event, once the reply is in.
|
|
130
89
|
- **The options.** `id` names the model in `usage.byModel`; give two fakes different ones. A
|
|
131
90
|
`generateText` function offers `analyze_whole_document`, the whole-file read in parts; without it
|
|
132
|
-
that tool is not offered. The capabilities make the fake look like a given model
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
91
|
+
that tool is not offered. The capabilities make the fake look like a given model. Unless you
|
|
92
|
+
say otherwise they are `toolFiles: "inside"`, as Gemini's, so the loop offers `view_pages` for
|
|
93
|
+
PDFs whose original is kept (see [PDFs](./pdfs.md)), and `pageTokens: 500`. `toolFiles: "none"`
|
|
94
|
+
makes a model that takes no files, `contextTokens` and `maxRequestBytes` make the budget of a
|
|
95
|
+
question fit a model, and `appendOnly: true` makes the loop treat it as Claude (a conversation
|
|
96
|
+
that may only grow).
|
|
97
|
+
- **The replies.** `says(text, more?)` is a reply of text, and `calls(list, more?)` a reply that
|
|
98
|
+
calls tools. `more` adds fields, such as `usage`. The `raw` of a `calls` reply is
|
|
99
|
+
`{ own: [the tool names] }`, a stand-in for the provider's own form of the turn.
|
|
100
|
+
|
|
101
|
+
Every export, with its defaults, is in the [reference](../reference/testing.md). The source of
|
|
102
|
+
`fakeModel` is in the package, in `src/testing/fake-model.ts`. A fake adapter is also the starting
|
|
103
|
+
point for one of your own: see [Your own adapter and loop](./own-adapter-and-loop.md).
|
|
139
104
|
|
|
140
105
|
## A first test
|
|
141
106
|
|
|
@@ -145,14 +110,15 @@ Two tests: a short document that goes in whole, and a log that the model must se
|
|
|
145
110
|
// test/ask.test.ts
|
|
146
111
|
import { describe, expect, it } from "vitest";
|
|
147
112
|
import { askWithFiles, createLargeFiles, MemoryDocumentStore } from "llm-large-files";
|
|
148
|
-
import {
|
|
113
|
+
import { calls, fakeModel, says, utf8 } from "llm-large-files/testing";
|
|
114
|
+
import { appLog } from "./documents.js";
|
|
149
115
|
|
|
150
116
|
const question = (text: string) => [{ role: "user" as const, text }];
|
|
151
117
|
|
|
152
118
|
describe("questions about documents", () => {
|
|
153
119
|
it("sends a short document whole, in one request without tools", async () => {
|
|
154
120
|
const files = createLargeFiles({ store: new MemoryDocumentStore() });
|
|
155
|
-
const meta = await files.ingest({ name: "notes.md", bytes:
|
|
121
|
+
const meta = await files.ingest({ name: "notes.md", bytes: utf8("# Notes\n\nThe code word is heron.") });
|
|
156
122
|
const model = fakeModel([says("Heron.", { usage: { inputTokens: 40, outputTokens: 3 } })]);
|
|
157
123
|
|
|
158
124
|
const result = await askWithFiles({ files, model, documentIds: [meta.id], messages: question("What is the code word?") });
|
|
@@ -167,7 +133,7 @@ describe("questions about documents", () => {
|
|
|
167
133
|
// Any document over about 1,000 tokens becomes a card with tools: a small test file takes
|
|
168
134
|
// the path that a real 50 MB log takes.
|
|
169
135
|
const files = createLargeFiles({ store: new MemoryDocumentStore(), budget: { inlineTokens: 1_000 } });
|
|
170
|
-
const meta = await files.ingest({ name: "app.log", bytes:
|
|
136
|
+
const meta = await files.ingest({ name: "app.log", bytes: utf8(appLog(400)) });
|
|
171
137
|
const search = { id: "call_1", name: "search_document", args: { document_id: meta.id, query: "ERROR", mode: "exact" } };
|
|
172
138
|
const model = fakeModel([calls([search]), says("8 payments failed, the first on line 50 (app.log L50).")]);
|
|
173
139
|
|
|
@@ -213,13 +179,14 @@ text, a blocked prompt and an explorer model:
|
|
|
213
179
|
// test/loop.test.ts
|
|
214
180
|
import { describe, expect, it } from "vitest";
|
|
215
181
|
import { askWithFiles, createLargeFiles, MemoryDocumentStore } from "llm-large-files";
|
|
216
|
-
import {
|
|
182
|
+
import { calls, fakeModel, says, utf8 } from "llm-large-files/testing";
|
|
183
|
+
import { appLog } from "./documents.js";
|
|
217
184
|
|
|
218
185
|
const question = (text: string) => [{ role: "user" as const, text }];
|
|
219
186
|
|
|
220
187
|
async function largeLog() {
|
|
221
188
|
const files = createLargeFiles({ store: new MemoryDocumentStore(), budget: { inlineTokens: 1_000 } });
|
|
222
|
-
const meta = await files.ingest({ name: "app.log", bytes:
|
|
189
|
+
const meta = await files.ingest({ name: "app.log", bytes: utf8(appLog(400)) });
|
|
223
190
|
return { files, meta };
|
|
224
191
|
}
|
|
225
192
|
|
|
@@ -353,11 +320,12 @@ the wording of a label or a notice is the library's.
|
|
|
353
320
|
// test/events.test.ts
|
|
354
321
|
import { expect, it } from "vitest";
|
|
355
322
|
import { askStream, createLargeFiles, MemoryDocumentStore, type AskEvent } from "llm-large-files";
|
|
356
|
-
import {
|
|
323
|
+
import { calls, fakeModel, says, utf8 } from "llm-large-files/testing";
|
|
324
|
+
import { appLog } from "./documents.js";
|
|
357
325
|
|
|
358
326
|
it("shows the search, then the answer, in order", async () => {
|
|
359
327
|
const files = createLargeFiles({ store: new MemoryDocumentStore(), budget: { inlineTokens: 1_000 } });
|
|
360
|
-
const meta = await files.ingest({ name: "app.log", bytes:
|
|
328
|
+
const meta = await files.ingest({ name: "app.log", bytes: utf8(appLog(400)) });
|
|
361
329
|
const search = { id: "s1", name: "search_document", args: { document_id: meta.id, query: "ERROR", mode: "exact" } };
|
|
362
330
|
const model = fakeModel([calls([search]), says("8 payments failed.")]);
|
|
363
331
|
|
|
@@ -518,7 +486,8 @@ means for a user is in [Errors and reliability](./errors-and-reliability.md).
|
|
|
518
486
|
// test/errors.test.ts
|
|
519
487
|
import { describe, expect, it } from "vitest";
|
|
520
488
|
import { askWithFiles, createLargeFiles, DocumentsNotFoundError, errorKindOf, MemoryDocumentStore, ModelRequestError, partialResultOf } from "llm-large-files";
|
|
521
|
-
import {
|
|
489
|
+
import { calls, fakeModel, says, utf8 } from "llm-large-files/testing";
|
|
490
|
+
import { appLog } from "./documents.js";
|
|
522
491
|
|
|
523
492
|
/** Your application's own words for a question that failed: the code under test. */
|
|
524
493
|
function userMessage(error: unknown): string {
|
|
@@ -540,7 +509,7 @@ const question = [{ role: "user" as const, text: "How many payments failed?" }];
|
|
|
540
509
|
|
|
541
510
|
async function largeLog() {
|
|
542
511
|
const files = createLargeFiles({ store: new MemoryDocumentStore(), budget: { inlineTokens: 1_000 } });
|
|
543
|
-
const meta = await files.ingest({ name: "app.log", bytes:
|
|
512
|
+
const meta = await files.ingest({ name: "app.log", bytes: utf8(appLog(400)), owner: "alice" });
|
|
544
513
|
const search = { name: "search_document", args: { document_id: meta.id, query: "ERROR", mode: "exact" } };
|
|
545
514
|
return { files, meta, search };
|
|
546
515
|
}
|
|
@@ -634,40 +603,17 @@ through it, the part reads of a whole-file read included:
|
|
|
634
603
|
| OpenAI-compatible | `openAiCompatible({ ..., fetch })` |
|
|
635
604
|
| Claude | `anthropic({ ..., fetch })` |
|
|
636
605
|
|
|
637
|
-
|
|
638
|
-
|
|
606
|
+
`fakeFetch` from `llm-large-files/testing` answers from a script, as the fake model does. Each
|
|
607
|
+
request gets the next reply, and the last one repeats. A reply is a `Response`, answered with a
|
|
608
|
+
copy each time, or a function of the request and its number. `sent` records every request: its
|
|
609
|
+
`url`, `method` and `headers`, and its `body` parsed as JSON. `sse(events)` makes a streamed reply:
|
|
610
|
+
one `data:` line for each event, an object as JSON, a string such as `"[DONE]"` as it is.
|
|
639
611
|
|
|
640
612
|
```ts
|
|
641
|
-
|
|
642
|
-
type Json = Record<string, any>;
|
|
643
|
-
|
|
644
|
-
/** A request as the adapter sent it. */
|
|
645
|
-
export type Sent = { url: string; headers: Record<string, string>; body: Json };
|
|
646
|
-
|
|
647
|
-
type Reply = Response | ((sent: Sent, call: number) => Response | Promise<Response>);
|
|
648
|
-
|
|
649
|
-
/** Answers each request with the next reply of the script; the last one repeats. Records every request. */
|
|
650
|
-
export function fakeFetch(replies: Reply[]) {
|
|
651
|
-
const sent: Sent[] = [];
|
|
652
|
-
const fetch = async (input: string | URL | Request, init?: RequestInit): Promise<Response> => {
|
|
653
|
-
const request: Sent = {
|
|
654
|
-
url: String(input),
|
|
655
|
-
headers: { ...(init?.headers as Record<string, string>) },
|
|
656
|
-
body: JSON.parse(String(init?.body)) as Json,
|
|
657
|
-
};
|
|
658
|
-
sent.push(request);
|
|
659
|
-
const reply = replies[Math.min(sent.length - 1, replies.length - 1)]!;
|
|
660
|
-
// A body can be read only once: each request gets a copy.
|
|
661
|
-
return typeof reply === "function" ? reply(request, sent.length) : reply.clone();
|
|
662
|
-
};
|
|
663
|
-
return { fetch: fetch as typeof globalThis.fetch, sent };
|
|
664
|
-
}
|
|
613
|
+
import { fakeFetch } from "llm-large-files/testing";
|
|
665
614
|
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
events.map((event) => `data: ${typeof event === "string" ? event : JSON.stringify(event)}\n\n`).join(""),
|
|
669
|
-
{ headers: { "Content-Type": "text/event-stream" } },
|
|
670
|
-
);
|
|
615
|
+
const api = fakeFetch([Response.json({ candidates: [] })]);
|
|
616
|
+
// Give api.fetch to the adapter; after the question, api.sent holds each request it made.
|
|
671
617
|
```
|
|
672
618
|
|
|
673
619
|
### Gemini on Vertex AI
|
|
@@ -680,8 +626,8 @@ credentials. The replies are in Gemini's own JSON:
|
|
|
680
626
|
import { describe, expect, it } from "vitest";
|
|
681
627
|
import { askWithFiles, createLargeFiles, MemoryDocumentStore } from "llm-large-files";
|
|
682
628
|
import { askGemini, gemini, GeminiRequestError, vertexAi } from "llm-large-files/gemini";
|
|
683
|
-
import {
|
|
684
|
-
import {
|
|
629
|
+
import { fakeFetch, sse, utf8 } from "llm-large-files/testing";
|
|
630
|
+
import { appLog } from "./documents.js";
|
|
685
631
|
|
|
686
632
|
// Replies as Gemini writes them.
|
|
687
633
|
const geminiText = (text: string) => ({
|
|
@@ -699,7 +645,7 @@ const question = [{ role: "user" as const, text: "How many payments failed?" }];
|
|
|
699
645
|
|
|
700
646
|
async function largeLog() {
|
|
701
647
|
const files = createLargeFiles({ store: new MemoryDocumentStore(), budget: { inlineTokens: 1_000 } });
|
|
702
|
-
const meta = await files.ingest({ name: "app.log", bytes:
|
|
648
|
+
const meta = await files.ingest({ name: "app.log", bytes: utf8(appLog(400)) });
|
|
703
649
|
return { files, meta };
|
|
704
650
|
}
|
|
705
651
|
|
|
@@ -723,7 +669,7 @@ describe("Gemini on Vertex AI, without the network", () => {
|
|
|
723
669
|
|
|
724
670
|
it("streams the answer through askGemini", async () => {
|
|
725
671
|
const files = createLargeFiles({ store: new MemoryDocumentStore() });
|
|
726
|
-
const meta = await files.ingest({ name: "notes.md", bytes:
|
|
672
|
+
const meta = await files.ingest({ name: "notes.md", bytes: utf8("# Notes\n\nThe code word is heron.") });
|
|
727
673
|
// A streamed Gemini reply ends with a chunk that says why it ended.
|
|
728
674
|
const api = fakeFetch([sse([
|
|
729
675
|
{ candidates: [{ content: { role: "model", parts: [{ text: "The code word " }] } }] },
|
|
@@ -783,14 +729,14 @@ import { describe, expect, it } from "vitest";
|
|
|
783
729
|
import { askWithFiles, createLargeFiles, MemoryDocumentStore } from "llm-large-files";
|
|
784
730
|
import { openAiCompatible } from "llm-large-files/openai";
|
|
785
731
|
import { anthropic } from "llm-large-files/anthropic";
|
|
786
|
-
import {
|
|
787
|
-
import {
|
|
732
|
+
import { fakeFetch, sse, utf8 } from "llm-large-files/testing";
|
|
733
|
+
import { appLog } from "./documents.js";
|
|
788
734
|
|
|
789
735
|
const question = [{ role: "user" as const, text: "How many payments failed?" }];
|
|
790
736
|
|
|
791
737
|
async function largeLog() {
|
|
792
738
|
const files = createLargeFiles({ store: new MemoryDocumentStore(), budget: { inlineTokens: 1_000 } });
|
|
793
|
-
const meta = await files.ingest({ name: "app.log", bytes:
|
|
739
|
+
const meta = await files.ingest({ name: "app.log", bytes: utf8(appLog(400)) });
|
|
794
740
|
const args = { document_id: meta.id, query: "ERROR", mode: "exact" };
|
|
795
741
|
return { files, meta, args };
|
|
796
742
|
}
|
|
@@ -957,7 +903,7 @@ Record in a live run, then replay in the test:
|
|
|
957
903
|
```ts
|
|
958
904
|
import { askWithFiles } from "llm-large-files";
|
|
959
905
|
import { gemini, vertexAi } from "llm-large-files/gemini";
|
|
960
|
-
import { fakeFetch } from "
|
|
906
|
+
import { fakeFetch } from "llm-large-files/testing";
|
|
961
907
|
import { recordingFetch, replay } from "./record.js";
|
|
962
908
|
|
|
963
909
|
// Assumes `files` and `meta` (a test document, as in largeLog()), and a real `endpoint`
|
|
@@ -1012,7 +958,7 @@ and deletes them.
|
|
|
1012
958
|
// test/expiry.test.ts
|
|
1013
959
|
import { describe, expect, it } from "vitest";
|
|
1014
960
|
import { askWithFiles, createLargeFiles, DocumentsNotFoundError, MemoryDocumentStore } from "llm-large-files";
|
|
1015
|
-
import {
|
|
961
|
+
import { fakeModel, says, utf8 } from "llm-large-files/testing";
|
|
1016
962
|
|
|
1017
963
|
describe("documents that expire", () => {
|
|
1018
964
|
it("are gone after their time to live, and swept from the store", async () => {
|
|
@@ -1020,7 +966,7 @@ describe("documents that expire", () => {
|
|
|
1020
966
|
const now = () => clock;
|
|
1021
967
|
// One clock for both: the library checks expiry by its clock, and the store by its own when it reads.
|
|
1022
968
|
const files = createLargeFiles({ store: new MemoryDocumentStore({ now }), now, ttlMs: 60 * 60_000 });
|
|
1023
|
-
const meta = await files.ingest({ name: "notes.md", bytes:
|
|
969
|
+
const meta = await files.ingest({ name: "notes.md", bytes: utf8("The code word is heron."), owner: "alice" });
|
|
1024
970
|
expect(meta.expiresAt).toBe("2026-01-01T10:00:00.000Z");
|
|
1025
971
|
// Another user cannot delete it.
|
|
1026
972
|
expect(await files.delete(meta.id, { owner: "bob" })).toBe(false);
|
|
@@ -1147,7 +1093,8 @@ import { describe, expect, it } from "vitest";
|
|
|
1147
1093
|
import { GoogleAuth } from "google-auth-library";
|
|
1148
1094
|
import { askWithFiles, createLargeFiles, MemoryDocumentStore } from "llm-large-files";
|
|
1149
1095
|
import { gemini, vertexAi } from "llm-large-files/gemini";
|
|
1150
|
-
import {
|
|
1096
|
+
import { utf8 } from "llm-large-files/testing";
|
|
1097
|
+
import { appLog } from "./documents.js";
|
|
1151
1098
|
|
|
1152
1099
|
// Skipped unless LIVE_TESTS=1 is set: the default test run never calls a model.
|
|
1153
1100
|
describe.skipIf(process.env.LIVE_TESTS !== "1")("live: Gemini on Vertex AI", () => {
|
|
@@ -1164,7 +1111,7 @@ describe.skipIf(process.env.LIVE_TESTS !== "1")("live: Gemini on Vertex AI", ()
|
|
|
1164
1111
|
},
|
|
1165
1112
|
}));
|
|
1166
1113
|
const files = createLargeFiles({ store: new MemoryDocumentStore(), budget: { inlineTokens: 1_000 } });
|
|
1167
|
-
const meta = await files.ingest({ name: "app.log", bytes:
|
|
1114
|
+
const meta = await files.ingest({ name: "app.log", bytes: utf8(appLog(400)) });
|
|
1168
1115
|
|
|
1169
1116
|
const result = await askWithFiles({
|
|
1170
1117
|
files, model, documentIds: [meta.id],
|
|
@@ -1189,6 +1136,8 @@ The credentials are the application default credentials, as on your servers: see
|
|
|
1189
1136
|
|
|
1190
1137
|
## Reference
|
|
1191
1138
|
|
|
1139
|
+
- `fakeModel`, `says`, `calls`, `httpError`, `fakeFetch`, `sse`, `utf8`, `serviceLog` and
|
|
1140
|
+
`documentStoreChecks`: [`llm-large-files/testing`](../reference/testing.md).
|
|
1192
1141
|
- [`ModelAdapter`](../reference/ask.md#modeladapter), [`ModelRequest`](../reference/ask.md#modelrequest),
|
|
1193
1142
|
[`AskResult`](../reference/ask.md#askresult), [`AskLimits`](../reference/ask.md#asklimits) and
|
|
1194
1143
|
`partialResultOf`: [the tool loop and the adapter contract](../reference/ask.md).
|
|
@@ -152,7 +152,6 @@ Every other option goes to `askWithFiles` as it is. What `askGemini` cannot do:
|
|
|
152
152
|
| An explorer with other headers, or of another provider | No: the same headers (with `vertexAi()` or `geminiApi()`, the same project, location and key too) | `explorer`: any adapter |
|
|
153
153
|
| Different waits, `fetch` or `toolFilePlacement` for each model | No: one value for both | Each adapter its own |
|
|
154
154
|
| A part reader of your own ([below](#whole-file-reads-in-parts)) | No | Yes |
|
|
155
|
-
| Turns with native parts in `messages` | Use `contents` | `{ role, parts: [{ text }, { native }] }` |
|
|
156
155
|
|
|
157
156
|
Start with `askGemini`. Move to `askWithFiles` when you need a row of this table:
|
|
158
157
|
|
|
@@ -572,8 +571,9 @@ does.
|
|
|
572
571
|
|
|
573
572
|
## Gemini history stored as contents
|
|
574
573
|
|
|
575
|
-
`askGemini` takes
|
|
576
|
-
|
|
574
|
+
`askGemini` takes `messages` as `askWithFiles` does (`ChatMessage[]`: plain turns, stored tool
|
|
575
|
+
rounds, turns with native parts; since 0.9.0), or your own Gemini `contents` (with images and inline
|
|
576
|
+
PDFs) in `contents`; given both, `contents` is used. The last turn must be
|
|
577
577
|
the user's. Document text and cards are added at the start of the first user turn, and your array
|
|
578
578
|
is not changed. `fromGeminiContents(contents)` turns `contents` into `messages` for `askWithFiles`,
|
|
579
579
|
and `geminiToolTurns(result.toolTurns)` gives the tool rounds of a question as `contents`, to store
|
|
@@ -593,10 +593,13 @@ history.push(...geminiToolTurns(result.toolTurns), ...(result.answered ? [{ role
|
|
|
593
593
|
Store no model turn for a question that was not answered
|
|
594
594
|
([Conversations](./conversations.md#storing-a-conversation-and-asking-the-next-question)).
|
|
595
595
|
|
|
596
|
-
Rounds kept this way are sent as they are.
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
the
|
|
596
|
+
Rounds kept this way are sent as they are. Since 0.9.0 they are also rounds the loop knows, as
|
|
597
|
+
`toolTurns` in `messages` are: `fromGeminiContents` turns the rounds `geminiToolTurns` wrote back
|
|
598
|
+
into stored rounds, so a repeated call is answered with a pointer to its earlier result, old results
|
|
599
|
+
can be shortened when room runs short, and the rounds are sized as they are. Before, they were
|
|
600
|
+
plain parts, sized as media, and a repeated search ran again. Function turns of your own stay as
|
|
601
|
+
they are. What to store and how to keep a conversation inside the window:
|
|
602
|
+
[Conversations](./conversations.md#gemini-contents).
|
|
600
603
|
|
|
601
604
|
## A production setup
|
|
602
605
|
|