llm-large-files 0.3.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 +30 -0
- package/README.md +21 -1
- package/dist/gemini/chat.d.ts +32 -4
- package/dist/gemini/chat.d.ts.map +1 -1
- package/dist/gemini/chat.js +65 -15
- 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/index.d.ts +1 -1
- package/dist/gemini/index.d.ts.map +1 -1
- package/dist/gemini/index.js +1 -1
- package/dist/gemini/index.js.map +1 -1
- package/docs/api.md +20 -6
- package/docs/configuration.md +122 -0
- package/docs/getting-started.md +16 -0
- package/docs/how-it-works.md +21 -0
- package/package.json +1 -1
- package/src/gemini/chat.ts +87 -19
- package/src/gemini/endpoints.ts +34 -2
- package/src/gemini/index.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,36 @@ 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
|
+
|
|
7
37
|
## [0.3.0] - 2026-11-10
|
|
8
38
|
|
|
9
39
|
The model can look at the pages of a large PDF. Until now such a PDF reached it as text only, and
|
package/README.md
CHANGED
|
@@ -99,6 +99,26 @@ not see the chart; with `view_pages` it looked at the page and answered correctl
|
|
|
99
99
|
at a page for any of ten questions that the text answers. A page costs about 530 tokens.
|
|
100
100
|
Details, limits and measurements: [Pictures in PDFs](docs/configuration.md#pictures-in-pdfs).
|
|
101
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
|
+
|
|
102
122
|
## What it handles
|
|
103
123
|
|
|
104
124
|
| | |
|
|
@@ -125,7 +145,7 @@ With `gemini-3.8-flash` on the Gemini API:
|
|
|
125
145
|
|
|
126
146
|
- [Integration guide](docs/getting-started.md): Express, plain Node, browser upload, streaming, Vertex AI, gateways, existing chat history
|
|
127
147
|
- [API reference](docs/api.md)
|
|
128
|
-
- [Configuration](docs/configuration.md): budget, stores, formats, extractors, pictures in PDFs
|
|
148
|
+
- [Configuration](docs/configuration.md): budget, an explorer model, stores, formats, extractors, pictures in PDFs
|
|
129
149
|
- [How it works](docs/how-it-works.md): planner, cards, tools, map-reduce, looking at pages, and the Gemini quirks it handles
|
|
130
150
|
- [Security](docs/security.md): owners, tool scoping, retention, original files, regex safety
|
|
131
151
|
|
package/dist/gemini/chat.d.ts
CHANGED
|
@@ -16,6 +16,13 @@ export type ChatTurn = {
|
|
|
16
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
17
|
/** The same, when the only tool was `view_pages`: the text of the files is in the prompt, not in tool results. */
|
|
18
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.";
|
|
19
26
|
/** Some document IDs were never uploaded, have expired, or belong to another owner. */
|
|
20
27
|
export declare class DocumentsNotFoundError extends Error {
|
|
21
28
|
readonly ids: string[];
|
|
@@ -28,6 +35,12 @@ export declare class GeminiRequestError extends Error {
|
|
|
28
35
|
}
|
|
29
36
|
export type AskGeminiOptions = {
|
|
30
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
|
+
*/
|
|
31
44
|
endpoint: GeminiEndpoint;
|
|
32
45
|
/** Documents this conversation may use. Where their text or cards go: see `documentPlacement`. */
|
|
33
46
|
documentIds: string[];
|
|
@@ -45,12 +58,19 @@ export type AskGeminiOptions = {
|
|
|
45
58
|
system?: string;
|
|
46
59
|
/** Sent with every chat request: temperature, maxOutputTokens, thinkingConfig, and so on. */
|
|
47
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>;
|
|
48
67
|
/** The user asking. Documents ingested with a different owner are treated as missing. */
|
|
49
68
|
owner?: string;
|
|
50
69
|
/**
|
|
51
70
|
* The most model requests for one question, counting the answer. Tools can run in all but the
|
|
52
71
|
* last: if the model is still calling tools then, the last request is sent with no tools so it
|
|
53
|
-
* 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.
|
|
54
74
|
*/
|
|
55
75
|
maxToolTurns?: number;
|
|
56
76
|
/** Offer `analyze_whole_document`, which reads a whole file in many model calls. Defaults to true. */
|
|
@@ -68,7 +88,10 @@ export type AskGeminiOptions = {
|
|
|
68
88
|
* function response.
|
|
69
89
|
*/
|
|
70
90
|
toolFilePlacement?: GeminiToolFilePlacement;
|
|
71
|
-
/**
|
|
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
|
+
*/
|
|
72
95
|
analysisConfig?: (maxOutputTokens: number) => Record<string, unknown>;
|
|
73
96
|
/** Answer text as it streams. Streaming is used when this is set and the endpoint has `streamUrl`. */
|
|
74
97
|
onText?: (delta: string) => void;
|
|
@@ -80,7 +103,10 @@ export type AskGeminiOptions = {
|
|
|
80
103
|
signal?: AbortSignal;
|
|
81
104
|
};
|
|
82
105
|
export type AskGeminiResult = {
|
|
83
|
-
/**
|
|
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
|
+
*/
|
|
84
110
|
text: string;
|
|
85
111
|
/**
|
|
86
112
|
* False when the model's last turn had no text: the prompt was blocked, the reply was cut off
|
|
@@ -94,7 +120,7 @@ export type AskGeminiResult = {
|
|
|
94
120
|
steps: string[];
|
|
95
121
|
/** Which documents went in whole and which were searched. */
|
|
96
122
|
plan: DocumentPlan;
|
|
97
|
-
/** 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). */
|
|
98
124
|
turns: number;
|
|
99
125
|
finishReason?: string;
|
|
100
126
|
/** Usage metadata of the last model response. */
|
|
@@ -104,6 +130,8 @@ export type AskGeminiResult = {
|
|
|
104
130
|
* Answers a chat turn about large documents with Gemini (Generate Content REST).
|
|
105
131
|
* Documents that fit go into the prompt whole; otherwise Gemini gets search/read tools and this
|
|
106
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.
|
|
107
135
|
*/
|
|
108
136
|
export declare function askGemini(options: AskGeminiOptions): Promise<AskGeminiResult>;
|
|
109
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;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;IA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E;IACtE,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IACpC,2FAA2F;IAC3F,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IAChC,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;IACrB,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B;;;OAGG;IACH,IAAI,EAAE,MAAM,CAAC;IACb;;;;OAIG;IACH,QAAQ,EAAE,OAAO,CAAC;IAClB,iFAAiF;IACjF,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,yEAAyE;IACzE,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,6DAA6D;IAC7D,IAAI,EAAE,YAAY,CAAC;IACnB,wHAAwH;IACxH,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,iDAAiD;IACjD,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACjC,CAAC;AAsDF;;;;;;GAMG;AACH,wBAAsB,SAAS,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,eAAe,CAAC,CAuMnF"}
|
package/dist/gemini/chat.js
CHANGED
|
@@ -8,6 +8,13 @@ import { parseSseJson } from "./sse.js";
|
|
|
8
8
|
export 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.";
|
|
9
9
|
/** The same, when the only tool was `view_pages`: the text of the files is in the prompt, not in tool results. */
|
|
10
10
|
export 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.";
|
|
11
|
+
/** Sent to the answering model when an explorer model used no tool, so there is no tool result to answer from. */
|
|
12
|
+
export 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.";
|
|
13
|
+
/**
|
|
14
|
+
* Added to the system instruction of an explorer model's requests: it gathers what the answer
|
|
15
|
+
* needs, and the other model writes the answer.
|
|
16
|
+
*/
|
|
17
|
+
export 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.";
|
|
11
18
|
/** What Gemini bills for one PDF page it is shown (measured: 520 to 532). */
|
|
12
19
|
const PAGE_TOKENS = 530;
|
|
13
20
|
/** Some document IDs were never uploaded, have expired, or belong to another owner. */
|
|
@@ -55,14 +62,28 @@ function baseSize(contents, system) {
|
|
|
55
62
|
}
|
|
56
63
|
return { tokens, bytes };
|
|
57
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* A turn of an explorer model as the answering model gets it: the tool calls alone. A thought
|
|
67
|
+
* signature belongs to the model that wrote it, and the explorer's thoughts and remarks are not
|
|
68
|
+
* what the files say.
|
|
69
|
+
*/
|
|
70
|
+
function callsOnly(content) {
|
|
71
|
+
if (content.role !== "model")
|
|
72
|
+
return content;
|
|
73
|
+
return { role: "model", parts: content.parts.flatMap((part) => (part.functionCall ? [{ functionCall: part.functionCall }] : [])) };
|
|
74
|
+
}
|
|
58
75
|
/**
|
|
59
76
|
* Answers a chat turn about large documents with Gemini (Generate Content REST).
|
|
60
77
|
* Documents that fit go into the prompt whole; otherwise Gemini gets search/read tools and this
|
|
61
78
|
* runs the tool loop, ending with a tool-free turn so there is always an answer.
|
|
79
|
+
* When the endpoint names an explorer model, that model runs the tool loop and the endpoint's own
|
|
80
|
+
* model writes the answer, in the tool-free turn.
|
|
62
81
|
*/
|
|
63
82
|
export async function askGemini(options) {
|
|
64
83
|
const fetchImpl = options.fetch ?? fetch;
|
|
65
84
|
const { files, endpoint, signal } = options;
|
|
85
|
+
// A second model for the requests that use tools. The answering model's own URL is not one.
|
|
86
|
+
const explorerUrl = endpoint.explorerUrl && endpoint.explorerUrl !== endpoint.url ? endpoint.explorerUrl : undefined;
|
|
66
87
|
const contents = toContents(options);
|
|
67
88
|
const prepared = await files.prepare(options.documentIds, baseSize(contents, options.system), { owner: options.owner, viewPages: options.viewPages !== false });
|
|
68
89
|
if (prepared.missing.length)
|
|
@@ -97,13 +118,16 @@ export async function askGemini(options) {
|
|
|
97
118
|
/**
|
|
98
119
|
* One model request. Text is emitted as it arrives; parts are kept as received.
|
|
99
120
|
* `wroteText` says whether this request produced any text of its own.
|
|
121
|
+
* Given `exploring`, the request is the explorer model's: that body goes to its URL, in one
|
|
122
|
+
* piece, and what it writes is not emitted, because the answer comes from the other model.
|
|
100
123
|
*/
|
|
101
|
-
const turn = async () => {
|
|
124
|
+
const turn = async (exploring) => {
|
|
102
125
|
turns++;
|
|
103
|
-
const
|
|
126
|
+
const stream = streaming && !exploring;
|
|
127
|
+
const response = await fetchImpl(exploring ? explorerUrl : stream ? endpoint.streamUrl : endpoint.url, {
|
|
104
128
|
method: "POST",
|
|
105
129
|
headers,
|
|
106
|
-
body: JSON.stringify(body),
|
|
130
|
+
body: JSON.stringify(exploring ?? body),
|
|
107
131
|
signal,
|
|
108
132
|
});
|
|
109
133
|
if (!response.ok) {
|
|
@@ -124,11 +148,13 @@ export async function askGemini(options) {
|
|
|
124
148
|
// Keep every part unchanged: Gemini 3 requires its thought signatures back.
|
|
125
149
|
parts.push(part);
|
|
126
150
|
if (typeof part.text === "string" && part.text && part.thought !== true) {
|
|
127
|
-
if (!
|
|
128
|
-
|
|
129
|
-
|
|
151
|
+
if (!exploring) {
|
|
152
|
+
if (!separated) {
|
|
153
|
+
emit("\n\n");
|
|
154
|
+
separated = true;
|
|
155
|
+
}
|
|
156
|
+
emit(part.text);
|
|
130
157
|
}
|
|
131
|
-
emit(part.text);
|
|
132
158
|
if (part.text.trim())
|
|
133
159
|
wroteText = true;
|
|
134
160
|
}
|
|
@@ -141,7 +167,7 @@ export async function askGemini(options) {
|
|
|
141
167
|
finishReason = candidate?.finishReason ?? finishReason;
|
|
142
168
|
usage = chunk.usageMetadata ?? usage;
|
|
143
169
|
};
|
|
144
|
-
if (
|
|
170
|
+
if (stream) {
|
|
145
171
|
if (!response.body)
|
|
146
172
|
throw new GeminiRequestError("Gemini returned an empty stream.", response.status);
|
|
147
173
|
for await (const chunk of parseSseJson(response.body))
|
|
@@ -170,14 +196,27 @@ export async function askGemini(options) {
|
|
|
170
196
|
return result((await turn()).wroteText);
|
|
171
197
|
const tools = files.tools(searchable, {
|
|
172
198
|
...(options.wholeDocumentAnalysis === false ? {} : {
|
|
173
|
-
|
|
199
|
+
// Reading a whole file in parts is exploring too: the explorer model's work, when there is one.
|
|
200
|
+
generate: createGeminiGenerateText({ url: explorerUrl ?? endpoint.url, headers, fetchImpl, ...(options.analysisConfig ? { generationConfig: options.analysisConfig } : {}) }),
|
|
174
201
|
}),
|
|
175
202
|
onProgress: (event) => options.onActivity?.(`${event.message}…`),
|
|
176
203
|
...(signal ? { signal } : {}),
|
|
177
204
|
...(viewable.length > 0 ? { viewPages: viewable } : {}),
|
|
178
205
|
});
|
|
179
|
-
|
|
180
|
-
|
|
206
|
+
// The request that uses the tools is the chat's own, or one for the explorer model: the same
|
|
207
|
+
// conversation (`contents` is one array, so it grows in both), that model's settings, and a
|
|
208
|
+
// line that says what its part is.
|
|
209
|
+
const explorerConfig = options.explorerGenerationConfig ?? options.generationConfig;
|
|
210
|
+
const exploring = explorerUrl ? {
|
|
211
|
+
contents,
|
|
212
|
+
systemInstruction: { parts: [{ text: `${system}\n\n${EXPLORER_INSTRUCTION}` }] },
|
|
213
|
+
...(explorerConfig ? { generationConfig: explorerConfig } : {}),
|
|
214
|
+
} : undefined;
|
|
215
|
+
const withTools = exploring ?? body;
|
|
216
|
+
withTools.tools = [{ functionDeclarations: tools.declarations }];
|
|
217
|
+
withTools.toolConfig = { functionCallingConfig: { mode: "AUTO" } };
|
|
218
|
+
// How many turns the conversation had as it was asked. What follows them is the tool loop's.
|
|
219
|
+
const asked = contents.length;
|
|
181
220
|
if (searchable.length > 0)
|
|
182
221
|
options.onActivity?.(`Too large to read at once: ${searchable.map((meta) => meta.name).join(", ")}. Searching instead…`);
|
|
183
222
|
// Pages the model looks at stay in the request until the question is answered, so they are
|
|
@@ -191,10 +230,11 @@ export async function askGemini(options) {
|
|
|
191
230
|
// Tools run in at most maxTurns - 1 requests. The last request is the tool-free one below, so a
|
|
192
231
|
// model that never stops calling tools does not cost a request whose calls are thrown away.
|
|
193
232
|
for (let round = 1; round < maxTurns; round++) {
|
|
194
|
-
const { parts, calls, wroteText } = await turn();
|
|
233
|
+
const { parts, calls, wroteText } = await turn(exploring);
|
|
195
234
|
if (calls.length === 0) {
|
|
196
235
|
// Text from this turn is the answer. A remark made earlier, before a tool call, is not.
|
|
197
|
-
|
|
236
|
+
// Neither is anything an explorer model writes: the answer is asked for below.
|
|
237
|
+
answered = wroteText && !exploring;
|
|
198
238
|
break;
|
|
199
239
|
}
|
|
200
240
|
body.contents.push({ role: "model", parts });
|
|
@@ -206,7 +246,7 @@ export async function askGemini(options) {
|
|
|
206
246
|
options.onActivity?.(`${label}…`);
|
|
207
247
|
let limits;
|
|
208
248
|
if (call.name === TOOL_NAMES.view) {
|
|
209
|
-
requestBytes ??= Buffer.byteLength(JSON.stringify(
|
|
249
|
+
requestBytes ??= Buffer.byteLength(JSON.stringify(withTools));
|
|
210
250
|
// Base64 turns three bytes into four characters.
|
|
211
251
|
limits = { maxFileBytes: Math.floor((maxRequestBytes - bytesKeptFree - requestBytes) * 3 / 4), maxPages: pagesLeft };
|
|
212
252
|
}
|
|
@@ -225,11 +265,21 @@ export async function askGemini(options) {
|
|
|
225
265
|
// Out of tool turns, or the last turn had no text (cut off, or a malformed tool call): ask once
|
|
226
266
|
// more with no tools declared at all. (Gemini can ignore functionCallingConfig mode NONE and
|
|
227
267
|
// keep calling tools.) A blocked prompt would only be blocked again.
|
|
268
|
+
// With an explorer model this request is always made: it is the one the answering model gets.
|
|
228
269
|
if (!answered && !blockReason) {
|
|
229
270
|
options.onActivity?.("Writing the answer…");
|
|
230
271
|
delete body.tools;
|
|
231
272
|
delete body.toolConfig;
|
|
232
|
-
|
|
273
|
+
let request = searchable.length > 0 ? FINAL_ANSWER_REQUEST : FINAL_ANSWER_AFTER_PAGES;
|
|
274
|
+
if (exploring) {
|
|
275
|
+
// The answering model gets the explorer's calls and what they returned, and nothing else of
|
|
276
|
+
// it. This request ends with a user message, as it must: a Gemini 3 model refuses a
|
|
277
|
+
// function call without its own thought signature in the turn it is still answering.
|
|
278
|
+
body.contents = [...contents.slice(0, asked), ...contents.slice(asked).map(callsOnly)];
|
|
279
|
+
if (steps.length === 0)
|
|
280
|
+
request = FINAL_ANSWER_WITHOUT_TOOLS;
|
|
281
|
+
}
|
|
282
|
+
body.contents.push({ role: "user", parts: [{ text: request }] });
|
|
233
283
|
answered = (await turn()).wroteText;
|
|
234
284
|
}
|
|
235
285
|
return result(answered);
|
package/dist/gemini/chat.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chat.js","sourceRoot":"","sources":["../../src/gemini/chat.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvD,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAE5D,OAAO,EAAE,cAAc,EAAuB,MAAM,gBAAgB,CAAC;AACrE,OAAO,EAAE,wBAAwB,EAAE,2BAA2B,EAAyD,MAAM,eAAe,CAAC;AAC7I,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAOxC,oFAAoF;AACpF,MAAM,CAAC,MAAM,oBAAoB,GAAG,8IAA8I,CAAC;AACnL,kHAAkH;AAClH,MAAM,CAAC,MAAM,wBAAwB,GAAG,8IAA8I,CAAC;AAEvL,6EAA6E;AAC7E,MA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,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IACxE,OAAO,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAK,GAAG,KAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACvF,CAAC"}
|
|
@@ -9,11 +9,25 @@ export type GeminiEndpoint = {
|
|
|
9
9
|
streamUrl?: string;
|
|
10
10
|
/** Request headers, or a function returning them (for short-lived access tokens). */
|
|
11
11
|
headers: Record<string, string> | (() => Record<string, string> | Promise<Record<string, string>>);
|
|
12
|
+
/**
|
|
13
|
+
* The non-streaming `...:generateContent` URL of a second model, the explorer. `askGemini` then
|
|
14
|
+
* sends it the requests that search, read and look at pages, and the model of `url` only writes
|
|
15
|
+
* the answer. It is called with the same headers. `geminiApi()` and `vertexAi()` set it from
|
|
16
|
+
* `explorerModel`. Unset, or the same as `url`, one model does both.
|
|
17
|
+
*/
|
|
18
|
+
explorerUrl?: string | undefined;
|
|
12
19
|
};
|
|
13
20
|
/** The Gemini Developer API with an API key. */
|
|
14
21
|
export declare function geminiApi(options: {
|
|
15
22
|
apiKey: string;
|
|
23
|
+
/** The model that answers. Without `explorerModel` it also searches and reads the documents. */
|
|
16
24
|
model: string;
|
|
25
|
+
/**
|
|
26
|
+
* A second model that explores the documents: it makes the tool calls (search, read, look at
|
|
27
|
+
* pages) and reads a whole file in parts, and `model` then writes the answer from what it found.
|
|
28
|
+
* Leave it out or empty to have `model` do both.
|
|
29
|
+
*/
|
|
30
|
+
explorerModel?: string | undefined;
|
|
17
31
|
/** Defaults to "v1beta". */
|
|
18
32
|
apiVersion?: string;
|
|
19
33
|
/** Defaults to https://generativelanguage.googleapis.com. */
|
|
@@ -24,7 +38,10 @@ export declare function vertexHost(location: string): string;
|
|
|
24
38
|
/** Vertex AI with an OAuth access token (for example from google-auth-library). */
|
|
25
39
|
export declare function vertexAi(options: {
|
|
26
40
|
project: string;
|
|
41
|
+
/** The model that answers. Without `explorerModel` it also searches and reads the documents. */
|
|
27
42
|
model: string;
|
|
43
|
+
/** A second model that explores the documents, as in `geminiApi()`. Leave it out or empty for one model. */
|
|
44
|
+
explorerModel?: string | undefined;
|
|
28
45
|
/** Defaults to "global". */
|
|
29
46
|
location?: string;
|
|
30
47
|
/** A token, or a function that returns a fresh one for each request. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoints.d.ts","sourceRoot":"","sources":["../../src/gemini/endpoints.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,+CAA+C;IAC/C,GAAG,EAAE,MAAM,CAAC;IACZ,kGAAkG;IAClG,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,qFAAqF;IACrF,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,MAAM,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"endpoints.d.ts","sourceRoot":"","sources":["../../src/gemini/endpoints.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,+CAA+C;IAC/C,GAAG,EAAE,MAAM,CAAC;IACZ,kGAAkG;IAClG,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,qFAAqF;IACrF,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,MAAM,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC;IACnG;;;;;OAKG;IACH,WAAW,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAClC,CAAC;AAWF,gDAAgD;AAChD,wBAAgB,SAAS,CAAC,OAAO,EAAE;IACjC,MAAM,EAAE,MAAM,CAAC;IACf,gGAAgG;IAChG,KAAK,EAAE,MAAM,CAAC;IACd;;;;OAIG;IACH,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACnC,4BAA4B;IAC5B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,6DAA6D;IAC7D,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,GAAG,cAAc,CAUjB;AAED,8FAA8F;AAC9F,wBAAgB,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAInD;AAED,mFAAmF;AACnF,wBAAgB,QAAQ,CAAC,OAAO,EAAE;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,gGAAgG;IAChG,KAAK,EAAE,MAAM,CAAC;IACd,4GAA4G;IAC5G,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACnC,4BAA4B;IAC5B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,wEAAwE;IACxE,WAAW,EAAE,MAAM,GAAG,CAAC,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;IACvD,wBAAwB;IACxB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,GAAG,cAAc,CAejB;AAED,wBAAsB,cAAc,CAAC,QAAQ,EAAE,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAE9F"}
|
package/dist/gemini/endpoints.js
CHANGED
|
@@ -1,10 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The model that explores the documents, when one is named and it is not the answering model
|
|
3
|
+
* itself. An empty name is no name, so a setting that was left empty can be passed as it is.
|
|
4
|
+
*/
|
|
5
|
+
function explorerOf(options) {
|
|
6
|
+
const name = options.explorerModel?.trim();
|
|
7
|
+
return name && name !== options.model ? name : undefined;
|
|
8
|
+
}
|
|
1
9
|
/** The Gemini Developer API with an API key. */
|
|
2
10
|
export function geminiApi(options) {
|
|
3
|
-
const
|
|
11
|
+
const models = `${(options.baseUrl ?? "https://generativelanguage.googleapis.com").replace(/\/+$/, "")}/${options.apiVersion ?? "v1beta"}/models/`;
|
|
12
|
+
const base = `${models}${encodeURIComponent(options.model)}`;
|
|
13
|
+
const explorer = explorerOf(options);
|
|
4
14
|
return {
|
|
5
15
|
url: `${base}:generateContent`,
|
|
6
16
|
streamUrl: `${base}:streamGenerateContent?alt=sse`,
|
|
7
17
|
headers: { "Content-Type": "application/json", "x-goog-api-key": options.apiKey },
|
|
18
|
+
...(explorer ? { explorerUrl: `${models}${encodeURIComponent(explorer)}:generateContent` } : {}),
|
|
8
19
|
};
|
|
9
20
|
}
|
|
10
21
|
/** The host for a Vertex AI location: global, the us/eu multi-regions, or a single region. */
|
|
@@ -18,7 +29,9 @@ export function vertexHost(location) {
|
|
|
18
29
|
/** Vertex AI with an OAuth access token (for example from google-auth-library). */
|
|
19
30
|
export function vertexAi(options) {
|
|
20
31
|
const location = options.location ?? "global";
|
|
21
|
-
const
|
|
32
|
+
const models = `${vertexHost(location)}/${options.apiVersion ?? "v1"}/projects/${encodeURIComponent(options.project)}/locations/${location}/publishers/google/models/`;
|
|
33
|
+
const base = `${models}${encodeURIComponent(options.model)}`;
|
|
34
|
+
const explorer = explorerOf(options);
|
|
22
35
|
const token = options.accessToken;
|
|
23
36
|
return {
|
|
24
37
|
url: `${base}:generateContent`,
|
|
@@ -27,6 +40,7 @@ export function vertexAi(options) {
|
|
|
27
40
|
"Content-Type": "application/json",
|
|
28
41
|
Authorization: `Bearer ${typeof token === "function" ? await token() : token}`,
|
|
29
42
|
}),
|
|
43
|
+
...(explorer ? { explorerUrl: `${models}${encodeURIComponent(explorer)}:generateContent` } : {}),
|
|
30
44
|
};
|
|
31
45
|
}
|
|
32
46
|
export async function resolveHeaders(endpoint) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"endpoints.js","sourceRoot":"","sources":["../../src/gemini/endpoints.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"endpoints.js","sourceRoot":"","sources":["../../src/gemini/endpoints.ts"],"names":[],"mappings":"AAoBA;;;GAGG;AACH,SAAS,UAAU,CAAC,OAA8D;IAChF,MAAM,IAAI,GAAG,OAAO,CAAC,aAAa,EAAE,IAAI,EAAE,CAAC;IAC3C,OAAO,IAAI,IAAI,IAAI,KAAK,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;AAC3D,CAAC;AAED,gDAAgD;AAChD,MAAM,UAAU,SAAS,CAAC,OAczB;IACC,MAAM,MAAM,GAAG,GAAG,CAAC,OAAO,CAAC,OAAO,IAAI,2CAA2C,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,OAAO,CAAC,UAAU,IAAI,QAAQ,UAAU,CAAC;IACnJ,MAAM,IAAI,GAAG,GAAG,MAAM,GAAG,kBAAkB,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;IAC7D,MAAM,QAAQ,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC;IACrC,OAAO;QACL,GAAG,EAAE,GAAG,IAAI,kBAAkB;QAC9B,SAAS,EAAE,GAAG,IAAI,gCAAgC;QAClD,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,OAAO,CAAC,MAAM,EAAE;QACjF,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,GAAG,MAAM,GAAG,kBAAkB,CAAC,QAAQ,CAAC,kBAAkB,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KACjG,CAAC;AACJ,CAAC;AAED,8FAA8F;AAC9F,MAAM,UAAU,UAAU,CAAC,QAAgB;IACzC,IAAI,QAAQ,KAAK,QAAQ;QAAE,OAAO,mCAAmC,CAAC;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E,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC;AACxF,CAAC"}
|
package/dist/gemini/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { askGemini, DocumentsNotFoundError, FINAL_ANSWER_AFTER_PAGES, FINAL_ANSWER_REQUEST, GeminiRequestError } from "./chat.js";
|
|
1
|
+
export { askGemini, DocumentsNotFoundError, EXPLORER_INSTRUCTION, FINAL_ANSWER_AFTER_PAGES, FINAL_ANSWER_REQUEST, FINAL_ANSWER_WITHOUT_TOOLS, GeminiRequestError } from "./chat.js";
|
|
2
2
|
export type { AskGeminiOptions, AskGeminiResult, ChatTurn, GeminiContent, GeminiPart } from "./chat.js";
|
|
3
3
|
export { geminiApi, vertexAi, vertexHost } from "./endpoints.js";
|
|
4
4
|
export type { GeminiEndpoint } from "./endpoints.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/gemini/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,sBAAsB,EAAE,wBAAwB,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/gemini/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,sBAAsB,EAAE,oBAAoB,EAAE,wBAAwB,EAAE,oBAAoB,EAAE,0BAA0B,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AACpL,YAAY,EAAE,gBAAgB,EAAE,eAAe,EAAE,QAAQ,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACxG,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AACjE,YAAY,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,EAAE,wBAAwB,EAAE,uBAAuB,EAAE,2BAA2B,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9H,YAAY,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AACnG,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC"}
|
package/dist/gemini/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { askGemini, DocumentsNotFoundError, FINAL_ANSWER_AFTER_PAGES, FINAL_ANSWER_REQUEST, GeminiRequestError } from "./chat.js";
|
|
1
|
+
export { askGemini, DocumentsNotFoundError, EXPLORER_INSTRUCTION, FINAL_ANSWER_AFTER_PAGES, FINAL_ANSWER_REQUEST, FINAL_ANSWER_WITHOUT_TOOLS, GeminiRequestError } from "./chat.js";
|
|
2
2
|
export { geminiApi, vertexAi, vertexHost } from "./endpoints.js";
|
|
3
3
|
export { createGeminiGenerateText, geminiFunctionResponses, geminiFunctionResponseTurns, toGeminiTools } from "./generate.js";
|
|
4
4
|
export { parseSseJson } from "./sse.js";
|
package/dist/gemini/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/gemini/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,sBAAsB,EAAE,wBAAwB,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/gemini/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,sBAAsB,EAAE,oBAAoB,EAAE,wBAAwB,EAAE,oBAAoB,EAAE,0BAA0B,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAEpL,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAEjE,OAAO,EAAE,wBAAwB,EAAE,uBAAuB,EAAE,2BAA2B,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAE9H,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC"}
|
package/docs/api.md
CHANGED
|
@@ -132,13 +132,14 @@ type ToolFile = { name: string; mimeType: string; bytes: Uint8Array };
|
|
|
132
132
|
| `contents` | Instead of `messages`: raw Gemini `contents` (images, inline PDFs…). Not mutated. |
|
|
133
133
|
| `system` | System instruction; the tool guide is appended when needed. |
|
|
134
134
|
| `generationConfig` | Sent with every chat request. |
|
|
135
|
+
| `explorerGenerationConfig` | The same, for the requests of an [explorer model](configuration.md#an-explorer-model). Defaults to `generationConfig`. Not used without an explorer. |
|
|
135
136
|
| `owner` | Only documents with this owner (or no owner) are used. |
|
|
136
137
|
| `documentPlacement` | `"first"` (default): document text and cards go at the start of the first user turn and stay there, so follow-up questions reuse Gemini's prompt cache. `"last"`: appended to the latest user turn, as in 0.1.0. |
|
|
137
|
-
| `maxToolTurns` | Default 12. The most requests for one question, counting the answer. If the model is still calling tools on the last one, it is sent with no tools so the model must answer. |
|
|
138
|
+
| `maxToolTurns` | Default 12. The most requests for one question, counting the answer. If the model is still calling tools on the last one, it is sent with no tools so the model must answer. With an explorer model the answer is always the last request, so the explorer has one less. |
|
|
138
139
|
| `wholeDocumentAnalysis` | Default `true`. Set `false` to withhold the costly whole-file tool. |
|
|
139
140
|
| `viewPages` | Default `true`. Offers `view_pages` for PDFs whose original is kept. It changes nothing for a library that keeps no originals. |
|
|
140
141
|
| `toolFilePlacement` | `"inside"` (default): the pages go in the function response itself, which Gemini 3 models read. `"beside"`: in a user turn of their own after the function responses, for Gemini 2.5, which does not see a file inside a function response. See [Where the pages go](configuration.md#where-the-pages-go-toolfileplacement). |
|
|
141
|
-
| `analysisConfig` | `(maxOutputTokens) => generationConfig` for whole-file part reads. |
|
|
142
|
+
| `analysisConfig` | `(maxOutputTokens) => generationConfig` for whole-file part reads. The explorer model reads the parts when there is one, so write it for that model. |
|
|
142
143
|
| `onText`, `onActivity`, `onStep` | Streaming answer text, transient status, completed steps. |
|
|
143
144
|
| `fetch`, `signal` | Custom fetch (proxies, tests) and cancellation. |
|
|
144
145
|
|
|
@@ -157,12 +158,22 @@ if (!result.answered) show(result.blockReason ? "The request was blocked." : "No
|
|
|
157
158
|
It throws `DocumentsNotFoundError` (`.ids`) before calling the model, `GeminiRequestError` (`.status`)
|
|
158
159
|
for HTTP errors, and `Error` when the last message isn't the user's.
|
|
159
160
|
|
|
161
|
+
With an [explorer model](configuration.md#an-explorer-model), `text` and `onText` carry only what
|
|
162
|
+
the answering model wrote, `turns` counts the requests of both models, and `usage` and
|
|
163
|
+
`finishReason` are those of the answer. An explorer that fails throws `GeminiRequestError` too.
|
|
164
|
+
|
|
160
165
|
### Endpoints
|
|
161
166
|
|
|
162
|
-
- `geminiApi({ apiKey, model, apiVersion = "v1beta", baseUrl? })`
|
|
163
|
-
- `vertexAi({ project, model, location = "global", accessToken, apiVersion = "v1" })`,
|
|
164
|
-
`accessToken` can be a string or `() => string | Promise<string>`.
|
|
165
|
-
- Or any `{ url, streamUrl?, headers }` (`headers` can be a function).
|
|
167
|
+
- `geminiApi({ apiKey, model, explorerModel?, apiVersion = "v1beta", baseUrl? })`
|
|
168
|
+
- `vertexAi({ project, model, explorerModel?, location = "global", accessToken, apiVersion = "v1" })`,
|
|
169
|
+
where `accessToken` can be a string or `() => string | Promise<string>`.
|
|
170
|
+
- Or any `{ url, streamUrl?, headers, explorerUrl? }` (`headers` can be a function).
|
|
171
|
+
|
|
172
|
+
`model` is the model that answers. `explorerModel` names a second one that makes the tool calls and
|
|
173
|
+
reads whole files in parts, so that `model` only writes the answer. Left out, empty, or the same as
|
|
174
|
+
`model`, one model does both, as before. In an endpoint of your own it is `explorerUrl`: the
|
|
175
|
+
`...:generateContent` URL of the explorer, called with the same headers. See
|
|
176
|
+
[An explorer model](configuration.md#an-explorer-model).
|
|
166
177
|
|
|
167
178
|
### Lower-level helpers
|
|
168
179
|
|
|
@@ -175,6 +186,9 @@ for HTTP errors, and `Error` when the last message isn't the user's.
|
|
|
175
186
|
responses followed by a `user` turn that holds the files.
|
|
176
187
|
- `FINAL_ANSWER_REQUEST`, `FINAL_ANSWER_AFTER_PAGES`: the messages `askGemini` adds when the model
|
|
177
188
|
must stop using tools. The second is used when `view_pages` was the only tool.
|
|
189
|
+
- `FINAL_ANSWER_WITHOUT_TOOLS`, `EXPLORER_INSTRUCTION`: the two texts `askGemini` adds for an
|
|
190
|
+
explorer model. The first asks the answering model for the answer when the explorer called no
|
|
191
|
+
tool. The second is the line in the explorer's system instruction that says what its part is.
|
|
178
192
|
- `parseSseJson(stream)`: async iterator over a Generate Content SSE stream.
|
|
179
193
|
|
|
180
194
|
## `llm-large-files/node`
|
package/docs/configuration.md
CHANGED
|
@@ -56,6 +56,128 @@ Tokenizers differ between providers. These weights are fitted to Gemini, which c
|
|
|
56
56
|
by one. A tokenizer that groups digits will count fewer tokens than this estimate for logs and
|
|
57
57
|
data, which is the safe direction.
|
|
58
58
|
|
|
59
|
+
## An explorer model
|
|
60
|
+
|
|
61
|
+
The model is yours to choose: it is the `model` you give `geminiApi()` or `vertexAi()`, or the URL
|
|
62
|
+
of your own endpoint. The package has no default. That one model does both halves of the work on a
|
|
63
|
+
large file: it explores the file (searches, reads, looks at pages), and it answers.
|
|
64
|
+
`explorerModel` names a second model for the first half:
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
const endpoint = geminiApi({
|
|
68
|
+
apiKey,
|
|
69
|
+
model: "gemini-3.8-flash", // writes the answer
|
|
70
|
+
explorerModel: process.env.GEMINI_EXPLORER_MODEL, // optional: searches and reads
|
|
71
|
+
});
|
|
72
|
+
// vertexAi({ project, model, explorerModel, accessToken }) takes it the same way.
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Left out, empty, or the same as `model`, there is no explorer, and `askGemini` sends exactly the
|
|
76
|
+
requests it sent before. So a setting that may be unset can be passed as it is.
|
|
77
|
+
|
|
78
|
+
With URLs of your own, give the explorer's as `explorerUrl`. It is called with the same headers:
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
const endpoint = {
|
|
82
|
+
url: "https://llm-gateway.corp.example/gemini/models/gemini-3.8-flash:generateContent",
|
|
83
|
+
streamUrl: "https://llm-gateway.corp.example/gemini/models/gemini-3.8-flash:streamGenerateContent?alt=sse",
|
|
84
|
+
explorerUrl: "https://llm-gateway.corp.example/gemini/models/gemini-3.5-flash-lite:generateContent",
|
|
85
|
+
headers,
|
|
86
|
+
};
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
The reason to use one: a question about a large log can take ten requests of searching and
|
|
90
|
+
reading, each larger than the one before, and a whole-file read adds up to 30 part reads. With an
|
|
91
|
+
explorer those go to a cheaper or faster model, and the model you chose for its answers is called
|
|
92
|
+
once.
|
|
93
|
+
|
|
94
|
+
### Who does what
|
|
95
|
+
|
|
96
|
+
| | The explorer | `model` |
|
|
97
|
+
|---|---|---|
|
|
98
|
+
| Requests with tools: `search_document`, `read_document`, `get_document_overview`, `view_pages` | All | None |
|
|
99
|
+
| The part reads of `analyze_whole_document` | All | None |
|
|
100
|
+
| The answer | | One request, without tools |
|
|
101
|
+
| A question about files that are sent whole, with no pages to look at | Not called | One request, as before |
|
|
102
|
+
|
|
103
|
+
The answering model gets the conversation, the explorer's tool calls, and what each call
|
|
104
|
+
returned, pages included. It does not get the explorer's thoughts, its thought signatures, or
|
|
105
|
+
anything it wrote: the answer is written from what the files say, not from what the explorer made
|
|
106
|
+
of them. So the answering model can use only what the explorer fetched. It has no tools of its own
|
|
107
|
+
to fetch more.
|
|
108
|
+
|
|
109
|
+
The explorer is told its part, in a line added to its system instruction (`EXPLORER_INSTRUCTION`):
|
|
110
|
+
gather what the answer needs, then reply `DONE`. `gemini-3.5-flash-lite` did exactly that, at one
|
|
111
|
+
output token. A model that writes an answer anyway (`gemini-2.5-flash-lite` did) costs those output
|
|
112
|
+
tokens, and its answer is dropped.
|
|
113
|
+
|
|
114
|
+
### What changes for your application
|
|
115
|
+
|
|
116
|
+
- **One request more.** The answer is always a request of its own, so a question that one model
|
|
117
|
+
settles in three requests takes four. `maxToolTurns` still counts them all: the explorer has
|
|
118
|
+
`maxToolTurns − 1` requests, and the last one is the answer.
|
|
119
|
+
- **`text` and `onText` carry only the answer.** Without an explorer, a remark that the model makes
|
|
120
|
+
before a tool call ("Let me search…") is part of `text`. The explorer's words are not shown.
|
|
121
|
+
`onActivity` and `onStep` report its tool calls as before, and `turns` counts its requests too.
|
|
122
|
+
`usage` and `finishReason` are those of the answer.
|
|
123
|
+
- **The explorer's requests are not streamed.** Nothing it writes is shown, so each goes to
|
|
124
|
+
`explorerUrl` in one piece. The answer streams as before.
|
|
125
|
+
- **Settings are per model.** `explorerGenerationConfig` is the explorer's `generationConfig`, and
|
|
126
|
+
defaults to yours. Set it when the two models do not take the same settings: given a
|
|
127
|
+
`thinkingLevel`, `gemini-2.5-flash-lite` on Vertex AI answers HTTP 400, "thinking_level is not
|
|
128
|
+
supported by this model". `analysisConfig` is for the part reads, which the explorer does, so
|
|
129
|
+
write it for the explorer.
|
|
130
|
+
- **The budget has to fit both models.** Both get the same conversation and the same tool
|
|
131
|
+
results. Set `contextTokens` and `maxRequestBytes` to the smaller of the two.
|
|
132
|
+
- **Pages have to be readable by both models.** `toolFilePlacement: "inside"` is read by Gemini 3
|
|
133
|
+
models only. If either model is Gemini 2.5, use `"beside"`, which both read
|
|
134
|
+
([below](#where-the-pages-go-toolfileplacement)).
|
|
135
|
+
- **An explorer that fails is an error.** A model name that does not exist, or a quota that is
|
|
136
|
+
used up, throws `GeminiRequestError`, as it does for `model`. The question is not passed to
|
|
137
|
+
`model` instead: that would hide the mistake, and send the searching to the model you wanted to
|
|
138
|
+
spare.
|
|
139
|
+
- **A PDF whose text is in the prompt**, and whose pages can be looked at, goes to the explorer
|
|
140
|
+
first: it decides whether to look. The text of such a PDF is then sent twice, once to each model.
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
await askGemini({
|
|
144
|
+
files, endpoint, messages, documentIds,
|
|
145
|
+
generationConfig: { thinkingConfig: { thinkingLevel: "high" } }, // for `model`
|
|
146
|
+
explorerGenerationConfig: { thinkingConfig: { thinkingBudget: 0 } }, // for a Gemini 2.5 explorer
|
|
147
|
+
toolFilePlacement: "beside", // one of the two is Gemini 2.5
|
|
148
|
+
});
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Live results with an explorer
|
|
152
|
+
|
|
153
|
+
Flash-Lite models, November 2026, through `ingest` and `askGemini`. The log has 6,000 lines, 24
|
|
154
|
+
deadlocks and one fatal error. The PDF is the one with a chart and a photo from
|
|
155
|
+
[Pictures in PDFs](#live-results).
|
|
156
|
+
|
|
157
|
+
| Explorer → `model` | Question | Result |
|
|
158
|
+
|---|---|---|
|
|
159
|
+
| `gemini-3.5-flash-lite` → `gemini-3.1-flash-lite`, Gemini API | How many deadlocks, and the line of the first | Two searches, then `DONE`. Answer right. Four requests, 3 s. |
|
|
160
|
+
| | What shut the gateway down, its error code, the warning before it | One search, then `DONE`. Four of four facts, with the right lines. |
|
|
161
|
+
| | The name of the file and its line count, which the card shows | `DONE` at once. Answered from the card, in two requests. |
|
|
162
|
+
| | Every kind of error, from a whole-file read | The explorer read the four parts and wrote up the notes, then searched three times. Answer right. |
|
|
163
|
+
| | The chart and the photo, with the text sent whole | The explorer looked at two pages. The other model got them and had five of five facts right. |
|
|
164
|
+
| | The same, with the PDF searched | A read and a look in one turn. Five of five. |
|
|
165
|
+
| | The gateway and the chart again, both models thinking (`thinkingLevel: "high"`) | Both right. |
|
|
166
|
+
| `gemini-3.1-flash-lite` → `gemini-3.5-flash-lite`, Gemini API | The deadlocks | Answer right. The explorer repeated one search until its eleven requests were used up, as it does when it works alone. |
|
|
167
|
+
| `gemini-2.5-flash-lite` → `gemini-3.5-flash-lite`, Vertex AI | The deadlocks, the gateway, and the chart with pages `"beside"` | All right. This explorer wrote an answer each time instead of `DONE`. |
|
|
168
|
+
| `gemini-3.5-flash-lite` → `gemini-2.5-flash-lite`, Vertex AI | The deadlocks, the chart with pages `"beside"`, and the card | All right. |
|
|
169
|
+
| `gemini-3.1-flash-lite` → `gemini-3.5-flash-lite`, Vertex AI, both thinking | The gateway | Right. |
|
|
170
|
+
|
|
171
|
+
What made the form of the handoff: a Gemini 3 model refuses a request that ends with the result of
|
|
172
|
+
a function call it did not sign (HTTP 400, "Function call is missing a thought_signature"), and a
|
|
173
|
+
thought signature belongs to the model that made the call. When a user message follows the
|
|
174
|
+
results, the same calls are accepted without a signature: by Gemini 3.1 and 3.5 Flash-Lite on the
|
|
175
|
+
Gemini API and on Vertex AI, and by Gemini 2.5 Flash-Lite on Vertex AI. So the answering model's
|
|
176
|
+
request always ends with a user message, and holds no signatures.
|
|
177
|
+
|
|
178
|
+
Not checked: models larger than Flash-Lite, and Gemini 4. The two models have to be reachable with
|
|
179
|
+
the same headers, so one cannot be on the Gemini API and the other on Vertex AI.
|
|
180
|
+
|
|
59
181
|
## Stores
|
|
60
182
|
|
|
61
183
|
| Store | Use it for |
|
package/docs/getting-started.md
CHANGED
|
@@ -170,6 +170,22 @@ Without `streamUrl`, answers arrive in one piece (and `onText` fires once).
|
|
|
170
170
|
**Existing Gemini history.** If your chat already stores Gemini `contents` (with images or inline
|
|
171
171
|
PDFs), pass `contents` instead of `messages`; document text is added at the start of the first user turn.
|
|
172
172
|
|
|
173
|
+
**A second model for the searching (optional).** The model you name does both halves of the work:
|
|
174
|
+
it searches and reads a large file, and it answers. `explorerModel` gives the first half to another
|
|
175
|
+
model, for example a cheaper one, and `model` then only writes the answer:
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
const endpoint = geminiApi({
|
|
179
|
+
apiKey: process.env.GEMINI_API_KEY!,
|
|
180
|
+
model: "gemini-3.8-flash",
|
|
181
|
+
explorerModel: process.env.GEMINI_EXPLORER_MODEL, // unset or empty: one model does both
|
|
182
|
+
});
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
`vertexAi()` takes it the same way, and an endpoint of your own takes the explorer's URL as
|
|
186
|
+
`explorerUrl`. What each model gets, and what it costs, is in
|
|
187
|
+
[An explorer model](configuration.md#an-explorer-model).
|
|
188
|
+
|
|
173
189
|
### Pictures, charts and scans in PDFs
|
|
174
190
|
|
|
175
191
|
The text of a PDF does not hold its pictures. To let the model look at a page when the text is not
|
package/docs/how-it-works.md
CHANGED
|
@@ -130,6 +130,27 @@ Jobs needing more than `maxMapChunks` parts are refused, with advice to narrow t
|
|
|
130
130
|
- Tool results, pages included, live inside one call of `askGemini`. They are not sent again with
|
|
131
131
|
the next question.
|
|
132
132
|
|
|
133
|
+
### With an explorer model
|
|
134
|
+
|
|
135
|
+
One model does all of the above unless the endpoint names an explorer
|
|
136
|
+
([configuration](configuration.md#an-explorer-model)). Then the loop is split in two:
|
|
137
|
+
|
|
138
|
+
- **The requests with tools go to the explorer.** It gets the same conversation, the same tool
|
|
139
|
+
guide and a line that says what its part is: gather what the answer needs, then reply `DONE`.
|
|
140
|
+
Its turns go back to it unchanged, thought signatures included. Reading a whole file in parts is
|
|
141
|
+
its work too.
|
|
142
|
+
- **The request without tools goes to the answering model**, and with an explorer it is always
|
|
143
|
+
made. It holds the conversation, the explorer's function calls, and the function responses, pages
|
|
144
|
+
included. It does not hold the explorer's thoughts, its thought signatures, or anything it
|
|
145
|
+
wrote: the answer is written from what the files say.
|
|
146
|
+
- **That request ends with a user message.** A Gemini 3 model refuses a request that ends with
|
|
147
|
+
the result of a function call it did not sign (HTTP 400, "Function call is missing a
|
|
148
|
+
thought_signature"), and a signature belongs to the model that made the call. With a user message
|
|
149
|
+
after the results, the calls belong to an earlier turn, and the request is accepted without
|
|
150
|
+
signatures. The message is the "answer now" one from above.
|
|
151
|
+
- An explorer that calls no tool leaves nothing to answer from but the conversation, so the
|
|
152
|
+
message then says that instead (`FINAL_ANSWER_WITHOUT_TOOLS`).
|
|
153
|
+
|
|
133
154
|
## Live results
|
|
134
155
|
|
|
135
156
|
`gemini-3.8-flash` on the Gemini API, November 2026:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "llm-large-files",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.5",
|
|
4
4
|
"description": "Let an LLM answer questions about books, logs, and other files larger than its request limit or context window: upload, plan, search and read tools, map-reduce, and a look at the pages of a PDF. Gemini and Vertex AI helpers included.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"llm",
|
package/src/gemini/chat.ts
CHANGED
|
@@ -16,6 +16,13 @@ export type ChatTurn = { role: "user" | "model" | "assistant"; text: string };
|
|
|
16
16
|
export 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
17
|
/** The same, when the only tool was `view_pages`: the text of the files is in the prompt, not in tool results. */
|
|
18
18
|
export 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 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 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.";
|
|
19
26
|
|
|
20
27
|
/** What Gemini bills for one PDF page it is shown (measured: 520 to 532). */
|
|
21
28
|
const PAGE_TOKENS = 530;
|
|
@@ -38,6 +45,12 @@ export class GeminiRequestError extends Error {
|
|
|
38
45
|
|
|
39
46
|
export type AskGeminiOptions = {
|
|
40
47
|
files: LargeFiles;
|
|
48
|
+
/**
|
|
49
|
+
* Where the model is: the one that answers. When the endpoint also names an explorer model
|
|
50
|
+
* (`explorerModel` in `geminiApi()` and `vertexAi()`, or `explorerUrl`), that model makes the
|
|
51
|
+
* tool calls and reads a whole file in parts, and this one writes the answer from what the
|
|
52
|
+
* tools returned. Without an explorer, one model does both.
|
|
53
|
+
*/
|
|
41
54
|
endpoint: GeminiEndpoint;
|
|
42
55
|
/** Documents this conversation may use. Where their text or cards go: see `documentPlacement`. */
|
|
43
56
|
documentIds: string[];
|
|
@@ -55,12 +68,19 @@ export type AskGeminiOptions = {
|
|
|
55
68
|
system?: string;
|
|
56
69
|
/** Sent with every chat request: temperature, maxOutputTokens, thinkingConfig, and so on. */
|
|
57
70
|
generationConfig?: Record<string, unknown>;
|
|
71
|
+
/**
|
|
72
|
+
* The same, for the requests of an explorer model. Defaults to `generationConfig`. Set it when
|
|
73
|
+
* the two models do not take the same settings: a thinking level that one of them refuses, for
|
|
74
|
+
* example. It is not used without an explorer model.
|
|
75
|
+
*/
|
|
76
|
+
explorerGenerationConfig?: Record<string, unknown>;
|
|
58
77
|
/** The user asking. Documents ingested with a different owner are treated as missing. */
|
|
59
78
|
owner?: string;
|
|
60
79
|
/**
|
|
61
80
|
* The most model requests for one question, counting the answer. Tools can run in all but the
|
|
62
81
|
* last: if the model is still calling tools then, the last request is sent with no tools so it
|
|
63
|
-
* must answer. Defaults to 12.
|
|
82
|
+
* must answer. Defaults to 12. With an explorer model the answer is always a request of its
|
|
83
|
+
* own, the last one, so the explorer has one less than this.
|
|
64
84
|
*/
|
|
65
85
|
maxToolTurns?: number;
|
|
66
86
|
/** Offer `analyze_whole_document`, which reads a whole file in many model calls. Defaults to true. */
|
|
@@ -78,7 +98,10 @@ export type AskGeminiOptions = {
|
|
|
78
98
|
* function response.
|
|
79
99
|
*/
|
|
80
100
|
toolFilePlacement?: GeminiToolFilePlacement;
|
|
81
|
-
/**
|
|
101
|
+
/**
|
|
102
|
+
* generationConfig for whole-document part reads. Defaults to `{ temperature: 0.2, maxOutputTokens }`.
|
|
103
|
+
* The parts are read by the explorer model when there is one, so write it for that model.
|
|
104
|
+
*/
|
|
82
105
|
analysisConfig?: (maxOutputTokens: number) => Record<string, unknown>;
|
|
83
106
|
/** Answer text as it streams. Streaming is used when this is set and the endpoint has `streamUrl`. */
|
|
84
107
|
onText?: (delta: string) => void;
|
|
@@ -91,7 +114,10 @@ export type AskGeminiOptions = {
|
|
|
91
114
|
};
|
|
92
115
|
|
|
93
116
|
export type AskGeminiResult = {
|
|
94
|
-
/**
|
|
117
|
+
/**
|
|
118
|
+
* Everything the model wrote, including any remark it made before calling a tool. With an
|
|
119
|
+
* explorer model, only what the answering model wrote: the explorer's words are not shown.
|
|
120
|
+
*/
|
|
95
121
|
text: string;
|
|
96
122
|
/**
|
|
97
123
|
* False when the model's last turn had no text: the prompt was blocked, the reply was cut off
|
|
@@ -105,7 +131,7 @@ export type AskGeminiResult = {
|
|
|
105
131
|
steps: string[];
|
|
106
132
|
/** Which documents went in whole and which were searched. */
|
|
107
133
|
plan: DocumentPlan;
|
|
108
|
-
/** Model requests made for the chat itself (whole-document part reads not included). */
|
|
134
|
+
/** Model requests made for the chat itself, an explorer model's among them (whole-document part reads not included). */
|
|
109
135
|
turns: number;
|
|
110
136
|
finishReason?: string;
|
|
111
137
|
/** Usage metadata of the last model response. */
|
|
@@ -154,14 +180,28 @@ function baseSize(contents: GeminiContent[], system: string | undefined): { toke
|
|
|
154
180
|
return { tokens, bytes };
|
|
155
181
|
}
|
|
156
182
|
|
|
183
|
+
/**
|
|
184
|
+
* A turn of an explorer model as the answering model gets it: the tool calls alone. A thought
|
|
185
|
+
* signature belongs to the model that wrote it, and the explorer's thoughts and remarks are not
|
|
186
|
+
* what the files say.
|
|
187
|
+
*/
|
|
188
|
+
function callsOnly(content: GeminiContent): GeminiContent {
|
|
189
|
+
if (content.role !== "model") return content;
|
|
190
|
+
return { role: "model", parts: content.parts.flatMap((part) => (part.functionCall ? [{ functionCall: part.functionCall }] : [])) };
|
|
191
|
+
}
|
|
192
|
+
|
|
157
193
|
/**
|
|
158
194
|
* Answers a chat turn about large documents with Gemini (Generate Content REST).
|
|
159
195
|
* Documents that fit go into the prompt whole; otherwise Gemini gets search/read tools and this
|
|
160
196
|
* runs the tool loop, ending with a tool-free turn so there is always an answer.
|
|
197
|
+
* When the endpoint names an explorer model, that model runs the tool loop and the endpoint's own
|
|
198
|
+
* model writes the answer, in the tool-free turn.
|
|
161
199
|
*/
|
|
162
200
|
export async function askGemini(options: AskGeminiOptions): Promise<AskGeminiResult> {
|
|
163
201
|
const fetchImpl = options.fetch ?? fetch;
|
|
164
202
|
const { files, endpoint, signal } = options;
|
|
203
|
+
// A second model for the requests that use tools. The answering model's own URL is not one.
|
|
204
|
+
const explorerUrl = endpoint.explorerUrl && endpoint.explorerUrl !== endpoint.url ? endpoint.explorerUrl : undefined;
|
|
165
205
|
const contents = toContents(options);
|
|
166
206
|
const prepared = await files.prepare(options.documentIds, baseSize(contents, options.system), { owner: options.owner, viewPages: options.viewPages !== false });
|
|
167
207
|
if (prepared.missing.length) throw new DocumentsNotFoundError(prepared.missing);
|
|
@@ -196,13 +236,16 @@ export async function askGemini(options: AskGeminiOptions): Promise<AskGeminiRes
|
|
|
196
236
|
/**
|
|
197
237
|
* One model request. Text is emitted as it arrives; parts are kept as received.
|
|
198
238
|
* `wroteText` says whether this request produced any text of its own.
|
|
239
|
+
* Given `exploring`, the request is the explorer model's: that body goes to its URL, in one
|
|
240
|
+
* piece, and what it writes is not emitted, because the answer comes from the other model.
|
|
199
241
|
*/
|
|
200
|
-
const turn = async (): Promise<{ parts: GeminiPart[]; calls: GeminiFunctionCall[]; wroteText: boolean }> => {
|
|
242
|
+
const turn = async (exploring?: Body): Promise<{ parts: GeminiPart[]; calls: GeminiFunctionCall[]; wroteText: boolean }> => {
|
|
201
243
|
turns++;
|
|
202
|
-
const
|
|
244
|
+
const stream = streaming && !exploring;
|
|
245
|
+
const response = await fetchImpl(exploring ? explorerUrl! : stream ? endpoint.streamUrl! : endpoint.url, {
|
|
203
246
|
method: "POST",
|
|
204
247
|
headers,
|
|
205
|
-
body: JSON.stringify(body),
|
|
248
|
+
body: JSON.stringify(exploring ?? body),
|
|
206
249
|
signal,
|
|
207
250
|
});
|
|
208
251
|
if (!response.ok) {
|
|
@@ -221,11 +264,13 @@ export async function askGemini(options: AskGeminiOptions): Promise<AskGeminiRes
|
|
|
221
264
|
// Keep every part unchanged: Gemini 3 requires its thought signatures back.
|
|
222
265
|
parts.push(part);
|
|
223
266
|
if (typeof part.text === "string" && part.text && part.thought !== true) {
|
|
224
|
-
if (!
|
|
225
|
-
|
|
226
|
-
|
|
267
|
+
if (!exploring) {
|
|
268
|
+
if (!separated) {
|
|
269
|
+
emit("\n\n");
|
|
270
|
+
separated = true;
|
|
271
|
+
}
|
|
272
|
+
emit(part.text);
|
|
227
273
|
}
|
|
228
|
-
emit(part.text);
|
|
229
274
|
if (part.text.trim()) wroteText = true;
|
|
230
275
|
}
|
|
231
276
|
const call = part.functionCall as { name?: unknown; args?: unknown; id?: unknown } | undefined;
|
|
@@ -237,7 +282,7 @@ export async function askGemini(options: AskGeminiOptions): Promise<AskGeminiRes
|
|
|
237
282
|
finishReason = candidate?.finishReason ?? finishReason;
|
|
238
283
|
usage = chunk.usageMetadata ?? usage;
|
|
239
284
|
};
|
|
240
|
-
if (
|
|
285
|
+
if (stream) {
|
|
241
286
|
if (!response.body) throw new GeminiRequestError("Gemini returned an empty stream.", response.status);
|
|
242
287
|
for await (const chunk of parseSseJson<Chunk>(response.body)) take(chunk);
|
|
243
288
|
} else {
|
|
@@ -265,14 +310,27 @@ export async function askGemini(options: AskGeminiOptions): Promise<AskGeminiRes
|
|
|
265
310
|
|
|
266
311
|
const tools = files.tools(searchable, {
|
|
267
312
|
...(options.wholeDocumentAnalysis === false ? {} : {
|
|
268
|
-
|
|
313
|
+
// Reading a whole file in parts is exploring too: the explorer model's work, when there is one.
|
|
314
|
+
generate: createGeminiGenerateText({ url: explorerUrl ?? endpoint.url, headers, fetchImpl, ...(options.analysisConfig ? { generationConfig: options.analysisConfig } : {}) }),
|
|
269
315
|
}),
|
|
270
316
|
onProgress: (event) => options.onActivity?.(`${event.message}…`),
|
|
271
317
|
...(signal ? { signal } : {}),
|
|
272
318
|
...(viewable.length > 0 ? { viewPages: viewable } : {}),
|
|
273
319
|
});
|
|
274
|
-
|
|
275
|
-
|
|
320
|
+
// The request that uses the tools is the chat's own, or one for the explorer model: the same
|
|
321
|
+
// conversation (`contents` is one array, so it grows in both), that model's settings, and a
|
|
322
|
+
// line that says what its part is.
|
|
323
|
+
const explorerConfig = options.explorerGenerationConfig ?? options.generationConfig;
|
|
324
|
+
const exploring: Body | undefined = explorerUrl ? {
|
|
325
|
+
contents,
|
|
326
|
+
systemInstruction: { parts: [{ text: `${system}\n\n${EXPLORER_INSTRUCTION}` }] },
|
|
327
|
+
...(explorerConfig ? { generationConfig: explorerConfig } : {}),
|
|
328
|
+
} : undefined;
|
|
329
|
+
const withTools = exploring ?? body;
|
|
330
|
+
withTools.tools = [{ functionDeclarations: tools.declarations }];
|
|
331
|
+
withTools.toolConfig = { functionCallingConfig: { mode: "AUTO" } };
|
|
332
|
+
// How many turns the conversation had as it was asked. What follows them is the tool loop's.
|
|
333
|
+
const asked = contents.length;
|
|
276
334
|
if (searchable.length > 0) options.onActivity?.(`Too large to read at once: ${searchable.map((meta) => meta.name).join(", ")}. Searching instead…`);
|
|
277
335
|
|
|
278
336
|
// Pages the model looks at stay in the request until the question is answered, so they are
|
|
@@ -287,10 +345,11 @@ export async function askGemini(options: AskGeminiOptions): Promise<AskGeminiRes
|
|
|
287
345
|
// Tools run in at most maxTurns - 1 requests. The last request is the tool-free one below, so a
|
|
288
346
|
// model that never stops calling tools does not cost a request whose calls are thrown away.
|
|
289
347
|
for (let round = 1; round < maxTurns; round++) {
|
|
290
|
-
const { parts, calls, wroteText } = await turn();
|
|
348
|
+
const { parts, calls, wroteText } = await turn(exploring);
|
|
291
349
|
if (calls.length === 0) {
|
|
292
350
|
// Text from this turn is the answer. A remark made earlier, before a tool call, is not.
|
|
293
|
-
|
|
351
|
+
// Neither is anything an explorer model writes: the answer is asked for below.
|
|
352
|
+
answered = wroteText && !exploring;
|
|
294
353
|
break;
|
|
295
354
|
}
|
|
296
355
|
body.contents.push({ role: "model", parts });
|
|
@@ -302,7 +361,7 @@ export async function askGemini(options: AskGeminiOptions): Promise<AskGeminiRes
|
|
|
302
361
|
options.onActivity?.(`${label}…`);
|
|
303
362
|
let limits: ToolFileLimits | undefined;
|
|
304
363
|
if (call.name === TOOL_NAMES.view) {
|
|
305
|
-
requestBytes ??= Buffer.byteLength(JSON.stringify(
|
|
364
|
+
requestBytes ??= Buffer.byteLength(JSON.stringify(withTools));
|
|
306
365
|
// Base64 turns three bytes into four characters.
|
|
307
366
|
limits = { maxFileBytes: Math.floor((maxRequestBytes - bytesKeptFree - requestBytes) * 3 / 4), maxPages: pagesLeft };
|
|
308
367
|
}
|
|
@@ -320,11 +379,20 @@ export async function askGemini(options: AskGeminiOptions): Promise<AskGeminiRes
|
|
|
320
379
|
// Out of tool turns, or the last turn had no text (cut off, or a malformed tool call): ask once
|
|
321
380
|
// more with no tools declared at all. (Gemini can ignore functionCallingConfig mode NONE and
|
|
322
381
|
// keep calling tools.) A blocked prompt would only be blocked again.
|
|
382
|
+
// With an explorer model this request is always made: it is the one the answering model gets.
|
|
323
383
|
if (!answered && !blockReason) {
|
|
324
384
|
options.onActivity?.("Writing the answer…");
|
|
325
385
|
delete body.tools;
|
|
326
386
|
delete body.toolConfig;
|
|
327
|
-
|
|
387
|
+
let request = searchable.length > 0 ? FINAL_ANSWER_REQUEST : FINAL_ANSWER_AFTER_PAGES;
|
|
388
|
+
if (exploring) {
|
|
389
|
+
// The answering model gets the explorer's calls and what they returned, and nothing else of
|
|
390
|
+
// it. This request ends with a user message, as it must: a Gemini 3 model refuses a
|
|
391
|
+
// function call without its own thought signature in the turn it is still answering.
|
|
392
|
+
body.contents = [...contents.slice(0, asked), ...contents.slice(asked).map(callsOnly)];
|
|
393
|
+
if (steps.length === 0) request = FINAL_ANSWER_WITHOUT_TOOLS;
|
|
394
|
+
}
|
|
395
|
+
body.contents.push({ role: "user", parts: [{ text: request }] });
|
|
328
396
|
answered = (await turn()).wroteText;
|
|
329
397
|
}
|
|
330
398
|
return result(answered);
|
package/src/gemini/endpoints.ts
CHANGED
|
@@ -9,22 +9,48 @@ export type GeminiEndpoint = {
|
|
|
9
9
|
streamUrl?: string;
|
|
10
10
|
/** Request headers, or a function returning them (for short-lived access tokens). */
|
|
11
11
|
headers: Record<string, string> | (() => Record<string, string> | Promise<Record<string, string>>);
|
|
12
|
+
/**
|
|
13
|
+
* The non-streaming `...:generateContent` URL of a second model, the explorer. `askGemini` then
|
|
14
|
+
* sends it the requests that search, read and look at pages, and the model of `url` only writes
|
|
15
|
+
* the answer. It is called with the same headers. `geminiApi()` and `vertexAi()` set it from
|
|
16
|
+
* `explorerModel`. Unset, or the same as `url`, one model does both.
|
|
17
|
+
*/
|
|
18
|
+
explorerUrl?: string | undefined;
|
|
12
19
|
};
|
|
13
20
|
|
|
21
|
+
/**
|
|
22
|
+
* The model that explores the documents, when one is named and it is not the answering model
|
|
23
|
+
* itself. An empty name is no name, so a setting that was left empty can be passed as it is.
|
|
24
|
+
*/
|
|
25
|
+
function explorerOf(options: { model: string; explorerModel?: string | undefined }): string | undefined {
|
|
26
|
+
const name = options.explorerModel?.trim();
|
|
27
|
+
return name && name !== options.model ? name : undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
14
30
|
/** The Gemini Developer API with an API key. */
|
|
15
31
|
export function geminiApi(options: {
|
|
16
32
|
apiKey: string;
|
|
33
|
+
/** The model that answers. Without `explorerModel` it also searches and reads the documents. */
|
|
17
34
|
model: string;
|
|
35
|
+
/**
|
|
36
|
+
* A second model that explores the documents: it makes the tool calls (search, read, look at
|
|
37
|
+
* pages) and reads a whole file in parts, and `model` then writes the answer from what it found.
|
|
38
|
+
* Leave it out or empty to have `model` do both.
|
|
39
|
+
*/
|
|
40
|
+
explorerModel?: string | undefined;
|
|
18
41
|
/** Defaults to "v1beta". */
|
|
19
42
|
apiVersion?: string;
|
|
20
43
|
/** Defaults to https://generativelanguage.googleapis.com. */
|
|
21
44
|
baseUrl?: string;
|
|
22
45
|
}): GeminiEndpoint {
|
|
23
|
-
const
|
|
46
|
+
const models = `${(options.baseUrl ?? "https://generativelanguage.googleapis.com").replace(/\/+$/, "")}/${options.apiVersion ?? "v1beta"}/models/`;
|
|
47
|
+
const base = `${models}${encodeURIComponent(options.model)}`;
|
|
48
|
+
const explorer = explorerOf(options);
|
|
24
49
|
return {
|
|
25
50
|
url: `${base}:generateContent`,
|
|
26
51
|
streamUrl: `${base}:streamGenerateContent?alt=sse`,
|
|
27
52
|
headers: { "Content-Type": "application/json", "x-goog-api-key": options.apiKey },
|
|
53
|
+
...(explorer ? { explorerUrl: `${models}${encodeURIComponent(explorer)}:generateContent` } : {}),
|
|
28
54
|
};
|
|
29
55
|
}
|
|
30
56
|
|
|
@@ -38,7 +64,10 @@ export function vertexHost(location: string): string {
|
|
|
38
64
|
/** Vertex AI with an OAuth access token (for example from google-auth-library). */
|
|
39
65
|
export function vertexAi(options: {
|
|
40
66
|
project: string;
|
|
67
|
+
/** The model that answers. Without `explorerModel` it also searches and reads the documents. */
|
|
41
68
|
model: string;
|
|
69
|
+
/** A second model that explores the documents, as in `geminiApi()`. Leave it out or empty for one model. */
|
|
70
|
+
explorerModel?: string | undefined;
|
|
42
71
|
/** Defaults to "global". */
|
|
43
72
|
location?: string;
|
|
44
73
|
/** A token, or a function that returns a fresh one for each request. */
|
|
@@ -47,7 +76,9 @@ export function vertexAi(options: {
|
|
|
47
76
|
apiVersion?: string;
|
|
48
77
|
}): GeminiEndpoint {
|
|
49
78
|
const location = options.location ?? "global";
|
|
50
|
-
const
|
|
79
|
+
const models = `${vertexHost(location)}/${options.apiVersion ?? "v1"}/projects/${encodeURIComponent(options.project)}/locations/${location}/publishers/google/models/`;
|
|
80
|
+
const base = `${models}${encodeURIComponent(options.model)}`;
|
|
81
|
+
const explorer = explorerOf(options);
|
|
51
82
|
const token = options.accessToken;
|
|
52
83
|
return {
|
|
53
84
|
url: `${base}:generateContent`,
|
|
@@ -56,6 +87,7 @@ export function vertexAi(options: {
|
|
|
56
87
|
"Content-Type": "application/json",
|
|
57
88
|
Authorization: `Bearer ${typeof token === "function" ? await token() : token}`,
|
|
58
89
|
}),
|
|
90
|
+
...(explorer ? { explorerUrl: `${models}${encodeURIComponent(explorer)}:generateContent` } : {}),
|
|
59
91
|
};
|
|
60
92
|
}
|
|
61
93
|
|
package/src/gemini/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { askGemini, DocumentsNotFoundError, FINAL_ANSWER_AFTER_PAGES, FINAL_ANSWER_REQUEST, GeminiRequestError } from "./chat.js";
|
|
1
|
+
export { askGemini, DocumentsNotFoundError, EXPLORER_INSTRUCTION, FINAL_ANSWER_AFTER_PAGES, FINAL_ANSWER_REQUEST, FINAL_ANSWER_WITHOUT_TOOLS, GeminiRequestError } from "./chat.js";
|
|
2
2
|
export type { AskGeminiOptions, AskGeminiResult, ChatTurn, GeminiContent, GeminiPart } from "./chat.js";
|
|
3
3
|
export { geminiApi, vertexAi, vertexHost } from "./endpoints.js";
|
|
4
4
|
export type { GeminiEndpoint } from "./endpoints.js";
|