llm-large-files 0.2.0 → 0.3.5
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 +101 -0
- package/README.md +52 -6
- package/dist/extract.d.ts +13 -6
- package/dist/extract.d.ts.map +1 -1
- package/dist/extract.js +27 -6
- package/dist/extract.js.map +1 -1
- package/dist/gemini/chat.d.ts +48 -4
- package/dist/gemini/chat.d.ts.map +1 -1
- package/dist/gemini/chat.js +108 -24
- package/dist/gemini/chat.js.map +1 -1
- package/dist/gemini/endpoints.d.ts +17 -0
- package/dist/gemini/endpoints.d.ts.map +1 -1
- package/dist/gemini/endpoints.js +16 -2
- package/dist/gemini/endpoints.js.map +1 -1
- package/dist/gemini/generate.d.ts +34 -4
- package/dist/gemini/generate.d.ts.map +1 -1
- package/dist/gemini/generate.js +47 -7
- 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/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/library.d.ts +31 -1
- package/dist/library.d.ts.map +1 -1
- package/dist/library.js +44 -6
- package/dist/library.js.map +1 -1
- package/dist/pdf-layout.d.ts +7 -0
- package/dist/pdf-layout.d.ts.map +1 -1
- package/dist/pdf-layout.js +32 -2
- package/dist/pdf-layout.js.map +1 -1
- package/dist/pdf-pages.d.ts +45 -0
- package/dist/pdf-pages.d.ts.map +1 -0
- package/dist/pdf-pages.js +336 -0
- package/dist/pdf-pages.js.map +1 -0
- package/dist/pdf-pictures.d.ts +20 -0
- package/dist/pdf-pictures.d.ts.map +1 -0
- package/dist/pdf-pictures.js +336 -0
- package/dist/pdf-pictures.js.map +1 -0
- package/dist/plan.d.ts +13 -3
- package/dist/plan.d.ts.map +1 -1
- package/dist/plan.js +22 -17
- package/dist/plan.js.map +1 -1
- package/dist/search.d.ts +9 -0
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +41 -0
- package/dist/search.js.map +1 -1
- package/dist/stores.d.ts +7 -1
- package/dist/stores.d.ts.map +1 -1
- package/dist/stores.js +37 -5
- package/dist/stores.js.map +1 -1
- package/dist/tools.d.ts +11 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +145 -13
- package/dist/tools.js.map +1 -1
- package/dist/types.d.ts +57 -3
- package/dist/types.d.ts.map +1 -1
- package/docs/api.md +59 -17
- package/docs/configuration.md +351 -5
- package/docs/getting-started.md +50 -3
- package/docs/how-it-works.md +73 -4
- package/docs/security.md +39 -4
- package/package.json +7 -2
- package/src/extract.ts +34 -8
- package/src/gemini/chat.ts +145 -30
- package/src/gemini/endpoints.ts +34 -2
- package/src/gemini/generate.ts +63 -9
- package/src/gemini/index.ts +3 -3
- package/src/index.ts +2 -2
- package/src/library.ts +74 -7
- package/src/pdf-layout.ts +37 -1
- package/src/pdf-pages.ts +315 -0
- package/src/pdf-pictures.ts +324 -0
- package/src/plan.ts +36 -16
- package/src/search.ts +37 -0
- package/src/stores.ts +39 -6
- package/src/tools.ts +147 -13
- package/src/types.ts +57 -3
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,107 @@ All notable changes to this package are recorded here. The format follows
|
|
|
4
4
|
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and versions follow
|
|
5
5
|
[semantic versioning](https://semver.org).
|
|
6
6
|
|
|
7
|
+
## [0.3.5] - 2026-11-11
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- **`explorerModel`: a second model for the searching and reading.** The application chooses the
|
|
12
|
+
model, as before, in `geminiApi()` or `vertexAi()`. It can now name two. The explorer makes the
|
|
13
|
+
tool calls (search, read, look at pages) and reads a whole file in parts; `model` then writes the
|
|
14
|
+
answer from what the tools returned, in one request without tools. So the many requests of a
|
|
15
|
+
search through a large file can go to a cheaper or faster model.
|
|
16
|
+
`geminiApi({ apiKey, model, explorerModel: process.env.GEMINI_EXPLORER_MODEL })`: left out,
|
|
17
|
+
empty, or the same as `model`, one model does both, and the requests are those of 0.3.0 (checked
|
|
18
|
+
byte for byte over seven scenarios). See
|
|
19
|
+
[An explorer model](docs/configuration.md#an-explorer-model).
|
|
20
|
+
- An endpoint of your own takes the explorer's URL as `explorerUrl`. It is called with the same
|
|
21
|
+
headers.
|
|
22
|
+
- `explorerGenerationConfig` is the explorer's `generationConfig`, for two models that do not
|
|
23
|
+
take the same settings. It defaults to `generationConfig`. `analysisConfig` applies to the part
|
|
24
|
+
reads of a whole-file read, which are the explorer's when there is one.
|
|
25
|
+
- The answering model gets the explorer's tool calls and their results, pages included. It does
|
|
26
|
+
not get the explorer's thoughts, thought signatures or words, and `text` and `onText` carry
|
|
27
|
+
only the answer. The answer is always a request of its own, so a question takes one request
|
|
28
|
+
more than with one model.
|
|
29
|
+
- Checked live with Flash-Lite models, on the Gemini API and on Vertex AI, between Gemini 3.1 and
|
|
30
|
+
3.5 and between Gemini 2.5 and 3.5, in both directions, with thinking on and off: counts and
|
|
31
|
+
causes from a searched log, a whole-file read, and a chart and a photo from pages that the
|
|
32
|
+
explorer looked at. Every answer was right. `gemini-3.5-flash-lite` as explorer stopped with
|
|
33
|
+
`DONE` each time, at one output token.
|
|
34
|
+
- `EXPLORER_INSTRUCTION` and `FINAL_ANSWER_WITHOUT_TOOLS`: the two texts that `askGemini` adds for
|
|
35
|
+
an explorer.
|
|
36
|
+
|
|
37
|
+
## [0.3.0] - 2026-11-10
|
|
38
|
+
|
|
39
|
+
The model can look at the pages of a large PDF. Until now such a PDF reached it as text only, and
|
|
40
|
+
pictures, charts and diagrams were lost. Everything here is off until you turn it on: an
|
|
41
|
+
application that changes nothing, and does not install the new optional dependency, sends the same
|
|
42
|
+
requests as with 0.2.0 (checked byte for byte against the published 0.2.0 over eight scenarios).
|
|
43
|
+
|
|
44
|
+
### Added
|
|
45
|
+
|
|
46
|
+
- **`view_pages`: the real pages of a PDF, on request.** The tool cuts up to five pages out of the
|
|
47
|
+
uploaded file and returns them as one small PDF, which Gemini reads like any PDF. Asked which bar
|
|
48
|
+
of a chart is tallest, `gemini-3.5-flash-lite` said from the text alone that it could not see the
|
|
49
|
+
chart; with the tool it looked at the page unasked and answered correctly. For ten questions that
|
|
50
|
+
the text answers, it did not look at a page once. A page costs about 530 tokens. See
|
|
51
|
+
[Pictures in PDFs](docs/configuration.md#pictures-in-pdfs) for the limits and the measurements.
|
|
52
|
+
- It needs the new optional dependency `@cantoo/pdf-lib`, and the uploaded file (next item).
|
|
53
|
+
- `askGemini` offers it for every PDF whose original is kept, also for one whose text fits in
|
|
54
|
+
the prompt (a slide deck of photos has little text). `viewPages: false` turns it off.
|
|
55
|
+
It works on the Gemini API and on Vertex AI. `toolFilePlacement: "beside"` sends the pages in
|
|
56
|
+
a user turn of their own instead of inside the function response: Gemini 2.5 needs that, as it
|
|
57
|
+
does not see a file inside a function response.
|
|
58
|
+
- In a loop of your own: `prepare(ids, base, { viewPages: true })` lists the PDFs in
|
|
59
|
+
`PreparedContext.viewable`, `tools(documents, { viewPages })` declares the tool, and
|
|
60
|
+
`ToolSet.executeWithFiles(name, args, { maxFileBytes, maxPages })` returns `{ result, files }`.
|
|
61
|
+
The tool is declared only on request, and `execute()` answers it with an error, because a loop
|
|
62
|
+
that drops the file would tell the model that pages are attached with nothing attached.
|
|
63
|
+
- Search and read results carry page numbers (`page`, `pages`) for a PDF that can be shown.
|
|
64
|
+
- **`keepOriginals`: keep the uploaded file.** `createLargeFiles({ keepOriginals: ["pdf"] })`
|
|
65
|
+
stores the file next to its text. It is off by default, because a kept file takes disk space, up
|
|
66
|
+
to the upload limit, until the document expires. `DocumentStore` has two new optional methods,
|
|
67
|
+
`putOriginal` and `getOriginal`; a store without them works as before and keeps nothing.
|
|
68
|
+
`FileDocumentStore` writes `<id>.bin`, and delete, expiry and sweep remove it.
|
|
69
|
+
`DocumentMeta.originalKept` records it, and `files.getOriginal(id, { owner })` returns the file
|
|
70
|
+
to its owner.
|
|
71
|
+
- **Scanned PDFs of up to 30 pages are accepted** when their original is kept: the model reads them
|
|
72
|
+
by looking at the pages. `scannedPdfPages` sets the limit, and 0 refuses them as before. A longer
|
|
73
|
+
scan, and any scan without a kept original, is still refused.
|
|
74
|
+
- `geminiFunctionResponseTurns(answers, { files })`: the turns that answer a model turn's function
|
|
75
|
+
calls, for a loop of your own. `geminiFunctionResponses` takes the files of each call and puts
|
|
76
|
+
them inside its function response.
|
|
77
|
+
- `createPdfExtractor({ pictures })`, `ExtractInput.scannedPages`, `MAX_VIEW_PAGES`,
|
|
78
|
+
`FINAL_ANSWER_AFTER_PAGES`, and the types `ToolFile`, `ToolOutput`, `ToolFileLimits`,
|
|
79
|
+
`PrepareOptions`, `GeminiToolAnswer` and `GeminiToolFilePlacement`.
|
|
80
|
+
|
|
81
|
+
### Changed
|
|
82
|
+
|
|
83
|
+
- **A page of a PDF that shows a picture is marked**, with one line under its header:
|
|
84
|
+
`[Picture on this page. This text cannot show it.]`. A picture is images that cover 3% of the
|
|
85
|
+
page or more, other than a logo or background that is on most pages; a drawing is shapes filled
|
|
86
|
+
in three or more colours, outside the tables. The pages are found from the structure of the file,
|
|
87
|
+
without decoding any image: a PDF of 200 photos took 0.3 s to ingest before and takes 0.8 s now.
|
|
88
|
+
This needs `@cantoo/pdf-lib`. Without it, and with `createPdfExtractor({ pictures: false })`, the
|
|
89
|
+
text is exactly that of 0.2.0. Documents already in a store keep the text they were ingested
|
|
90
|
+
with.
|
|
91
|
+
- `ToolSet` has a new method, `executeWithFiles`, `LargeFiles` has `getOriginal`, and
|
|
92
|
+
`PreparedContext` has `viewable`. Code that builds these objects itself (a test double, for
|
|
93
|
+
example) has to add them.
|
|
94
|
+
|
|
95
|
+
### Known limits
|
|
96
|
+
|
|
97
|
+
- A chart in one or two colours, and a drawing made only of lines, is not marked. The model can
|
|
98
|
+
still look at the page: it did so for a one-colour chart that had only a caption.
|
|
99
|
+
- Every look reads the whole PDF into memory: 0.06 s for 100 MB of photos, but 4 to 5 seconds and
|
|
100
|
+
700 MB for a file of 330,000 objects. A file that declares more objects than memory allows is
|
|
101
|
+
refused. Reading cannot be interrupted, so a crafted PDF can hold the process for a long time:
|
|
102
|
+
see [Security](docs/security.md#reading-pdfs-for-pages-and-pictures).
|
|
103
|
+
- Tested with PDFs made by Chrome, Word and PowerPoint. PDFs made by LaTeX and InDesign were not
|
|
104
|
+
tested. For FPDF, Ghostscript and scanners, the structures they are known to write were tested
|
|
105
|
+
with files made for the purpose, not with files from those tools. Encrypted PDFs were tested
|
|
106
|
+
with files encrypted by the library itself, and confirmed as encrypted by pdf.js.
|
|
107
|
+
|
|
7
108
|
## [0.2.0] - 2026-11-10
|
|
8
109
|
|
|
9
110
|
### Fixed
|
package/README.md
CHANGED
|
@@ -9,6 +9,8 @@ and 1M tokens of context. This package bridges the gap:
|
|
|
9
9
|
- **Files that fit** go into the prompt whole.
|
|
10
10
|
- **Files that don't** become a short summary card, and the model gets tools to **search**,
|
|
11
11
|
**read line ranges**, or **read the whole file in parts** (map-reduce). Answers cite line numbers.
|
|
12
|
+
- **Pictures in a PDF** are not in its text. The model can **look at the real pages** when it
|
|
13
|
+
needs to: a chart, a photo, a scan.
|
|
12
14
|
|
|
13
15
|
It's a small library, not a framework: no model SDK, no server, and no UI are required. A drop-in
|
|
14
16
|
Gemini / Vertex AI helper and Node HTTP handlers are included for the common case.
|
|
@@ -75,11 +77,53 @@ await fetch("/api/documents", {
|
|
|
75
77
|
The full walkthrough, with streaming to the browser, is in the [integration guide](docs/getting-started.md).
|
|
76
78
|
There's also a [one-file demo server](examples/server.ts).
|
|
77
79
|
|
|
80
|
+
## Pictures, charts and scans in PDFs
|
|
81
|
+
|
|
82
|
+
A large PDF goes to the model as text, and text cannot show a chart. Keep the uploaded PDFs, and
|
|
83
|
+
the model gets a tool, `view_pages`, that hands it the pages it asks for as a small PDF:
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
npm install @cantoo/pdf-lib
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
const files = createLargeFiles({
|
|
91
|
+
store: new FileDocumentStore("./data/large-files"),
|
|
92
|
+
keepOriginals: ["pdf"], // off by default: a kept file takes disk space until it expires
|
|
93
|
+
});
|
|
94
|
+
// askGemini needs no change. A step then reads: Look at report.pdf pages 12-14
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Asked which bar of a chart is tallest, `gemini-3.5-flash-lite` said from the text that it could
|
|
98
|
+
not see the chart; with `view_pages` it looked at the page and answered correctly. It did not look
|
|
99
|
+
at a page for any of ten questions that the text answers. A page costs about 530 tokens.
|
|
100
|
+
Details, limits and measurements: [Pictures in PDFs](docs/configuration.md#pictures-in-pdfs).
|
|
101
|
+
|
|
102
|
+
## One model to explore, another to answer
|
|
103
|
+
|
|
104
|
+
Your application chooses the model: it is the `model` of the endpoint, and the package has no
|
|
105
|
+
default. That model does both halves of the work on a large file. It explores the file (searches,
|
|
106
|
+
reads, looks at pages), and then it answers. You can name a second model for the first half:
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
const endpoint = geminiApi({
|
|
110
|
+
apiKey: process.env.GEMINI_API_KEY!,
|
|
111
|
+
model: "gemini-3.8-flash", // writes the answer
|
|
112
|
+
explorerModel: process.env.GEMINI_EXPLORER_MODEL, // optional: searches and reads
|
|
113
|
+
});
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Left out or empty, nothing changes: one model does both. With an explorer, the many requests of a
|
|
117
|
+
search through a large file go to that model, which can be a cheaper or faster one, and `model`
|
|
118
|
+
gets what the tools returned and writes the answer in one request. In eleven live checks with
|
|
119
|
+
`gemini-3.5-flash-lite` exploring, the other model answered every one correctly from what it found.
|
|
120
|
+
What each model gets, and what it costs: [An explorer model](docs/configuration.md#an-explorer-model).
|
|
121
|
+
|
|
78
122
|
## What it handles
|
|
79
123
|
|
|
80
124
|
| | |
|
|
81
125
|
|---|---|
|
|
82
|
-
| **Formats** | Text, Markdown, logs (UTF-8 and UTF-16), JSON/JSONL, CSV/TSV, DOCX, PPTX, PDF (text layer, with tables rebuilt so empty cells stay empty) |
|
|
126
|
+
| **Formats** | Text, Markdown, logs (UTF-8 and UTF-16), JSON/JSONL, CSV/TSV, DOCX, PPTX, PDF (text layer, with tables rebuilt so empty cells stay empty; pages with a picture are marked, and the model can look at the pages themselves; scans of up to 30 pages) |
|
|
83
127
|
| **Logs** | Detected automatically; the card shows level counts, the time range, and the most frequent errors grouped by shape (`timeout after <n>ms to <ip>`) |
|
|
84
128
|
| **Books and documents** | An outline of headings with line numbers, including Markdown, "Chapter 12", numbered sections, and Hebrew chapter titles |
|
|
85
129
|
| **Limits** | One budget covers tokens *and* request bytes, so files fit both your context window and your connector's payload limit |
|
|
@@ -95,22 +139,24 @@ With `gemini-3.8-flash` on the Gemini API:
|
|
|
95
139
|
| 5 MB log, about 1.27M tokens (over the 1M limit) | "What caused the outage?" | Correct root cause, citing the right lines, in about 60 s over 11 tool calls |
|
|
96
140
|
| 467 KB novel, about 117K tokens | "Where was the ledger hidden?" | Sent whole; correct. Estimated 116,840 tokens; Gemini billed 116,842 |
|
|
97
141
|
| Same novel, map-reduce | "Read all of it…" | 4 parts read in parallel; correct, cited answer in 32 s |
|
|
142
|
+
| PDF with a chart and photos (`gemini-3.5-flash-lite`) | "Which bar is tallest, and what number is on the sign?" | Looked at 2 pages unasked; all five facts right, in 3 s. From the text alone: "cannot see the chart" |
|
|
98
143
|
|
|
99
144
|
## Documentation
|
|
100
145
|
|
|
101
146
|
- [Integration guide](docs/getting-started.md): Express, plain Node, browser upload, streaming, Vertex AI, gateways, existing chat history
|
|
102
147
|
- [API reference](docs/api.md)
|
|
103
|
-
- [Configuration](docs/configuration.md): budget, stores, formats, extractors
|
|
104
|
-
- [How it works](docs/how-it-works.md): planner, cards, tools, map-reduce, and the Gemini quirks it handles
|
|
105
|
-
- [Security](docs/security.md): owners, tool scoping, retention, regex safety
|
|
148
|
+
- [Configuration](docs/configuration.md): budget, an explorer model, stores, formats, extractors, pictures in PDFs
|
|
149
|
+
- [How it works](docs/how-it-works.md): planner, cards, tools, map-reduce, looking at pages, and the Gemini quirks it handles
|
|
150
|
+
- [Security](docs/security.md): owners, tool scoping, retention, original files, regex safety
|
|
106
151
|
|
|
107
152
|
## Requirements
|
|
108
153
|
|
|
109
154
|
Node.js 20.19 or newer. The package is ESM; CommonJS projects can `require()` it on Node 20.19+ and 22.12+.
|
|
110
|
-
For DOCX/PPTX install `jszip`,
|
|
155
|
+
For DOCX/PPTX install `jszip`, for PDF install `unpdf`, and to mark and show the pages of a PDF
|
|
156
|
+
install `@cantoo/pdf-lib`. All three are optional:
|
|
111
157
|
|
|
112
158
|
```bash
|
|
113
|
-
npm install jszip unpdf
|
|
159
|
+
npm install jszip unpdf @cantoo/pdf-lib
|
|
114
160
|
```
|
|
115
161
|
|
|
116
162
|
## License
|
package/dist/extract.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DocumentFormat, ExtractedText, Extractor } from "./types.js";
|
|
1
|
+
import type { DocumentFormat, ExtractedText, ExtractInput, Extractor } from "./types.js";
|
|
2
2
|
/** Picks a format from the file name, then the MIME type. Unknown text-like files are "text". */
|
|
3
3
|
export declare function detectFormat(name: string, mimeType: string): DocumentFormat;
|
|
4
4
|
/**
|
|
@@ -15,16 +15,23 @@ export type PdfExtractorOptions = {
|
|
|
15
15
|
* in version 0.1.0.
|
|
16
16
|
*/
|
|
17
17
|
tables?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Write a line under the page header of every page that shows a picture or a drawing, so the
|
|
20
|
+
* model knows that the text does not tell all of that page. Defaults to true. It needs the
|
|
21
|
+
* optional dependency `@cantoo/pdf-lib`; without it, and with false, the text is that of 0.2.0.
|
|
22
|
+
*/
|
|
23
|
+
pictures?: boolean;
|
|
18
24
|
};
|
|
25
|
+
/**
|
|
26
|
+
* Written under the header of a page that shows a picture or a drawing. The words hold whether or
|
|
27
|
+
* not the model is later given a way to look at the page.
|
|
28
|
+
*/
|
|
29
|
+
export declare const PICTURE_ON_PAGE = "[Picture on this page. This text cannot show it.]";
|
|
19
30
|
/**
|
|
20
31
|
* The built-in PDF extractor (needs `unpdf`). Use it to change an option:
|
|
21
32
|
* `createLargeFiles({ extractors: { pdf: createPdfExtractor({ tables: false }) } })`.
|
|
22
33
|
*/
|
|
23
34
|
export declare function createPdfExtractor(options?: PdfExtractorOptions): Extractor;
|
|
24
35
|
export declare const defaultExtractors: Record<DocumentFormat, Extractor>;
|
|
25
|
-
export declare function extractText(bytes: Uint8Array, input:
|
|
26
|
-
name: string;
|
|
27
|
-
mimeType: string;
|
|
28
|
-
format: DocumentFormat;
|
|
29
|
-
}, extractors?: Partial<Record<DocumentFormat, Extractor>>): Promise<ExtractedText>;
|
|
36
|
+
export declare function extractText(bytes: Uint8Array, input: ExtractInput, extractors?: Partial<Record<DocumentFormat, Extractor>>): Promise<ExtractedText>;
|
|
30
37
|
//# sourceMappingURL=extract.d.ts.map
|
package/dist/extract.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extract.d.ts","sourceRoot":"","sources":["../src/extract.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"extract.d.ts","sourceRoot":"","sources":["../src/extract.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAmBzF,iGAAiG;AACjG,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,cAAc,CAG3E;AAID;;;GAGG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAkCpD;AAiBD,4GAA4G;AAC5G,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,GAAG,GAAG,GAAG,MAAM,EAAE,CAY5E;AA6CD,MAAM,MAAM,mBAAmB,GAAG;IAChC;;;;OAIG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAIF;;;GAGG;AACH,eAAO,MAAM,eAAe,sDAAsD,CAAC;AAEnF;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,GAAE,mBAAwB,GAAG,SAAS,CAoD/E;AAED,eAAO,MAAM,iBAAiB,EAAE,MAAM,CAAC,cAAc,EAAE,SAAS,CAS/D,CAAC;AAEF,wBAAsB,WAAW,CAC/B,KAAK,EAAE,UAAU,EACjB,KAAK,EAAE,YAAY,EACnB,UAAU,GAAE,OAAO,CAAC,MAAM,CAAC,cAAc,EAAE,SAAS,CAAC,CAAM,GAC1D,OAAO,CAAC,aAAa,CAAC,CAGxB"}
|
package/dist/extract.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { layoutPage, plainPageText } from "./pdf-layout.js";
|
|
2
|
+
import { findPicturePages } from "./pdf-pictures.js";
|
|
2
3
|
const EXTENSION_FORMATS = {
|
|
3
4
|
log: "log", out: "log", err: "log",
|
|
4
5
|
md: "markdown", markdown: "markdown",
|
|
@@ -147,13 +148,19 @@ const pptx = async (bytes) => {
|
|
|
147
148
|
};
|
|
148
149
|
/** Written in place of a page that has no text at all, so the model can say so instead of guessing. */
|
|
149
150
|
const PAGE_WITHOUT_TEXT = "[No text on this page. It may be a scan, a photo, or a chart.]";
|
|
151
|
+
/**
|
|
152
|
+
* Written under the header of a page that shows a picture or a drawing. The words hold whether or
|
|
153
|
+
* not the model is later given a way to look at the page.
|
|
154
|
+
*/
|
|
155
|
+
export const PICTURE_ON_PAGE = "[Picture on this page. This text cannot show it.]";
|
|
150
156
|
/**
|
|
151
157
|
* The built-in PDF extractor (needs `unpdf`). Use it to change an option:
|
|
152
158
|
* `createLargeFiles({ extractors: { pdf: createPdfExtractor({ tables: false }) } })`.
|
|
153
159
|
*/
|
|
154
160
|
export function createPdfExtractor(options = {}) {
|
|
155
161
|
const tables = options.tables ?? true;
|
|
156
|
-
|
|
162
|
+
const pictures = options.pictures ?? true;
|
|
163
|
+
return async (bytes, input) => {
|
|
157
164
|
let unpdf;
|
|
158
165
|
try {
|
|
159
166
|
unpdf = await import("unpdf");
|
|
@@ -163,16 +170,19 @@ export function createPdfExtractor(options = {}) {
|
|
|
163
170
|
}
|
|
164
171
|
// pdf.js may detach the buffer it reads, so give it a copy.
|
|
165
172
|
const document = await unpdf.getDocumentProxy(new Uint8Array(bytes));
|
|
166
|
-
const
|
|
173
|
+
const texts = [];
|
|
174
|
+
const geometry = [];
|
|
167
175
|
let hasText = false;
|
|
168
176
|
for (let number = 1; number <= document.numPages; number++) {
|
|
169
177
|
const page = await document.getPage(number);
|
|
170
178
|
const content = await page.getTextContent();
|
|
171
179
|
const items = content.items.filter((item) => typeof item.str === "string");
|
|
180
|
+
const transform = page.getViewport({ scale: 1 }).transform;
|
|
172
181
|
let text = "";
|
|
182
|
+
let found = [];
|
|
173
183
|
if (tables) {
|
|
174
184
|
try {
|
|
175
|
-
text =
|
|
185
|
+
({ text, tables: found } = layoutPage(items, transform));
|
|
176
186
|
}
|
|
177
187
|
catch {
|
|
178
188
|
// Unusual geometry must never lose a page: fall back to the plain order.
|
|
@@ -185,12 +195,23 @@ export function createPdfExtractor(options = {}) {
|
|
|
185
195
|
text = text.trim();
|
|
186
196
|
if (text)
|
|
187
197
|
hasText = true;
|
|
188
|
-
|
|
198
|
+
texts.push(text);
|
|
199
|
+
geometry.push({ view: page.view, transform, tables: found });
|
|
189
200
|
page.cleanup();
|
|
190
201
|
}
|
|
191
202
|
if (!hasText) {
|
|
192
|
-
|
|
203
|
+
// A scan has nothing to search, but a short one can be read by looking at its pages, when the
|
|
204
|
+
// caller says that they can be shown. Every page then says that it has no text.
|
|
205
|
+
const most = input?.scannedPages ?? 0;
|
|
206
|
+
if (most <= 0)
|
|
207
|
+
throw new Error("This PDF has no text layer (it may be scanned). It needs OCR before it can be searched.");
|
|
208
|
+
if (document.numPages > most) {
|
|
209
|
+
throw new Error(`This PDF has no text layer (it may be scanned) and ${document.numPages} pages. Without text it can only be read by looking at its pages, which works for up to ${most} pages. It needs OCR first.`);
|
|
210
|
+
}
|
|
193
211
|
}
|
|
212
|
+
// Which pages show a picture. Undefined when that could not be found out: then none is marked.
|
|
213
|
+
const pictured = pictures ? await findPicturePages(bytes, geometry) : undefined;
|
|
214
|
+
const pages = texts.map((text, index) => `--- Page ${index + 1} ---\n${pictured?.[index] ? `${PICTURE_ON_PAGE}\n` : ""}${text || PAGE_WITHOUT_TEXT}`);
|
|
194
215
|
return { text: pages.join("\n\n"), pages: document.numPages };
|
|
195
216
|
};
|
|
196
217
|
}
|
package/dist/extract.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extract.js","sourceRoot":"","sources":["../src/extract.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"extract.js","sourceRoot":"","sources":["../src/extract.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,aAAa,EAAoC,MAAM,iBAAiB,CAAC;AAC9F,OAAO,EAAE,gBAAgB,EAAqB,MAAM,mBAAmB,CAAC;AAGxE,MAAM,iBAAiB,GAAmC;IACxD,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK;IAClC,EAAE,EAAE,UAAU,EAAE,QAAQ,EAAE,UAAU;IACpC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;IAC3C,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK;IACtB,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;CACvC,CAAC;AAEF,MAAM,Y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.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { LargeFiles } from "../library.js";
|
|
2
2
|
import type { DocumentPlan } from "../types.js";
|
|
3
3
|
import { type GeminiEndpoint } from "./endpoints.js";
|
|
4
|
+
import { type GeminiToolFilePlacement } from "./generate.js";
|
|
4
5
|
export type GeminiPart = Record<string, unknown>;
|
|
5
6
|
export type GeminiContent = {
|
|
6
7
|
role: "user" | "model";
|
|
@@ -13,6 +14,15 @@ export type ChatTurn = {
|
|
|
13
14
|
};
|
|
14
15
|
/** Sent (invisibly to the user) when the model must stop using tools and answer. */
|
|
15
16
|
export declare const FINAL_ANSWER_REQUEST = "Stop using tools now. Answer my question using only what the tools returned above, with line citations. Say plainly what you could not find.";
|
|
17
|
+
/** The same, when the only tool was `view_pages`: the text of the files is in the prompt, not in tool results. */
|
|
18
|
+
export declare const FINAL_ANSWER_AFTER_PAGES = "Stop using tools now. Answer my question from the text of the files and the pages you looked at. Say plainly what you could not see or find.";
|
|
19
|
+
/** Sent to the answering model when an explorer model used no tool, so there is no tool result to answer from. */
|
|
20
|
+
export declare const FINAL_ANSWER_WITHOUT_TOOLS = "Answer my question now, from what is in this conversation. No tools are available for this reply: if the answer needs a part of a file that is not shown here, say so plainly.";
|
|
21
|
+
/**
|
|
22
|
+
* Added to the system instruction of an explorer model's requests: it gathers what the answer
|
|
23
|
+
* needs, and the other model writes the answer.
|
|
24
|
+
*/
|
|
25
|
+
export declare const EXPLORER_INSTRUCTION = "You only gather material in this conversation: another model writes the answer. It sees this conversation and what your tool calls returned, not what you thought or wrote. So call the tools until their results hold everything the answer needs from the files. Then, or at once if no tool is needed, reply with the single word DONE instead of an answer.";
|
|
16
26
|
/** Some document IDs were never uploaded, have expired, or belong to another owner. */
|
|
17
27
|
export declare class DocumentsNotFoundError extends Error {
|
|
18
28
|
readonly ids: string[];
|
|
@@ -25,6 +35,12 @@ export declare class GeminiRequestError extends Error {
|
|
|
25
35
|
}
|
|
26
36
|
export type AskGeminiOptions = {
|
|
27
37
|
files: LargeFiles;
|
|
38
|
+
/**
|
|
39
|
+
* Where the model is: the one that answers. When the endpoint also names an explorer model
|
|
40
|
+
* (`explorerModel` in `geminiApi()` and `vertexAi()`, or `explorerUrl`), that model makes the
|
|
41
|
+
* tool calls and reads a whole file in parts, and this one writes the answer from what the
|
|
42
|
+
* tools returned. Without an explorer, one model does both.
|
|
43
|
+
*/
|
|
28
44
|
endpoint: GeminiEndpoint;
|
|
29
45
|
/** Documents this conversation may use. Where their text or cards go: see `documentPlacement`. */
|
|
30
46
|
documentIds: string[];
|
|
@@ -42,17 +58,40 @@ export type AskGeminiOptions = {
|
|
|
42
58
|
system?: string;
|
|
43
59
|
/** Sent with every chat request: temperature, maxOutputTokens, thinkingConfig, and so on. */
|
|
44
60
|
generationConfig?: Record<string, unknown>;
|
|
61
|
+
/**
|
|
62
|
+
* The same, for the requests of an explorer model. Defaults to `generationConfig`. Set it when
|
|
63
|
+
* the two models do not take the same settings: a thinking level that one of them refuses, for
|
|
64
|
+
* example. It is not used without an explorer model.
|
|
65
|
+
*/
|
|
66
|
+
explorerGenerationConfig?: Record<string, unknown>;
|
|
45
67
|
/** The user asking. Documents ingested with a different owner are treated as missing. */
|
|
46
68
|
owner?: string;
|
|
47
69
|
/**
|
|
48
70
|
* The most model requests for one question, counting the answer. Tools can run in all but the
|
|
49
71
|
* last: if the model is still calling tools then, the last request is sent with no tools so it
|
|
50
|
-
* must answer. Defaults to 12.
|
|
72
|
+
* must answer. Defaults to 12. With an explorer model the answer is always a request of its
|
|
73
|
+
* own, the last one, so the explorer has one less than this.
|
|
51
74
|
*/
|
|
52
75
|
maxToolTurns?: number;
|
|
53
76
|
/** Offer `analyze_whole_document`, which reads a whole file in many model calls. Defaults to true. */
|
|
54
77
|
wholeDocumentAnalysis?: boolean;
|
|
55
|
-
/**
|
|
78
|
+
/**
|
|
79
|
+
* Offer `view_pages` for PDFs whose original file is kept (`keepOriginals`), so the model can
|
|
80
|
+
* look at pictures, charts and layout. Defaults to true. It changes nothing for a library that
|
|
81
|
+
* keeps no originals.
|
|
82
|
+
*/
|
|
83
|
+
viewPages?: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Where the pages go in the request. "inside" (the default) puts the PDF in the function
|
|
86
|
+
* response itself, which Gemini 3 models read. "beside" sends it in a user turn of its own,
|
|
87
|
+
* after the function responses: use it for Gemini 2.5, which does not see a file inside a
|
|
88
|
+
* function response.
|
|
89
|
+
*/
|
|
90
|
+
toolFilePlacement?: GeminiToolFilePlacement;
|
|
91
|
+
/**
|
|
92
|
+
* generationConfig for whole-document part reads. Defaults to `{ temperature: 0.2, maxOutputTokens }`.
|
|
93
|
+
* The parts are read by the explorer model when there is one, so write it for that model.
|
|
94
|
+
*/
|
|
56
95
|
analysisConfig?: (maxOutputTokens: number) => Record<string, unknown>;
|
|
57
96
|
/** Answer text as it streams. Streaming is used when this is set and the endpoint has `streamUrl`. */
|
|
58
97
|
onText?: (delta: string) => void;
|
|
@@ -64,7 +103,10 @@ export type AskGeminiOptions = {
|
|
|
64
103
|
signal?: AbortSignal;
|
|
65
104
|
};
|
|
66
105
|
export type AskGeminiResult = {
|
|
67
|
-
/**
|
|
106
|
+
/**
|
|
107
|
+
* Everything the model wrote, including any remark it made before calling a tool. With an
|
|
108
|
+
* explorer model, only what the answering model wrote: the explorer's words are not shown.
|
|
109
|
+
*/
|
|
68
110
|
text: string;
|
|
69
111
|
/**
|
|
70
112
|
* False when the model's last turn had no text: the prompt was blocked, the reply was cut off
|
|
@@ -78,7 +120,7 @@ export type AskGeminiResult = {
|
|
|
78
120
|
steps: string[];
|
|
79
121
|
/** Which documents went in whole and which were searched. */
|
|
80
122
|
plan: DocumentPlan;
|
|
81
|
-
/** Model requests made for the chat itself (whole-document part reads not included). */
|
|
123
|
+
/** Model requests made for the chat itself, an explorer model's among them (whole-document part reads not included). */
|
|
82
124
|
turns: number;
|
|
83
125
|
finishReason?: string;
|
|
84
126
|
/** Usage metadata of the last model response. */
|
|
@@ -88,6 +130,8 @@ export type AskGeminiResult = {
|
|
|
88
130
|
* Answers a chat turn about large documents with Gemini (Generate Content REST).
|
|
89
131
|
* Documents that fit go into the prompt whole; otherwise Gemini gets search/read tools and this
|
|
90
132
|
* runs the tool loop, ending with a tool-free turn so there is always an answer.
|
|
133
|
+
* When the endpoint names an explorer model, that model runs the tool loop and the endpoint's own
|
|
134
|
+
* model writes the answer, in the tool-free turn.
|
|
91
135
|
*/
|
|
92
136
|
export declare function askGemini(options: AskGeminiOptions): Promise<AskGeminiResult>;
|
|
93
137
|
//# sourceMappingURL=chat.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chat.d.ts","sourceRoot":"","sources":["../../src/gemini/chat.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"chat.d.ts","sourceRoot":"","sources":["../../src/gemini/chat.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAIhD,OAAO,KAAK,EAAE,YAAY,EAAwC,MAAM,aAAa,CAAC;AACtF,OAAO,EAAkB,KAAK,cAAc,EAAE,MAAM,gBAAgB,CAAC;AACrE,OAAO,EAAkF,KAAK,uBAAuB,EAAE,MAAM,eAAe,CAAC;AAG7I,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AACjD,MAAM,MAAM,aAAa,GAAG;IAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;IAAC,KAAK,EAAE,UAAU,EAAE,CAAA;CAAE,CAAC;AAC5E,0EAA0E;AAC1E,MAAM,MAAM,QAAQ,GAAG;IAAE,IAAI,EAAE,MAAM,GAAG,OAAO,GAAG,WAAW,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAE9E,oFAAoF;AACpF,eAAO,MAAM,oBAAoB,iJAAiJ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