llm-large-files 0.9.0 → 0.10.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 +446 -0
- package/README.md +13 -7
- package/dist/access.d.ts +45 -3
- package/dist/access.d.ts.map +1 -1
- package/dist/access.js +106 -3
- package/dist/access.js.map +1 -1
- package/dist/aggregate.d.ts +6 -0
- package/dist/aggregate.d.ts.map +1 -1
- package/dist/aggregate.js +86 -13
- package/dist/aggregate.js.map +1 -1
- package/dist/analyze.d.ts +4 -0
- package/dist/analyze.d.ts.map +1 -1
- package/dist/analyze.js +2 -2
- package/dist/analyze.js.map +1 -1
- package/dist/answer-info.d.ts +26 -0
- package/dist/answer-info.d.ts.map +1 -0
- package/dist/answer-info.js +32 -0
- package/dist/answer-info.js.map +1 -0
- package/dist/anthropic/adapter.d.ts +5 -1
- package/dist/anthropic/adapter.d.ts.map +1 -1
- package/dist/anthropic/adapter.js +35 -10
- package/dist/anthropic/adapter.js.map +1 -1
- package/dist/ask.d.ts +115 -14
- package/dist/ask.d.ts.map +1 -1
- package/dist/ask.js +572 -103
- package/dist/ask.js.map +1 -1
- package/dist/citations.d.ts +36 -0
- package/dist/citations.d.ts.map +1 -0
- package/dist/citations.js +291 -0
- package/dist/citations.js.map +1 -0
- package/dist/error-info.d.ts +13 -0
- package/dist/error-info.d.ts.map +1 -1
- package/dist/error-info.js +37 -1
- package/dist/error-info.js.map +1 -1
- package/dist/errors.d.ts +3 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +3 -1
- package/dist/errors.js.map +1 -1
- package/dist/events.d.ts +25 -7
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js.map +1 -1
- package/dist/evidence.d.ts.map +1 -1
- package/dist/evidence.js +21 -2
- package/dist/evidence.js.map +1 -1
- package/dist/extract.d.ts.map +1 -1
- package/dist/extract.js +6 -1
- package/dist/extract.js.map +1 -1
- package/dist/gemini/adapter.d.ts +35 -4
- package/dist/gemini/adapter.d.ts.map +1 -1
- package/dist/gemini/adapter.js +68 -29
- package/dist/gemini/adapter.js.map +1 -1
- package/dist/gemini/chat.d.ts +28 -55
- package/dist/gemini/chat.d.ts.map +1 -1
- package/dist/gemini/chat.js +20 -27
- package/dist/gemini/chat.js.map +1 -1
- package/dist/gemini/endpoints.d.ts +59 -11
- package/dist/gemini/endpoints.d.ts.map +1 -1
- package/dist/gemini/endpoints.js +33 -7
- package/dist/gemini/endpoints.js.map +1 -1
- package/dist/gemini/errors.d.ts +39 -5
- package/dist/gemini/errors.d.ts.map +1 -1
- package/dist/gemini/errors.js +70 -9
- package/dist/gemini/errors.js.map +1 -1
- package/dist/gemini/generate.d.ts +53 -2
- package/dist/gemini/generate.d.ts.map +1 -1
- package/dist/gemini/generate.js +100 -50
- 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/http.d.ts +41 -3
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +40 -2
- package/dist/http.js.map +1 -1
- package/dist/index.d.ts +6 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/ingest.d.ts +9 -1
- package/dist/ingest.d.ts.map +1 -1
- package/dist/ingest.js +50 -25
- package/dist/ingest.js.map +1 -1
- package/dist/library.d.ts +129 -2
- package/dist/library.d.ts.map +1 -1
- package/dist/library.js +224 -149
- package/dist/library.js.map +1 -1
- package/dist/map-reduce.d.ts +176 -10
- package/dist/map-reduce.d.ts.map +1 -1
- package/dist/map-reduce.js +804 -58
- package/dist/map-reduce.js.map +1 -1
- package/dist/model.d.ts +36 -4
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +17 -1
- package/dist/model.js.map +1 -1
- package/dist/node/handlers.d.ts +29 -8
- package/dist/node/handlers.d.ts.map +1 -1
- package/dist/node/handlers.js +148 -126
- package/dist/node/handlers.js.map +1 -1
- package/dist/node/index.d.ts +1 -1
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js.map +1 -1
- package/dist/node/ingest-child.js +2 -2
- package/dist/node/ingest-child.js.map +1 -1
- package/dist/node/spool.d.ts +9 -5
- package/dist/node/spool.d.ts.map +1 -1
- package/dist/node/spool.js +28 -15
- package/dist/node/spool.js.map +1 -1
- package/dist/node/sse.d.ts +6 -5
- package/dist/node/sse.d.ts.map +1 -1
- package/dist/node/sse.js +8 -7
- package/dist/node/sse.js.map +1 -1
- package/dist/openai/adapter.d.ts +5 -1
- package/dist/openai/adapter.d.ts.map +1 -1
- package/dist/openai/adapter.js +53 -12
- package/dist/openai/adapter.js.map +1 -1
- package/dist/output-limit.d.ts +2 -1
- package/dist/output-limit.d.ts.map +1 -1
- package/dist/output-limit.js +5 -2
- package/dist/output-limit.js.map +1 -1
- package/dist/pdf-outline.d.ts +71 -0
- package/dist/pdf-outline.d.ts.map +1 -0
- package/dist/pdf-outline.js +414 -0
- package/dist/pdf-outline.js.map +1 -0
- package/dist/plan.d.ts +137 -7
- package/dist/plan.d.ts.map +1 -1
- package/dist/plan.js +248 -26
- package/dist/plan.js.map +1 -1
- package/dist/prepare.d.ts +50 -0
- package/dist/prepare.d.ts.map +1 -0
- package/dist/prepare.js +207 -0
- package/dist/prepare.js.map +1 -0
- package/dist/ranged.d.ts +2 -1
- package/dist/ranged.d.ts.map +1 -1
- package/dist/ranged.js +16 -3
- package/dist/ranged.js.map +1 -1
- package/dist/search.d.ts +7 -0
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +72 -10
- package/dist/search.js.map +1 -1
- package/dist/stores.d.ts +6 -0
- package/dist/stores.d.ts.map +1 -1
- package/dist/stores.js +82 -11
- package/dist/stores.js.map +1 -1
- package/dist/tools.d.ts +22 -2
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +323 -76
- package/dist/tools.js.map +1 -1
- package/dist/types.d.ts +49 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/whole-cost.d.ts +37 -0
- package/dist/whole-cost.d.ts.map +1 -0
- package/dist/whole-cost.js +46 -0
- package/dist/whole-cost.js.map +1 -0
- package/docs/README.md +4 -0
- package/docs/guides/anthropic.md +5 -2
- package/docs/guides/conversations.md +1 -1
- package/docs/guides/costs-and-limits.md +132 -36
- package/docs/guides/deployment.md +106 -13
- package/docs/guides/errors-and-reliability.md +157 -45
- package/docs/guides/explorer-model.md +4 -1
- package/docs/guides/openai-compatible.md +47 -7
- package/docs/guides/own-adapter-and-loop.md +33 -6
- package/docs/guides/pdfs.md +96 -7
- package/docs/guides/storage.md +46 -3
- package/docs/guides/testing.md +71 -0
- package/docs/guides/vertex-and-gemini.md +159 -50
- package/docs/guides/web-app.md +10 -6
- package/docs/how-it-works.md +120 -22
- package/docs/measurements.md +172 -5
- package/docs/publishing.md +76 -4
- package/docs/reference/anthropic.md +12 -7
- package/docs/reference/ask.md +262 -35
- package/docs/reference/errors-and-retries.md +30 -2
- package/docs/reference/events.md +27 -10
- package/docs/reference/gemini.md +184 -68
- package/docs/reference/library.md +190 -23
- package/docs/reference/node.md +51 -17
- package/docs/reference/openai.md +21 -10
- package/docs/reference/tools.md +271 -26
- package/docs/security.md +101 -12
- package/docs/troubleshooting.md +14 -12
- package/docs/upgrading-0.10.md +472 -0
- package/package.json +5 -1
- package/src/access.ts +127 -4
- package/src/aggregate.ts +92 -14
- package/src/analyze.ts +2 -2
- package/src/answer-info.ts +45 -0
- package/src/anthropic/adapter.ts +40 -11
- package/src/ask.ts +646 -106
- package/src/citations.ts +294 -0
- package/src/error-info.ts +36 -1
- package/src/errors.ts +3 -1
- package/src/events.ts +29 -6
- package/src/evidence.ts +21 -2
- package/src/extract.ts +6 -1
- package/src/gemini/adapter.ts +99 -32
- package/src/gemini/chat.ts +53 -82
- package/src/gemini/endpoints.ts +89 -16
- package/src/gemini/errors.ts +74 -11
- package/src/gemini/generate.ts +147 -57
- package/src/gemini/index.ts +3 -3
- package/src/http.ts +65 -4
- package/src/index.ts +6 -4
- package/src/ingest.ts +58 -27
- package/src/library.ts +336 -142
- package/src/map-reduce.ts +855 -67
- package/src/model.ts +44 -4
- package/src/node/handlers.ts +159 -118
- package/src/node/index.ts +1 -0
- package/src/node/ingest-child.ts +2 -2
- package/src/node/spool.ts +28 -14
- package/src/node/sse.ts +8 -7
- package/src/openai/adapter.ts +56 -13
- package/src/output-limit.ts +5 -2
- package/src/pdf-outline.ts +400 -0
- package/src/plan.ts +287 -36
- package/src/prepare.ts +253 -0
- package/src/ranged.ts +16 -3
- package/src/search.ts +69 -13
- package/src/stores.ts +76 -11
- package/src/tools.ts +327 -75
- package/src/types.ts +45 -1
- package/src/whole-cost.ts +64 -0
package/src/types.ts
CHANGED
|
@@ -211,6 +211,17 @@ export type ExtractedText = {
|
|
|
211
211
|
* to hash it, a slice at a time. Other extractors leave it out. New in 0.9.0.
|
|
212
212
|
*/
|
|
213
213
|
file?: { bytes: number; sha256: string };
|
|
214
|
+
/**
|
|
215
|
+
* A PDF's headings, found from the size of its type: each line set larger than the body, with
|
|
216
|
+
* its page (from 1), its level (1 is the top) and its text, in the order of the text; and `skip`,
|
|
217
|
+
* when lines above it on its page read the same (a contents list, a running header), how many.
|
|
218
|
+
* The built-in PDF extractor returns them; `ingest` finds each among the lines of its page (the
|
|
219
|
+
* lines after `--- Page N ---`), with spaces left out of the comparison, past the first `skip`
|
|
220
|
+
* that match, and makes them the outline when the text's own rules find fewer than 3 headings and
|
|
221
|
+
* it finds 3 or more. Read for a PDF only. An extractor that wraps the built-in one passes them on
|
|
222
|
+
* to keep that outline. New in 0.10.0.
|
|
223
|
+
*/
|
|
224
|
+
headings?: Array<{ page: number; level: number; title: string; skip?: number }>;
|
|
214
225
|
};
|
|
215
226
|
|
|
216
227
|
export type ExtractInput = {
|
|
@@ -341,7 +352,12 @@ export type ToolCallLimits = {
|
|
|
341
352
|
/**
|
|
342
353
|
* The most model requests a whole-file read (`analyze_whole_document`) may make: what the
|
|
343
354
|
* question has left of `limits.requests`. A part the model refuses as too large is read as two
|
|
344
|
-
* halves only while that and `maxMapChunks` allow it.
|
|
355
|
+
* halves only while that and `maxMapChunks` allow it. Given to a `read_document` call (since
|
|
356
|
+
* 0.10.0), it is what a whole read of its file may make on the model's next turn, the first it
|
|
357
|
+
* can call one on: a read cut short of its range points at one (`whole_file`) only when the
|
|
358
|
+
* file's estimated parts are within it. `askWithFiles` gives a read 0 when the question would not
|
|
359
|
+
* start a whole read of its file on that turn (a ceiling it would pass, with that turn's request
|
|
360
|
+
* counted, or its whole reads made).
|
|
345
361
|
*/
|
|
346
362
|
maxRequests?: number;
|
|
347
363
|
/**
|
|
@@ -376,6 +392,11 @@ export type ToolCallHooks = {
|
|
|
376
392
|
* the server is busy. `askWithFiles` makes it a "page_looks_busy" notice. New in 0.9.0.
|
|
377
393
|
*/
|
|
378
394
|
onPagesBusy?: (documentId: string) => void;
|
|
395
|
+
/**
|
|
396
|
+
* Ends this call alone: its scans, regex searches, whole-file read, look at pages and loads. It is
|
|
397
|
+
* joined with the tool set's own signal (`ToolOptions.signal`). New in 0.10.0.
|
|
398
|
+
*/
|
|
399
|
+
signal?: AbortSignal;
|
|
379
400
|
};
|
|
380
401
|
|
|
381
402
|
/** One model request of a tool call (`ToolCallHooks.onRequest`). */
|
|
@@ -423,6 +444,13 @@ export type ToolSet = {
|
|
|
423
444
|
* before it runs a call.
|
|
424
445
|
*/
|
|
425
446
|
resultCharacters(name: string): number;
|
|
447
|
+
/**
|
|
448
|
+
* How many more times this tool set may read the document `documentId` whole
|
|
449
|
+
* (`analyze_whole_document`): 0 once a read of it found no part it could read, or at the cap.
|
|
450
|
+
* Optional, so that a tool set of your own still compiles. The tool set of `files.tools()` has
|
|
451
|
+
* it. New in 0.10.0.
|
|
452
|
+
*/
|
|
453
|
+
wholeReadsLeft?(documentId: string): number;
|
|
426
454
|
};
|
|
427
455
|
|
|
428
456
|
/** Tokens one model request used, as far as the provider said. */
|
|
@@ -466,6 +494,15 @@ export type DocumentPlan = {
|
|
|
466
494
|
inlineBytes: number;
|
|
467
495
|
};
|
|
468
496
|
|
|
497
|
+
/**
|
|
498
|
+
* The name of a document tool, as the model calls it (`TOOL_NAMES`). New in 0.10.0.
|
|
499
|
+
*/
|
|
500
|
+
export type ToolName = "search_document" | "read_document" | "get_document_overview" | "analyze_whole_document" | "view_pages" | "aggregate_table";
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* What `prepare` gives for one request. The fields new in 0.10.0 are optional, so that a
|
|
504
|
+
* `LargeFiles` of your own still compiles: without them, `askWithFiles` works as in 0.9.
|
|
505
|
+
*/
|
|
469
506
|
export type PreparedContext = {
|
|
470
507
|
plan: DocumentPlan;
|
|
471
508
|
/** Prompt text per document ID: the full text for inline documents, a summary card otherwise. */
|
|
@@ -479,4 +516,11 @@ export type PreparedContext = {
|
|
|
479
516
|
* was called with `viewPages: true`. Pass it to `tools(..., { viewPages })`.
|
|
480
517
|
*/
|
|
481
518
|
viewable: DocumentMeta[];
|
|
519
|
+
/**
|
|
520
|
+
* Documents sent whole whose tables the tools can still count and search: every CSV and XLSX
|
|
521
|
+
* file, and a file of another format that `aggregate_table` reads whose text has a `| cell |`
|
|
522
|
+
* table of at least 3 rows. Set when `prepare` was called with `inlineTables: "tools"`, else
|
|
523
|
+
* absent. `askWithFiles` makes the tools for them with the searched documents. New in 0.10.0.
|
|
524
|
+
*/
|
|
525
|
+
toolable?: DocumentMeta[];
|
|
482
526
|
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// What a whole-file read (`analyze_whole_document`) costs, before it starts: the input tokens of
|
|
2
|
+
// all its requests, and how many requests it makes, from the document's metadata alone. New in
|
|
3
|
+
// 0.10.0, for the ceiling of a question, which prices a read before it lets it run. Internal: not
|
|
4
|
+
// exported from the package.
|
|
5
|
+
import { estimateMerges, ITEMS_FRAME_TOKENS, PART_HEADING_TOKENS, PART_OUTPUT_TOKENS, partsHaveHeadings, REQUEST_FRAME_TOKENS } from "./map-reduce.js";
|
|
6
|
+
import { estimateParts } from "./plan.js";
|
|
7
|
+
import type { Budget, DocumentMeta } from "./types.js";
|
|
8
|
+
|
|
9
|
+
/** The merges of notes a read makes, worked out from the rounds of `analyzeWholeDocument` (src/map-reduce.ts). */
|
|
10
|
+
export { estimateMerges };
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The digits of the line numbers 1 to `lines`, written out: what the numbers take in the prompts of
|
|
14
|
+
* the parts. `estimateParts` (src/plan.ts) counts them the same way.
|
|
15
|
+
*/
|
|
16
|
+
export function lineNumberDigits(lines: number): number {
|
|
17
|
+
let digits = 0;
|
|
18
|
+
for (let width = 1, first = 1; first <= lines; width++, first *= 10) digits += (Math.min(lines, first * 10 - 1) - first + 1) * width;
|
|
19
|
+
return digits;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** What a whole-file read costs. */
|
|
23
|
+
export type WholeReadCost = {
|
|
24
|
+
/** The input tokens of all its requests, in the model's own tokens (the library's estimate times `scale`). */
|
|
25
|
+
inputTokens: number;
|
|
26
|
+
/** All its requests: the parts, the merges of their notes, and the answer of the read. */
|
|
27
|
+
requests: number;
|
|
28
|
+
/** The part reads among them. */
|
|
29
|
+
parts: number;
|
|
30
|
+
/** The merges of notes among them. */
|
|
31
|
+
merges: number;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The input tokens and requests of a whole-file read of `meta`, with the `budget` of the question
|
|
36
|
+
* and the `scale` of the model that reads it (`capabilities.tokenScale`). The parts are
|
|
37
|
+
* `estimateParts`'s; each sends its lines numbered (the document's tokens, 1.2 tokens a digit of
|
|
38
|
+
* the line numbers and 1.5 a line) with its system text, task and rules, and each after the first
|
|
39
|
+
* of a file with an outline the heading it starts under (`PART_HEADING_TOKENS`).
|
|
40
|
+
* The merges are `estimateMerges`'s, for notes as long as the output limit allows, and the answer
|
|
41
|
+
* of the read is one request more. It is a price before the read: what a read sends is usually
|
|
42
|
+
* less, as notes are shorter than the limit, and not every part starts under a heading. NaN for
|
|
43
|
+
* metadata that does not give its size.
|
|
44
|
+
*
|
|
45
|
+
* `output` "items" (a read that lists items, new in 0.10.0) is the parts alone, each with the
|
|
46
|
+
* longer rules of a list: no merge and no answer. A part read again in halves, when its list does
|
|
47
|
+
* not fit the output limit, is not in the price, as a part halved by an answer read is not.
|
|
48
|
+
*/
|
|
49
|
+
export function wholeReadCost(
|
|
50
|
+
meta: Pick<DocumentMeta, "estimatedTokens" | "lines" | "textBytes" | "jsonBytes"> & Partial<Pick<DocumentMeta, "format" | "outline">>,
|
|
51
|
+
budget: Budget,
|
|
52
|
+
scale: number,
|
|
53
|
+
output: "answer" | "items" = "answer",
|
|
54
|
+
): WholeReadCost {
|
|
55
|
+
const parts = estimateParts(meta, budget);
|
|
56
|
+
const lines = Math.max(1, meta.lines);
|
|
57
|
+
// The first part starts at line 1, under no heading.
|
|
58
|
+
const headings = partsHaveHeadings(meta) ? (parts - 1) * PART_HEADING_TOKENS : 0;
|
|
59
|
+
const numbered = meta.estimatedTokens + 1.2 * lineNumberDigits(lines) + 1.5 * lines + headings;
|
|
60
|
+
if (output === "items") return { inputTokens: Math.ceil((numbered + parts * ITEMS_FRAME_TOKENS) * scale), requests: parts, parts, merges: 0 };
|
|
61
|
+
const merges = estimateMerges(parts, PART_OUTPUT_TOKENS, budget.mapChunkTokens);
|
|
62
|
+
const tokens = numbered + parts * REQUEST_FRAME_TOKENS + merges.inputTokens + merges.finalTokens + REQUEST_FRAME_TOKENS;
|
|
63
|
+
return { inputTokens: Math.ceil(tokens * scale), requests: parts + merges.requests + 1, parts, merges: merges.requests };
|
|
64
|
+
}
|