@mizchi/vlmkit-ai 0.22.0 → 0.23.1
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/dist/image-gen-client.d.mts +41 -3
- package/dist/image-gen-client.mjs +63 -15
- package/dist/index.d.mts +6 -2
- package/dist/index.mjs +6 -2
- package/dist/llm-client.d.mts +4 -4
- package/dist/llm-client.mjs +4 -4
- package/dist/zoom-drivers.d.mts +33 -0
- package/dist/zoom-drivers.mjs +316 -0
- package/dist/zoom-geometry.d.mts +85 -0
- package/dist/zoom-geometry.mjs +103 -0
- package/dist/zoom-image.d.mts +42 -0
- package/dist/zoom-image.mjs +147 -0
- package/dist/zoom-loop.d.mts +129 -0
- package/dist/zoom-loop.mjs +314 -0
- package/dist/zoom.d.mts +29 -0
- package/dist/zoom.mjs +61 -0
- package/package.json +3 -2
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import { DEFAULT_IMAGE_BUDGET, toViewBox } from "./zoom-geometry.mjs";
|
|
2
|
+
import { prepareZoomSource, zoomInto } from "./zoom-image.mjs";
|
|
3
|
+
//#region src/zoom-loop.ts
|
|
4
|
+
/**
|
|
5
|
+
* The zoom loop, independent of any provider: show the model its images at a size we chose,
|
|
6
|
+
* give it one tool — `zoom(image, x1, y1, x2, y2)` — and answer each call with a magnified crop
|
|
7
|
+
* of the original, until it answers the question or spends its zoom budget.
|
|
8
|
+
*
|
|
9
|
+
* A provider enters only through `VisionChatDriver`: one function that takes the transcript so
|
|
10
|
+
* far and returns the model's next turn. Two ways for the model to ask for a zoom, so any vision
|
|
11
|
+
* model can use it:
|
|
12
|
+
*
|
|
13
|
+
* - `native`: the provider's own function calling (OpenAI-compatible `tools`, Anthropic
|
|
14
|
+
* `tool_use`, Gemini `functionCall`). The driver declares `nativeTools`.
|
|
15
|
+
* - `text`: a protocol in the reply itself — a line `ZOOM <image> <x1> <y1> <x2> <y2>` —
|
|
16
|
+
* for models with no function calling (most small open VLMs). `parseZoomRequests` reads it.
|
|
17
|
+
*
|
|
18
|
+
* How a result carries its image is the driver's business, because providers disagree: some
|
|
19
|
+
* accept an image inside the tool result, OpenAI-compatible APIs only accept text there, so
|
|
20
|
+
* those drivers send the crop as a user image right after it. The loop hands every driver the
|
|
21
|
+
* same `ZoomResult`.
|
|
22
|
+
*/
|
|
23
|
+
function zoomToolSpec(imageCount, coordinates) {
|
|
24
|
+
const unit = coordinates === "pixels" ? "in pixels of the image as you see it (origin top-left, x right, y down)" : "as 0-1000 of the image's width (x) and height (y), origin top-left";
|
|
25
|
+
return {
|
|
26
|
+
name: "zoom",
|
|
27
|
+
description: "Crop a region of an image and see it magnified from the full-resolution original. Use it for any detail too small to read with confidence: small text, thin lines, the exact colour or edge of an element.",
|
|
28
|
+
parameters: {
|
|
29
|
+
type: "object",
|
|
30
|
+
properties: {
|
|
31
|
+
...imageCount > 1 ? { image_index: {
|
|
32
|
+
type: "integer",
|
|
33
|
+
description: `Which image, counting from 0 (0-${imageCount - 1})`
|
|
34
|
+
} } : {},
|
|
35
|
+
x1: {
|
|
36
|
+
type: "number",
|
|
37
|
+
description: `Left edge, ${unit}`
|
|
38
|
+
},
|
|
39
|
+
y1: {
|
|
40
|
+
type: "number",
|
|
41
|
+
description: "Top edge"
|
|
42
|
+
},
|
|
43
|
+
x2: {
|
|
44
|
+
type: "number",
|
|
45
|
+
description: "Right edge (greater than x1)"
|
|
46
|
+
},
|
|
47
|
+
y2: {
|
|
48
|
+
type: "number",
|
|
49
|
+
description: "Bottom edge (greater than y1)"
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
required: [
|
|
53
|
+
"x1",
|
|
54
|
+
"y1",
|
|
55
|
+
"x2",
|
|
56
|
+
"y2"
|
|
57
|
+
],
|
|
58
|
+
additionalProperties: false
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/** Arguments a native call carried, whatever shape the provider gave them in. */
|
|
63
|
+
function zoomCallFromArgs(id, args) {
|
|
64
|
+
let value = args;
|
|
65
|
+
if (typeof value === "string") try {
|
|
66
|
+
value = JSON.parse(value);
|
|
67
|
+
} catch {
|
|
68
|
+
return {
|
|
69
|
+
id,
|
|
70
|
+
error: "arguments are not valid JSON"
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
const a = value ?? {};
|
|
74
|
+
const num = (k) => typeof a[k] === "number" ? a[k] : typeof a[k] === "string" ? Number(a[k]) : NaN;
|
|
75
|
+
const box = {
|
|
76
|
+
x1: num("x1"),
|
|
77
|
+
y1: num("y1"),
|
|
78
|
+
x2: num("x2"),
|
|
79
|
+
y2: num("y2")
|
|
80
|
+
};
|
|
81
|
+
if (![
|
|
82
|
+
box.x1,
|
|
83
|
+
box.y1,
|
|
84
|
+
box.x2,
|
|
85
|
+
box.y2
|
|
86
|
+
].every(Number.isFinite)) return {
|
|
87
|
+
id,
|
|
88
|
+
error: "x1, y1, x2 and y2 are required numbers"
|
|
89
|
+
};
|
|
90
|
+
const index = a.image_index === void 0 ? 0 : num("image_index");
|
|
91
|
+
return {
|
|
92
|
+
id,
|
|
93
|
+
imageIndex: Number.isFinite(index) ? Math.trunc(index) : NaN,
|
|
94
|
+
box
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
const ZOOM_LINE = /^\s*ZOOM\s+(-?\d+(?:\.\d+)?)\s+(-?\d+(?:\.\d+)?)\s+(-?\d+(?:\.\d+)?)\s+(-?\d+(?:\.\d+)?)\s+(-?\d+(?:\.\d+)?)\s*$/gim;
|
|
98
|
+
/**
|
|
99
|
+
* Zoom requests written in a reply, one `ZOOM <image> <x1> <y1> <x2> <y2>` per line. The
|
|
100
|
+
* image index is always written, even with one image, so the grammar has one shape.
|
|
101
|
+
*/
|
|
102
|
+
function parseZoomRequests(text, turn) {
|
|
103
|
+
const calls = [];
|
|
104
|
+
for (const m of text.matchAll(ZOOM_LINE)) calls.push({
|
|
105
|
+
id: `text-${turn}-${calls.length}`,
|
|
106
|
+
imageIndex: Math.trunc(Number(m[1])),
|
|
107
|
+
box: {
|
|
108
|
+
x1: Number(m[2]),
|
|
109
|
+
y1: Number(m[3]),
|
|
110
|
+
x2: Number(m[4]),
|
|
111
|
+
y2: Number(m[5])
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
return calls;
|
|
115
|
+
}
|
|
116
|
+
function textProtocolInstructions(coordinates) {
|
|
117
|
+
return `You can zoom: to see a region magnified from the full-resolution original, reply with only lines of the form
|
|
118
|
+
ZOOM <image> <x1> <y1> <x2> <y2>
|
|
119
|
+
where <image> counts from 0 and the box is in ${coordinates === "pixels" ? "pixels of the image as you see it" : "0-1000 of the image's width and height"} (origin top-left). You will get the magnified regions back. When you can answer, reply with the answer and no ZOOM line.`;
|
|
120
|
+
}
|
|
121
|
+
/** The opening message: each image labelled with its index and the size it is shown at, then the question. */
|
|
122
|
+
function introParts(images, sources, question, coordinates, instructions) {
|
|
123
|
+
const intro = [];
|
|
124
|
+
sources.forEach((src, i) => {
|
|
125
|
+
const label = images[i].label ? `${images[i].label} — ` : "";
|
|
126
|
+
intro.push({
|
|
127
|
+
type: "text",
|
|
128
|
+
text: `${label}Image ${i} (${src.view.width}x${src.view.height} pixels):`
|
|
129
|
+
});
|
|
130
|
+
intro.push({
|
|
131
|
+
type: "image",
|
|
132
|
+
png: src.viewPng
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
const coordNote = coordinates === "pixels" ? "Coordinates are absolute pixels of the image as shown, origin top-left." : "Coordinates are 0-1000 of each image's width and height, origin top-left.";
|
|
136
|
+
intro.push({
|
|
137
|
+
type: "text",
|
|
138
|
+
text: `${question}\n\n${coordNote}${instructions ? ` ${instructions}` : ""}`
|
|
139
|
+
});
|
|
140
|
+
return intro;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* The control arm of a zoom measurement: the same driver, the same images resized to the same
|
|
144
|
+
* budget, the same labels and question — and one turn with no tool. Comparing this against
|
|
145
|
+
* `runZoomLoop` isolates what zooming adds; comparing against a different client would also
|
|
146
|
+
* measure the difference between two request builders.
|
|
147
|
+
*/
|
|
148
|
+
async function runSingleLook(driver, images, question, options = {}) {
|
|
149
|
+
if (images.length === 0) throw new RangeError("runSingleLook needs at least one image");
|
|
150
|
+
const budget = options.budget ?? DEFAULT_IMAGE_BUDGET;
|
|
151
|
+
const intro = introParts(images, images.map((img) => prepareZoomSource(img.png, budget)), question, options.coordinates ?? "pixels", "");
|
|
152
|
+
const reply = await driver.turn([{
|
|
153
|
+
role: "user",
|
|
154
|
+
parts: intro
|
|
155
|
+
}], { maxTokens: options.maxTokens ?? 2048 });
|
|
156
|
+
return {
|
|
157
|
+
answer: reply.text.trim(),
|
|
158
|
+
usage: {
|
|
159
|
+
promptTokens: reply.usage?.promptTokens ?? 0,
|
|
160
|
+
completionTokens: reply.usage?.completionTokens ?? 0
|
|
161
|
+
},
|
|
162
|
+
...reply.stop ? { stop: reply.stop } : {}
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Run the loop. `images` are PNGs at any size; each is shown at `budget` and labelled with its
|
|
167
|
+
* index and size. The answer is the model's last reply with no zoom request.
|
|
168
|
+
*/
|
|
169
|
+
async function runZoomLoop(driver, images, question, options = {}) {
|
|
170
|
+
if (images.length === 0) throw new RangeError("runZoomLoop needs at least one image");
|
|
171
|
+
const budget = options.budget ?? DEFAULT_IMAGE_BUDGET;
|
|
172
|
+
const coordinates = options.coordinates ?? "pixels";
|
|
173
|
+
const maxZooms = Math.max(0, options.maxZooms ?? 6);
|
|
174
|
+
const maxTokens = options.maxTokens ?? 2048;
|
|
175
|
+
const protocol = options.protocol ?? (driver.nativeTools ? "native" : "text");
|
|
176
|
+
if (protocol === "native" && !driver.nativeTools) throw new Error(`${driver.model}: this driver has no native tools; use protocol "text"`);
|
|
177
|
+
const sources = images.map((img) => prepareZoomSource(img.png, budget));
|
|
178
|
+
const transcript = [{
|
|
179
|
+
role: "user",
|
|
180
|
+
parts: introParts(images, sources, question, coordinates, protocol === "native" ? "Use the zoom tool to examine any detail too small to read confidently." : textProtocolInstructions(coordinates))
|
|
181
|
+
}];
|
|
182
|
+
const tool = zoomToolSpec(images.length, coordinates);
|
|
183
|
+
const usage = {
|
|
184
|
+
promptTokens: 0,
|
|
185
|
+
completionTokens: 0
|
|
186
|
+
};
|
|
187
|
+
const zooms = [];
|
|
188
|
+
const rejected = [];
|
|
189
|
+
let turns = 0;
|
|
190
|
+
let wrappedUp = false;
|
|
191
|
+
for (;;) {
|
|
192
|
+
const offerTool = !wrappedUp;
|
|
193
|
+
const reply = await driver.turn(transcript, {
|
|
194
|
+
...offerTool && protocol === "native" ? { tool } : {},
|
|
195
|
+
maxTokens
|
|
196
|
+
});
|
|
197
|
+
turns++;
|
|
198
|
+
usage.promptTokens += reply.usage?.promptTokens ?? 0;
|
|
199
|
+
usage.completionTokens += reply.usage?.completionTokens ?? 0;
|
|
200
|
+
const calls = protocol === "native" ? reply.calls : parseZoomRequests(reply.text, turns);
|
|
201
|
+
const done = (answer) => ({
|
|
202
|
+
answer,
|
|
203
|
+
zooms,
|
|
204
|
+
rejected,
|
|
205
|
+
turns,
|
|
206
|
+
wrappedUp,
|
|
207
|
+
protocol,
|
|
208
|
+
usage,
|
|
209
|
+
...reply.stop ? { stop: reply.stop } : {}
|
|
210
|
+
});
|
|
211
|
+
if (calls.length === 0 || wrappedUp) return done(protocol === "text" ? reply.text.replace(ZOOM_LINE, "").trim() : reply.text.trim());
|
|
212
|
+
transcript.push({
|
|
213
|
+
role: "assistant",
|
|
214
|
+
text: reply.text,
|
|
215
|
+
calls,
|
|
216
|
+
...reply.raw !== void 0 ? { raw: reply.raw } : {}
|
|
217
|
+
});
|
|
218
|
+
const results = [];
|
|
219
|
+
for (const call of calls) {
|
|
220
|
+
if (zooms.length >= maxZooms) {
|
|
221
|
+
results.push({
|
|
222
|
+
callId: call.id,
|
|
223
|
+
isError: true,
|
|
224
|
+
parts: [{
|
|
225
|
+
type: "text",
|
|
226
|
+
text: "Zoom budget used up; answer from what you have seen."
|
|
227
|
+
}]
|
|
228
|
+
});
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
if (call.error) {
|
|
232
|
+
rejected.push({
|
|
233
|
+
call,
|
|
234
|
+
reason: call.error
|
|
235
|
+
});
|
|
236
|
+
results.push({
|
|
237
|
+
callId: call.id,
|
|
238
|
+
isError: true,
|
|
239
|
+
parts: [{
|
|
240
|
+
type: "text",
|
|
241
|
+
text: `Error: ${call.error}`
|
|
242
|
+
}]
|
|
243
|
+
});
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
const source = sources[call.imageIndex];
|
|
247
|
+
if (!source) {
|
|
248
|
+
const reason = `no image ${call.imageIndex} (there are ${sources.length}, counting from 0)`;
|
|
249
|
+
rejected.push({
|
|
250
|
+
call,
|
|
251
|
+
reason
|
|
252
|
+
});
|
|
253
|
+
results.push({
|
|
254
|
+
callId: call.id,
|
|
255
|
+
isError: true,
|
|
256
|
+
parts: [{
|
|
257
|
+
type: "text",
|
|
258
|
+
text: `Error: ${reason}`
|
|
259
|
+
}]
|
|
260
|
+
});
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
const outcome = zoomInto(source, toViewBox(call.box, source.view, coordinates), budget);
|
|
264
|
+
if (!outcome.ok) {
|
|
265
|
+
rejected.push({
|
|
266
|
+
call,
|
|
267
|
+
reason: outcome.text
|
|
268
|
+
});
|
|
269
|
+
results.push({
|
|
270
|
+
callId: call.id,
|
|
271
|
+
isError: true,
|
|
272
|
+
parts: [{
|
|
273
|
+
type: "text",
|
|
274
|
+
text: outcome.text
|
|
275
|
+
}]
|
|
276
|
+
});
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
zooms.push({
|
|
280
|
+
imageIndex: call.imageIndex,
|
|
281
|
+
viewBox: outcome.viewBox,
|
|
282
|
+
originalBox: outcome.originalBox
|
|
283
|
+
});
|
|
284
|
+
results.push({
|
|
285
|
+
callId: call.id,
|
|
286
|
+
isError: false,
|
|
287
|
+
parts: [{
|
|
288
|
+
type: "text",
|
|
289
|
+
text: outcome.text
|
|
290
|
+
}, {
|
|
291
|
+
type: "image",
|
|
292
|
+
png: outcome.png
|
|
293
|
+
}]
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
transcript.push({
|
|
297
|
+
role: "tool",
|
|
298
|
+
native: protocol === "native",
|
|
299
|
+
results
|
|
300
|
+
});
|
|
301
|
+
if (zooms.length >= maxZooms || turns >= maxZooms + 3) {
|
|
302
|
+
wrappedUp = true;
|
|
303
|
+
transcript.push({
|
|
304
|
+
role: "user",
|
|
305
|
+
parts: [{
|
|
306
|
+
type: "text",
|
|
307
|
+
text: "You have used your zoom budget. Answer the original question now, based on everything you have seen."
|
|
308
|
+
}]
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
//#endregion
|
|
314
|
+
export { parseZoomRequests, runSingleLook, runZoomLoop, zoomCallFromArgs, zoomToolSpec };
|
package/dist/zoom.d.mts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { VlmModel, VlmResponse } from "./vlm-client.mjs";
|
|
2
|
+
import { Box, BoxProblem, DEFAULT_IMAGE_BUDGET, ImageBudget, Size, ZoomCoordinates, clampBox, fitToBudget, toViewBox, viewBoxToOriginal, zoomSize } from "./zoom-geometry.mjs";
|
|
3
|
+
import { DriverTurn, SingleLookResult, VisionChatDriver, ZoomCall, ZoomImageInput, ZoomLoopOptions, ZoomLoopResult, ZoomPart, ZoomResult, ZoomToolSpec, ZoomTurn, ZoomUsage, parseZoomRequests, runSingleLook, runZoomLoop, zoomCallFromArgs, zoomToolSpec } from "./zoom-loop.mjs";
|
|
4
|
+
import { ZoomOutcome, ZoomSource, prepareZoomSource, zoomInto } from "./zoom-image.mjs";
|
|
5
|
+
import { AnthropicDriverOptions, GeminiDriverOptions, OpenAiCompatibleDriverOptions, anthropicDriver, geminiDriver, openAiCompatibleDriver } from "./zoom-drivers.mjs";
|
|
6
|
+
|
|
7
|
+
//#region src/zoom.d.ts
|
|
8
|
+
interface ZoomDriverOptions {
|
|
9
|
+
/** Overrides the environment key lookup, as in `createVlmClient`. */
|
|
10
|
+
apiKey?: string;
|
|
11
|
+
/** OpenRouter only: false for a model without function calling (the loop then uses the text protocol). */
|
|
12
|
+
nativeTools?: boolean;
|
|
13
|
+
}
|
|
14
|
+
/** A driver for a model id, with the same key lookup and error as `createVlmClient`. */
|
|
15
|
+
declare function createZoomDriver(model: VlmModel, options?: ZoomDriverOptions): VisionChatDriver;
|
|
16
|
+
interface ZoomAnalysis extends VlmResponse {
|
|
17
|
+
zoom: Omit<ZoomLoopResult, "answer" | "usage">;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Ask a question about one or more PNG screenshots, letting the model zoom. Same shape as a
|
|
21
|
+
* `VlmClient` response (content, tokens, cost, latency) plus what the model zoomed into, so a
|
|
22
|
+
* bench can put it beside a single-look answer.
|
|
23
|
+
*/
|
|
24
|
+
declare function analyzeWithZoom(model: VlmModel, images: readonly {
|
|
25
|
+
png: Buffer;
|
|
26
|
+
label?: string;
|
|
27
|
+
}[], prompt: string, options?: ZoomLoopOptions & ZoomDriverOptions): Promise<ZoomAnalysis>;
|
|
28
|
+
//#endregion
|
|
29
|
+
export { AnthropicDriverOptions, Box, BoxProblem, DEFAULT_IMAGE_BUDGET, DriverTurn, GeminiDriverOptions, ImageBudget, OpenAiCompatibleDriverOptions, SingleLookResult, Size, VisionChatDriver, ZoomAnalysis, ZoomCall, ZoomCoordinates, ZoomDriverOptions, ZoomImageInput, ZoomLoopOptions, ZoomLoopResult, type ZoomOutcome, ZoomPart, ZoomResult, type ZoomSource, ZoomToolSpec, ZoomTurn, ZoomUsage, analyzeWithZoom, anthropicDriver, clampBox, createZoomDriver, fitToBudget, geminiDriver, openAiCompatibleDriver, parseZoomRequests, prepareZoomSource, runSingleLook, runZoomLoop, toViewBox, viewBoxToOriginal, zoomCallFromArgs, zoomInto, zoomSize, zoomToolSpec };
|
package/dist/zoom.mjs
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { VrtConfigError } from "./errors.mjs";
|
|
2
|
+
import { isClaudeDirectModel, isGeminiDirectModel } from "./vlm-client.mjs";
|
|
3
|
+
import { DEFAULT_IMAGE_BUDGET, clampBox, fitToBudget, toViewBox, viewBoxToOriginal, zoomSize } from "./zoom-geometry.mjs";
|
|
4
|
+
import { prepareZoomSource, zoomInto } from "./zoom-image.mjs";
|
|
5
|
+
import { parseZoomRequests, runSingleLook, runZoomLoop, zoomCallFromArgs, zoomToolSpec } from "./zoom-loop.mjs";
|
|
6
|
+
import { anthropicDriver, geminiDriver, openAiCompatibleDriver } from "./zoom-drivers.mjs";
|
|
7
|
+
//#region src/zoom.ts
|
|
8
|
+
/**
|
|
9
|
+
* Zoom for any VLM this package already knows: the model ids `createVlmClient` takes
|
|
10
|
+
* (`claude:…`, `gemini:…`, an OpenRouter id) resolve to a zoom driver with the same keys, so a
|
|
11
|
+
* caller switches a question from one look to "look, then zoom where it matters" by changing
|
|
12
|
+
* one call.
|
|
13
|
+
*
|
|
14
|
+
* Also usable with a self-hosted OpenAI-compatible server (vLLM, Ollama, LM Studio): build
|
|
15
|
+
* `openAiCompatibleDriver({ url, model })` and hand it to `runZoomLoop` directly.
|
|
16
|
+
*/
|
|
17
|
+
/** A driver for a model id, with the same key lookup and error as `createVlmClient`. */
|
|
18
|
+
function createZoomDriver(model, options = {}) {
|
|
19
|
+
const need = (value, name) => {
|
|
20
|
+
if (!value) throw new VrtConfigError("MISSING_KEY", `${name} is required for ${model.id}`);
|
|
21
|
+
return value;
|
|
22
|
+
};
|
|
23
|
+
if (isGeminiDirectModel(model.id)) return geminiDriver({
|
|
24
|
+
model: model.id.replace("gemini:", ""),
|
|
25
|
+
apiKey: need(options.apiKey ?? process.env.GEMINI_API_KEY ?? process.env.GOOGLE_AI_API_KEY, "GEMINI_API_KEY (or GOOGLE_AI_API_KEY)")
|
|
26
|
+
});
|
|
27
|
+
if (isClaudeDirectModel(model.id)) return anthropicDriver({
|
|
28
|
+
model: model.id.replace("claude:", ""),
|
|
29
|
+
apiKey: need(options.apiKey ?? process.env.ANTHROPIC_API_KEY, "ANTHROPIC_API_KEY")
|
|
30
|
+
});
|
|
31
|
+
return openAiCompatibleDriver({
|
|
32
|
+
model: model.id,
|
|
33
|
+
apiKey: need(options.apiKey ?? process.env.OPENROUTER_API_KEY, "OPENROUTER_API_KEY"),
|
|
34
|
+
headers: {
|
|
35
|
+
"HTTP-Referer": "https://github.com/mizchi/vrt",
|
|
36
|
+
"X-Title": "vrt"
|
|
37
|
+
},
|
|
38
|
+
...options.nativeTools !== void 0 ? { nativeTools: options.nativeTools } : {}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Ask a question about one or more PNG screenshots, letting the model zoom. Same shape as a
|
|
43
|
+
* `VlmClient` response (content, tokens, cost, latency) plus what the model zoomed into, so a
|
|
44
|
+
* bench can put it beside a single-look answer.
|
|
45
|
+
*/
|
|
46
|
+
async function analyzeWithZoom(model, images, prompt, options = {}) {
|
|
47
|
+
const start = Date.now();
|
|
48
|
+
const { answer, usage, ...zoom } = await runZoomLoop(createZoomDriver(model, options), images, prompt, options);
|
|
49
|
+
return {
|
|
50
|
+
content: answer,
|
|
51
|
+
model: model.id,
|
|
52
|
+
promptTokens: usage.promptTokens,
|
|
53
|
+
completionTokens: usage.completionTokens,
|
|
54
|
+
totalTokens: usage.promptTokens + usage.completionTokens,
|
|
55
|
+
costUsd: usage.promptTokens / 1e3 * model.promptCostPer1k + usage.completionTokens / 1e3 * model.completionCostPer1k,
|
|
56
|
+
latencyMs: Date.now() - start,
|
|
57
|
+
zoom
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
export { DEFAULT_IMAGE_BUDGET, analyzeWithZoom, anthropicDriver, clampBox, createZoomDriver, fitToBudget, geminiDriver, openAiCompatibleDriver, parseZoomRequests, prepareZoomSource, runSingleLook, runZoomLoop, toViewBox, viewBoxToOriginal, zoomCallFromArgs, zoomInto, zoomSize, zoomToolSpec };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mizchi/vlmkit-ai",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.23.1",
|
|
4
4
|
"description": "VLM / LLM clients, reasoning pipeline, and NLP helpers used by VRT to interpret diff images and natural-language intent.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "mizchi",
|
|
@@ -37,7 +37,8 @@
|
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@google/generative-ai": "^0.24.1",
|
|
40
|
-
"
|
|
40
|
+
"pngjs": "^7.0.0",
|
|
41
|
+
"@mizchi/vlmkit-core": "0.23.1"
|
|
41
42
|
},
|
|
42
43
|
"scripts": {
|
|
43
44
|
"build": "pnpm --dir ../.. exec tsdown --config tsdown.packages.config.ts --filter @mizchi/vlmkit-ai",
|