@mastra/voyageai 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/README.md +365 -0
- package/dist/contextualized-embedding.d.ts +137 -0
- package/dist/contextualized-embedding.d.ts.map +1 -0
- package/dist/index.cjs +579 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +167 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +552 -0
- package/dist/index.js.map +1 -0
- package/dist/multimodal-embedding.d.ts +92 -0
- package/dist/multimodal-embedding.d.ts.map +1 -0
- package/dist/reranker.d.ts +101 -0
- package/dist/reranker.d.ts.map +1 -0
- package/dist/text-embedding.d.ts +97 -0
- package/dist/text-embedding.d.ts.map +1 -0
- package/dist/types.d.ts +251 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +73 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,579 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var voyageai = require('voyageai');
|
|
6
|
+
|
|
7
|
+
// src/types.ts
|
|
8
|
+
var TEXT_MODEL_INFO = {
|
|
9
|
+
"voyage-4-large": {
|
|
10
|
+
maxInputTokens: 12e4,
|
|
11
|
+
defaultDimension: 1024,
|
|
12
|
+
supportedDimensions: [256, 512, 1024, 2048],
|
|
13
|
+
isMultimodal: false,
|
|
14
|
+
isContextualized: false
|
|
15
|
+
},
|
|
16
|
+
"voyage-4": {
|
|
17
|
+
maxInputTokens: 32e4,
|
|
18
|
+
defaultDimension: 1024,
|
|
19
|
+
supportedDimensions: [256, 512, 1024, 2048],
|
|
20
|
+
isMultimodal: false,
|
|
21
|
+
isContextualized: false
|
|
22
|
+
},
|
|
23
|
+
"voyage-4-lite": {
|
|
24
|
+
maxInputTokens: 1e6,
|
|
25
|
+
defaultDimension: 1024,
|
|
26
|
+
supportedDimensions: [256, 512, 1024, 2048],
|
|
27
|
+
isMultimodal: false,
|
|
28
|
+
isContextualized: false
|
|
29
|
+
},
|
|
30
|
+
"voyage-3-large": {
|
|
31
|
+
maxInputTokens: 12e4,
|
|
32
|
+
defaultDimension: 1024,
|
|
33
|
+
supportedDimensions: [256, 512, 1024, 2048],
|
|
34
|
+
isMultimodal: false,
|
|
35
|
+
isContextualized: false
|
|
36
|
+
},
|
|
37
|
+
"voyage-3.5": {
|
|
38
|
+
maxInputTokens: 32e4,
|
|
39
|
+
defaultDimension: 1024,
|
|
40
|
+
supportedDimensions: [256, 512, 1024, 2048],
|
|
41
|
+
isMultimodal: false,
|
|
42
|
+
isContextualized: false
|
|
43
|
+
},
|
|
44
|
+
"voyage-3.5-lite": {
|
|
45
|
+
maxInputTokens: 1e6,
|
|
46
|
+
defaultDimension: 1024,
|
|
47
|
+
supportedDimensions: [256, 512, 1024, 2048],
|
|
48
|
+
isMultimodal: false,
|
|
49
|
+
isContextualized: false
|
|
50
|
+
},
|
|
51
|
+
"voyage-code-3": {
|
|
52
|
+
maxInputTokens: 32e3,
|
|
53
|
+
defaultDimension: 1024,
|
|
54
|
+
supportedDimensions: [256, 512, 1024, 2048],
|
|
55
|
+
isMultimodal: false,
|
|
56
|
+
isContextualized: false
|
|
57
|
+
},
|
|
58
|
+
"voyage-finance-2": {
|
|
59
|
+
maxInputTokens: 32e3,
|
|
60
|
+
defaultDimension: 1024,
|
|
61
|
+
isMultimodal: false,
|
|
62
|
+
isContextualized: false
|
|
63
|
+
},
|
|
64
|
+
"voyage-law-2": {
|
|
65
|
+
maxInputTokens: 32e3,
|
|
66
|
+
defaultDimension: 1024,
|
|
67
|
+
isMultimodal: false,
|
|
68
|
+
isContextualized: false
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
var MULTIMODAL_MODEL_INFO = {
|
|
72
|
+
"voyage-multimodal-3": {
|
|
73
|
+
maxInputTokens: 32e3,
|
|
74
|
+
defaultDimension: 1024,
|
|
75
|
+
isMultimodal: true,
|
|
76
|
+
isContextualized: false
|
|
77
|
+
},
|
|
78
|
+
"voyage-multimodal-3.5": {
|
|
79
|
+
maxInputTokens: 32e3,
|
|
80
|
+
defaultDimension: 1024,
|
|
81
|
+
isMultimodal: true,
|
|
82
|
+
isContextualized: false
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
var CONTEXTUALIZED_MODEL_INFO = {
|
|
86
|
+
"voyage-context-3": {
|
|
87
|
+
maxInputTokens: 32e3,
|
|
88
|
+
defaultDimension: 1024,
|
|
89
|
+
supportedDimensions: [256, 512, 1024, 2048],
|
|
90
|
+
isMultimodal: false,
|
|
91
|
+
isContextualized: true
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
var RERANKER_MODEL_INFO = {
|
|
95
|
+
"rerank-2.5": {
|
|
96
|
+
contextLength: 32e3,
|
|
97
|
+
description: "Best quality with instruction-following support"
|
|
98
|
+
},
|
|
99
|
+
"rerank-2.5-lite": {
|
|
100
|
+
contextLength: 32e3,
|
|
101
|
+
description: "Optimized for latency and quality"
|
|
102
|
+
},
|
|
103
|
+
"rerank-2": {
|
|
104
|
+
contextLength: 16e3,
|
|
105
|
+
description: "Second-generation with multilingual support"
|
|
106
|
+
},
|
|
107
|
+
"rerank-2-lite": {
|
|
108
|
+
contextLength: 8e3,
|
|
109
|
+
description: "Second-generation, latency-optimized"
|
|
110
|
+
},
|
|
111
|
+
"rerank-1": {
|
|
112
|
+
contextLength: 8e3,
|
|
113
|
+
description: "First-generation, quality-focused"
|
|
114
|
+
},
|
|
115
|
+
"rerank-lite-1": {
|
|
116
|
+
contextLength: 4e3,
|
|
117
|
+
description: "First-generation, latency-optimized"
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
function toSdkInputType(inputType) {
|
|
121
|
+
if (inputType === null) return void 0;
|
|
122
|
+
return inputType;
|
|
123
|
+
}
|
|
124
|
+
async function createTokenAwareBatches(client, model, texts, maxTokens, maxInputsPerBatch) {
|
|
125
|
+
const tokenResults = await client.tokenize(texts, model);
|
|
126
|
+
const batches = [];
|
|
127
|
+
let currentBatch = [];
|
|
128
|
+
let currentTokens = 0;
|
|
129
|
+
for (let i = 0; i < texts.length; i++) {
|
|
130
|
+
const tokenCount = tokenResults[i]?.ids.length ?? 0;
|
|
131
|
+
if (currentBatch.length > 0 && (currentTokens + tokenCount > maxTokens || currentBatch.length >= maxInputsPerBatch)) {
|
|
132
|
+
batches.push(currentBatch);
|
|
133
|
+
currentBatch = [];
|
|
134
|
+
currentTokens = 0;
|
|
135
|
+
}
|
|
136
|
+
currentBatch.push(texts[i]);
|
|
137
|
+
currentTokens += tokenCount;
|
|
138
|
+
}
|
|
139
|
+
if (currentBatch.length > 0) {
|
|
140
|
+
batches.push(currentBatch);
|
|
141
|
+
}
|
|
142
|
+
return batches;
|
|
143
|
+
}
|
|
144
|
+
var VoyageTextEmbeddingModelV2 = class {
|
|
145
|
+
constructor(config) {
|
|
146
|
+
this.specificationVersion = "v2";
|
|
147
|
+
this.provider = "voyage";
|
|
148
|
+
this.maxEmbeddingsPerCall = 1e3;
|
|
149
|
+
// VoyageAI supports up to 1000 inputs per API call
|
|
150
|
+
this.supportsParallelCalls = true;
|
|
151
|
+
this.modelId = config.model;
|
|
152
|
+
this.config = config;
|
|
153
|
+
this.maxInputTokens = TEXT_MODEL_INFO[config.model]?.maxInputTokens ?? 32e3;
|
|
154
|
+
const apiKey = config.apiKey || process.env.VOYAGE_API_KEY;
|
|
155
|
+
if (!apiKey) {
|
|
156
|
+
throw new Error(
|
|
157
|
+
"VoyageAI API key is required. Set VOYAGE_API_KEY environment variable or pass apiKey in config."
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
this.client = new voyageai.VoyageAIClient({ apiKey });
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Generate embeddings for the provided text values.
|
|
164
|
+
* Automatically splits inputs into token-aware batches when total tokens
|
|
165
|
+
* would exceed the model's limit.
|
|
166
|
+
*/
|
|
167
|
+
async doEmbed(args) {
|
|
168
|
+
const { values, providerOptions } = args;
|
|
169
|
+
const inputType = providerOptions?.voyage?.inputType ?? this.config.inputType;
|
|
170
|
+
const outputDimension = providerOptions?.voyage?.outputDimension ?? this.config.outputDimension;
|
|
171
|
+
const outputDtype = providerOptions?.voyage?.outputDtype ?? this.config.outputDtype;
|
|
172
|
+
const truncation = providerOptions?.voyage?.truncation ?? this.config.truncation ?? true;
|
|
173
|
+
const batches = await createTokenAwareBatches(
|
|
174
|
+
this.client,
|
|
175
|
+
this.modelId,
|
|
176
|
+
values,
|
|
177
|
+
this.maxInputTokens,
|
|
178
|
+
this.maxEmbeddingsPerCall
|
|
179
|
+
);
|
|
180
|
+
const allEmbeddings = [];
|
|
181
|
+
for (const batch of batches) {
|
|
182
|
+
const response = await this.client.embed({
|
|
183
|
+
input: batch,
|
|
184
|
+
model: this.modelId,
|
|
185
|
+
inputType: toSdkInputType(inputType),
|
|
186
|
+
outputDimension,
|
|
187
|
+
outputDtype,
|
|
188
|
+
truncation
|
|
189
|
+
});
|
|
190
|
+
const embeddings = response.data?.sort((a, b) => (a.index ?? 0) - (b.index ?? 0)).map((item) => item.embedding ?? []) ?? [];
|
|
191
|
+
allEmbeddings.push(...embeddings);
|
|
192
|
+
}
|
|
193
|
+
return { embeddings: allEmbeddings };
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
var VoyageTextEmbeddingModelV3 = class {
|
|
197
|
+
constructor(config) {
|
|
198
|
+
this.specificationVersion = "v3";
|
|
199
|
+
this.provider = "voyage";
|
|
200
|
+
this.maxEmbeddingsPerCall = 1e3;
|
|
201
|
+
// VoyageAI supports up to 1000 inputs per API call
|
|
202
|
+
this.supportsParallelCalls = true;
|
|
203
|
+
this.modelId = config.model;
|
|
204
|
+
this.v2Model = new VoyageTextEmbeddingModelV2(config);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Generate embeddings for the provided text values
|
|
208
|
+
*/
|
|
209
|
+
async doEmbed(args) {
|
|
210
|
+
const result = await this.v2Model.doEmbed(args);
|
|
211
|
+
return { ...result, warnings: [] };
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
function createVoyageTextEmbedding(config) {
|
|
215
|
+
const normalizedConfig = typeof config === "string" ? { model: config } : config;
|
|
216
|
+
return new VoyageTextEmbeddingModelV3(normalizedConfig);
|
|
217
|
+
}
|
|
218
|
+
function createVoyageTextEmbeddingV2(config) {
|
|
219
|
+
const normalizedConfig = typeof config === "string" ? { model: config } : config;
|
|
220
|
+
return new VoyageTextEmbeddingModelV2(normalizedConfig);
|
|
221
|
+
}
|
|
222
|
+
function toSdkContent(content) {
|
|
223
|
+
switch (content.type) {
|
|
224
|
+
case "text":
|
|
225
|
+
return content.text;
|
|
226
|
+
case "image_url":
|
|
227
|
+
return { type: "image_url", image_url: content.image_url };
|
|
228
|
+
case "image_base64":
|
|
229
|
+
return { type: "image_base64", image_base64: content.image_base64 };
|
|
230
|
+
case "video_url":
|
|
231
|
+
return { type: "video_url", video_url: content.video_url };
|
|
232
|
+
default:
|
|
233
|
+
throw new Error(`Unknown content type: ${content.type}`);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
function toSdkInput(input) {
|
|
237
|
+
return input.content.map(toSdkContent);
|
|
238
|
+
}
|
|
239
|
+
function toSdkInputType2(inputType) {
|
|
240
|
+
if (inputType === null) return void 0;
|
|
241
|
+
return inputType;
|
|
242
|
+
}
|
|
243
|
+
var VoyageMultimodalEmbeddingModel = class {
|
|
244
|
+
constructor(config) {
|
|
245
|
+
this.provider = "voyage";
|
|
246
|
+
this.maxEmbeddingsPerCall = 1e3;
|
|
247
|
+
this.supportsParallelCalls = true;
|
|
248
|
+
this.modelId = config.model;
|
|
249
|
+
this.config = config;
|
|
250
|
+
const apiKey = config.apiKey || process.env.VOYAGE_API_KEY;
|
|
251
|
+
if (!apiKey) {
|
|
252
|
+
throw new Error(
|
|
253
|
+
"VoyageAI API key is required. Set VOYAGE_API_KEY environment variable or pass apiKey in config."
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
this.client = new voyageai.VoyageAIClient({ apiKey });
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Generate embeddings for multimodal inputs
|
|
260
|
+
*
|
|
261
|
+
* @param args.values - Array of multimodal inputs, each containing interleaved content
|
|
262
|
+
* @param args.providerOptions - Runtime options to override config
|
|
263
|
+
* @returns Object containing embeddings array
|
|
264
|
+
*/
|
|
265
|
+
async doEmbed(args) {
|
|
266
|
+
const { values, providerOptions } = args;
|
|
267
|
+
const inputType = providerOptions?.voyage?.inputType ?? this.config.inputType;
|
|
268
|
+
const truncation = providerOptions?.voyage?.truncation ?? this.config.truncation ?? true;
|
|
269
|
+
const sdkInputs = values.map(toSdkInput);
|
|
270
|
+
const response = await this.client.multimodalEmbed({
|
|
271
|
+
inputs: sdkInputs,
|
|
272
|
+
model: this.modelId,
|
|
273
|
+
inputType: toSdkInputType2(inputType),
|
|
274
|
+
truncation
|
|
275
|
+
});
|
|
276
|
+
const embeddings = response.data?.sort((a, b) => (a.index ?? 0) - (b.index ?? 0)).map((item) => item.embedding ?? []) ?? [];
|
|
277
|
+
return { embeddings };
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Generate a single embedding for a multimodal input
|
|
281
|
+
*
|
|
282
|
+
* @param input - Single multimodal input
|
|
283
|
+
* @returns Single embedding vector
|
|
284
|
+
*/
|
|
285
|
+
async embedOne(input) {
|
|
286
|
+
const result = await this.doEmbed({ values: [input] });
|
|
287
|
+
return result.embeddings[0] ?? [];
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
function createVoyageMultimodalEmbedding(config) {
|
|
291
|
+
const normalizedConfig = typeof config === "string" ? { model: config } : config;
|
|
292
|
+
return new VoyageMultimodalEmbeddingModel(normalizedConfig);
|
|
293
|
+
}
|
|
294
|
+
function toSdkInputType3(inputType) {
|
|
295
|
+
if (inputType === null) return void 0;
|
|
296
|
+
return inputType;
|
|
297
|
+
}
|
|
298
|
+
var VoyageContextualizedEmbeddingModel = class {
|
|
299
|
+
constructor(config) {
|
|
300
|
+
this.provider = "voyage";
|
|
301
|
+
this.maxEmbeddingsPerCall = 1e3;
|
|
302
|
+
// Max inputs
|
|
303
|
+
this.maxTotalChunks = 16e3;
|
|
304
|
+
// Max total chunks across all inputs
|
|
305
|
+
this.supportsParallelCalls = true;
|
|
306
|
+
this.modelId = config.model;
|
|
307
|
+
this.config = config;
|
|
308
|
+
const apiKey = config.apiKey || process.env.VOYAGE_API_KEY;
|
|
309
|
+
if (!apiKey) {
|
|
310
|
+
throw new Error(
|
|
311
|
+
"VoyageAI API key is required. Set VOYAGE_API_KEY environment variable or pass apiKey in config."
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
this.client = new voyageai.VoyageAIClient({ apiKey });
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Generate contextualized embeddings for grouped chunks
|
|
318
|
+
*
|
|
319
|
+
* @param args.values - Nested array where each inner array contains chunks from the same document
|
|
320
|
+
* @param args.inputType - 'query' for search queries, 'document' for content being indexed
|
|
321
|
+
* @param args.outputDimension - Output embedding dimension (256, 512, 1024, or 2048)
|
|
322
|
+
* @param args.outputDtype - Output data type
|
|
323
|
+
* @param args.providerOptions - Runtime options to override config
|
|
324
|
+
* @returns Object containing flattened embeddings array (one per chunk across all documents)
|
|
325
|
+
*/
|
|
326
|
+
async doEmbed(args) {
|
|
327
|
+
const { values, providerOptions } = args;
|
|
328
|
+
const inputType = args.inputType ?? providerOptions?.voyage?.inputType ?? this.config.inputType;
|
|
329
|
+
const outputDimension = args.outputDimension ?? providerOptions?.voyage?.outputDimension ?? this.config.outputDimension;
|
|
330
|
+
const outputDtype = args.outputDtype ?? providerOptions?.voyage?.outputDtype ?? this.config.outputDtype;
|
|
331
|
+
const response = await this.client.contextualizedEmbed({
|
|
332
|
+
inputs: values,
|
|
333
|
+
model: this.modelId,
|
|
334
|
+
inputType: toSdkInputType3(inputType),
|
|
335
|
+
outputDimension,
|
|
336
|
+
outputDtype
|
|
337
|
+
});
|
|
338
|
+
const sortedData = [...response.data ?? []].sort((a, b) => (a.index ?? 0) - (b.index ?? 0));
|
|
339
|
+
const allEmbeddings = [];
|
|
340
|
+
const chunkCounts = [];
|
|
341
|
+
for (const item of sortedData) {
|
|
342
|
+
const chunkData = item.data ?? [];
|
|
343
|
+
const docEmbeddings = chunkData.map((chunk) => chunk.embedding ?? []);
|
|
344
|
+
chunkCounts.push(docEmbeddings.length);
|
|
345
|
+
allEmbeddings.push(...docEmbeddings);
|
|
346
|
+
}
|
|
347
|
+
return { embeddings: allEmbeddings, chunkCounts };
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Generate contextualized embeddings and return grouped by document
|
|
351
|
+
*
|
|
352
|
+
* @param args - Same as doEmbed
|
|
353
|
+
* @returns Embeddings grouped by document
|
|
354
|
+
*/
|
|
355
|
+
async doEmbedGrouped(args) {
|
|
356
|
+
const { values, providerOptions } = args;
|
|
357
|
+
const inputType = args.inputType ?? providerOptions?.voyage?.inputType ?? this.config.inputType;
|
|
358
|
+
const outputDimension = args.outputDimension ?? providerOptions?.voyage?.outputDimension ?? this.config.outputDimension;
|
|
359
|
+
const outputDtype = args.outputDtype ?? providerOptions?.voyage?.outputDtype ?? this.config.outputDtype;
|
|
360
|
+
const response = await this.client.contextualizedEmbed({
|
|
361
|
+
inputs: values,
|
|
362
|
+
model: this.modelId,
|
|
363
|
+
inputType: toSdkInputType3(inputType),
|
|
364
|
+
outputDimension,
|
|
365
|
+
outputDtype
|
|
366
|
+
});
|
|
367
|
+
const sortedData = [...response.data ?? []].sort((a, b) => (a.index ?? 0) - (b.index ?? 0));
|
|
368
|
+
const embeddingsByDocument = sortedData.map((item) => {
|
|
369
|
+
const chunkData = item.data ?? [];
|
|
370
|
+
return chunkData.map((chunk) => chunk.embedding ?? []);
|
|
371
|
+
});
|
|
372
|
+
return { embeddingsByDocument };
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Generate a query embedding (contextualized with itself)
|
|
376
|
+
*
|
|
377
|
+
* @param query - The search query text
|
|
378
|
+
* @returns Single embedding vector
|
|
379
|
+
*/
|
|
380
|
+
async embedQuery(query) {
|
|
381
|
+
const result = await this.doEmbed({
|
|
382
|
+
values: [[query]],
|
|
383
|
+
inputType: "query"
|
|
384
|
+
});
|
|
385
|
+
return result.embeddings[0] ?? [];
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Generate document chunk embeddings with context
|
|
389
|
+
*
|
|
390
|
+
* @param chunks - Array of text chunks from the same document
|
|
391
|
+
* @returns Array of embeddings, one per chunk
|
|
392
|
+
*/
|
|
393
|
+
async embedDocumentChunks(chunks) {
|
|
394
|
+
const result = await this.doEmbed({
|
|
395
|
+
values: [chunks],
|
|
396
|
+
inputType: "document"
|
|
397
|
+
});
|
|
398
|
+
return result.embeddings;
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
function createVoyageContextualizedEmbedding(config) {
|
|
402
|
+
const normalizedConfig = typeof config === "string" ? { model: config } : config;
|
|
403
|
+
return new VoyageContextualizedEmbeddingModel(normalizedConfig);
|
|
404
|
+
}
|
|
405
|
+
var VoyageRelevanceScorer = class {
|
|
406
|
+
constructor(config) {
|
|
407
|
+
this.modelId = config.model;
|
|
408
|
+
this.config = config;
|
|
409
|
+
const apiKey = config.apiKey || process.env.VOYAGE_API_KEY;
|
|
410
|
+
if (!apiKey) {
|
|
411
|
+
throw new Error(
|
|
412
|
+
"VoyageAI API key is required. Set VOYAGE_API_KEY environment variable or pass apiKey in config."
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
this.client = new voyageai.VoyageAIClient({ apiKey });
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Get relevance score between a query and a document.
|
|
419
|
+
*
|
|
420
|
+
* @param query - The search query (text1)
|
|
421
|
+
* @param document - The document to score (text2)
|
|
422
|
+
* @returns Relevance score between 0 and 1
|
|
423
|
+
*/
|
|
424
|
+
async getRelevanceScore(query, document) {
|
|
425
|
+
const response = await this.client.rerank({
|
|
426
|
+
query,
|
|
427
|
+
documents: [document],
|
|
428
|
+
model: this.modelId,
|
|
429
|
+
topK: 1,
|
|
430
|
+
truncation: this.config.truncation ?? true
|
|
431
|
+
});
|
|
432
|
+
const result = response.data?.[0];
|
|
433
|
+
if (!result || result.relevanceScore === void 0) {
|
|
434
|
+
throw new Error("No relevance score found in VoyageAI response");
|
|
435
|
+
}
|
|
436
|
+
return result.relevanceScore;
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Rerank multiple documents against a query.
|
|
440
|
+
*
|
|
441
|
+
* This is more efficient than calling getRelevanceScore multiple times
|
|
442
|
+
* as it makes a single API call for all documents.
|
|
443
|
+
*
|
|
444
|
+
* @param query - The search query
|
|
445
|
+
* @param documents - Array of documents to rerank
|
|
446
|
+
* @param topK - Optional number of top results to return
|
|
447
|
+
* @returns Array of reranked results with scores
|
|
448
|
+
*/
|
|
449
|
+
async rerankDocuments(query, documents, topK) {
|
|
450
|
+
const response = await this.client.rerank({
|
|
451
|
+
query,
|
|
452
|
+
documents,
|
|
453
|
+
model: this.modelId,
|
|
454
|
+
topK,
|
|
455
|
+
truncation: this.config.truncation ?? true
|
|
456
|
+
});
|
|
457
|
+
return response.data?.map((item) => ({
|
|
458
|
+
document: documents[item.index ?? 0] ?? "",
|
|
459
|
+
index: item.index ?? 0,
|
|
460
|
+
score: item.relevanceScore ?? 0
|
|
461
|
+
})) ?? [];
|
|
462
|
+
}
|
|
463
|
+
};
|
|
464
|
+
function createVoyageReranker(config) {
|
|
465
|
+
const normalizedConfig = typeof config === "string" ? { model: config } : config;
|
|
466
|
+
return new VoyageRelevanceScorer(normalizedConfig);
|
|
467
|
+
}
|
|
468
|
+
var voyageReranker = createVoyageReranker;
|
|
469
|
+
|
|
470
|
+
// src/index.ts
|
|
471
|
+
var voyageEmbedding = createVoyageTextEmbedding;
|
|
472
|
+
var voyageEmbeddingV2 = createVoyageTextEmbeddingV2;
|
|
473
|
+
var voyageMultimodalEmbedding = createVoyageMultimodalEmbedding;
|
|
474
|
+
var voyageContextualizedEmbedding = createVoyageContextualizedEmbedding;
|
|
475
|
+
var voyage = (() => {
|
|
476
|
+
const cache = /* @__PURE__ */ new Map();
|
|
477
|
+
function lazy(key, factory) {
|
|
478
|
+
let instance = cache.get(key);
|
|
479
|
+
if (!instance) {
|
|
480
|
+
instance = factory();
|
|
481
|
+
cache.set(key, instance);
|
|
482
|
+
}
|
|
483
|
+
return instance;
|
|
484
|
+
}
|
|
485
|
+
const base = {
|
|
486
|
+
get specificationVersion() {
|
|
487
|
+
return "v3";
|
|
488
|
+
},
|
|
489
|
+
get provider() {
|
|
490
|
+
return "voyage";
|
|
491
|
+
},
|
|
492
|
+
get modelId() {
|
|
493
|
+
return "voyage-3.5";
|
|
494
|
+
},
|
|
495
|
+
get maxEmbeddingsPerCall() {
|
|
496
|
+
return 1e3;
|
|
497
|
+
},
|
|
498
|
+
get supportsParallelCalls() {
|
|
499
|
+
return true;
|
|
500
|
+
},
|
|
501
|
+
doEmbed(args) {
|
|
502
|
+
return lazy("_default", () => createVoyageTextEmbedding("voyage-3.5")).doEmbed(args);
|
|
503
|
+
}
|
|
504
|
+
};
|
|
505
|
+
return Object.defineProperties(base, {
|
|
506
|
+
// Text models (V3) - voyage-4 series
|
|
507
|
+
v4large: { get: () => lazy("v4large", () => createVoyageTextEmbedding("voyage-4-large")) },
|
|
508
|
+
v4: { get: () => lazy("v4", () => createVoyageTextEmbedding("voyage-4")) },
|
|
509
|
+
v4lite: { get: () => lazy("v4lite", () => createVoyageTextEmbedding("voyage-4-lite")) },
|
|
510
|
+
// Text models (V3) - voyage-3 series
|
|
511
|
+
large: { get: () => lazy("large", () => createVoyageTextEmbedding("voyage-3-large")) },
|
|
512
|
+
v35: { get: () => lazy("v35", () => createVoyageTextEmbedding("voyage-3.5")) },
|
|
513
|
+
v35lite: { get: () => lazy("v35lite", () => createVoyageTextEmbedding("voyage-3.5-lite")) },
|
|
514
|
+
code: { get: () => lazy("code", () => createVoyageTextEmbedding("voyage-code-3")) },
|
|
515
|
+
finance: { get: () => lazy("finance", () => createVoyageTextEmbedding("voyage-finance-2")) },
|
|
516
|
+
law: { get: () => lazy("law", () => createVoyageTextEmbedding("voyage-law-2")) },
|
|
517
|
+
// Text models (V2) - voyage-4 series
|
|
518
|
+
v4largeV2: { get: () => lazy("v4largeV2", () => createVoyageTextEmbeddingV2("voyage-4-large")) },
|
|
519
|
+
v4V2: { get: () => lazy("v4V2", () => createVoyageTextEmbeddingV2("voyage-4")) },
|
|
520
|
+
v4liteV2: { get: () => lazy("v4liteV2", () => createVoyageTextEmbeddingV2("voyage-4-lite")) },
|
|
521
|
+
// Text models (V2) - voyage-3 series
|
|
522
|
+
largeV2: { get: () => lazy("largeV2", () => createVoyageTextEmbeddingV2("voyage-3-large")) },
|
|
523
|
+
v35V2: { get: () => lazy("v35V2", () => createVoyageTextEmbeddingV2("voyage-3.5")) },
|
|
524
|
+
v35liteV2: { get: () => lazy("v35liteV2", () => createVoyageTextEmbeddingV2("voyage-3.5-lite")) },
|
|
525
|
+
codeV2: { get: () => lazy("codeV2", () => createVoyageTextEmbeddingV2("voyage-code-3")) },
|
|
526
|
+
financeV2: { get: () => lazy("financeV2", () => createVoyageTextEmbeddingV2("voyage-finance-2")) },
|
|
527
|
+
lawV2: { get: () => lazy("lawV2", () => createVoyageTextEmbeddingV2("voyage-law-2")) },
|
|
528
|
+
// Multimodal models
|
|
529
|
+
multimodal: { get: () => lazy("multimodal", () => createVoyageMultimodalEmbedding("voyage-multimodal-3.5")) },
|
|
530
|
+
multimodal3: { get: () => lazy("multimodal3", () => createVoyageMultimodalEmbedding("voyage-multimodal-3")) },
|
|
531
|
+
multimodal35: { get: () => lazy("multimodal35", () => createVoyageMultimodalEmbedding("voyage-multimodal-3.5")) },
|
|
532
|
+
// Contextualized model
|
|
533
|
+
contextualized: {
|
|
534
|
+
get: () => lazy("contextualized", () => createVoyageContextualizedEmbedding("voyage-context-3"))
|
|
535
|
+
},
|
|
536
|
+
context3: { get: () => lazy("context3", () => createVoyageContextualizedEmbedding("voyage-context-3")) },
|
|
537
|
+
// Reranker models
|
|
538
|
+
reranker: { get: () => lazy("reranker", () => createVoyageReranker("rerank-2.5")) },
|
|
539
|
+
reranker25: { get: () => lazy("reranker25", () => createVoyageReranker("rerank-2.5")) },
|
|
540
|
+
reranker25lite: { get: () => lazy("reranker25lite", () => createVoyageReranker("rerank-2.5-lite")) },
|
|
541
|
+
reranker2: { get: () => lazy("reranker2", () => createVoyageReranker("rerank-2")) },
|
|
542
|
+
reranker2lite: { get: () => lazy("reranker2lite", () => createVoyageReranker("rerank-2-lite")) },
|
|
543
|
+
// Factory functions (no lazy needed - they are factories themselves)
|
|
544
|
+
embedding: { value: createVoyageTextEmbedding },
|
|
545
|
+
embeddingV2: { value: createVoyageTextEmbeddingV2 },
|
|
546
|
+
multimodalEmbedding: { value: createVoyageMultimodalEmbedding },
|
|
547
|
+
contextualizedEmbedding: { value: createVoyageContextualizedEmbedding },
|
|
548
|
+
createReranker: { value: createVoyageReranker }
|
|
549
|
+
});
|
|
550
|
+
})();
|
|
551
|
+
var index_default = voyage;
|
|
552
|
+
|
|
553
|
+
Object.defineProperty(exports, "VoyageAIClient", {
|
|
554
|
+
enumerable: true,
|
|
555
|
+
get: function () { return voyageai.VoyageAIClient; }
|
|
556
|
+
});
|
|
557
|
+
exports.CONTEXTUALIZED_MODEL_INFO = CONTEXTUALIZED_MODEL_INFO;
|
|
558
|
+
exports.MULTIMODAL_MODEL_INFO = MULTIMODAL_MODEL_INFO;
|
|
559
|
+
exports.RERANKER_MODEL_INFO = RERANKER_MODEL_INFO;
|
|
560
|
+
exports.TEXT_MODEL_INFO = TEXT_MODEL_INFO;
|
|
561
|
+
exports.VoyageContextualizedEmbeddingModel = VoyageContextualizedEmbeddingModel;
|
|
562
|
+
exports.VoyageMultimodalEmbeddingModel = VoyageMultimodalEmbeddingModel;
|
|
563
|
+
exports.VoyageRelevanceScorer = VoyageRelevanceScorer;
|
|
564
|
+
exports.VoyageTextEmbeddingModelV2 = VoyageTextEmbeddingModelV2;
|
|
565
|
+
exports.VoyageTextEmbeddingModelV3 = VoyageTextEmbeddingModelV3;
|
|
566
|
+
exports.createVoyageContextualizedEmbedding = createVoyageContextualizedEmbedding;
|
|
567
|
+
exports.createVoyageMultimodalEmbedding = createVoyageMultimodalEmbedding;
|
|
568
|
+
exports.createVoyageReranker = createVoyageReranker;
|
|
569
|
+
exports.createVoyageTextEmbedding = createVoyageTextEmbedding;
|
|
570
|
+
exports.createVoyageTextEmbeddingV2 = createVoyageTextEmbeddingV2;
|
|
571
|
+
exports.default = index_default;
|
|
572
|
+
exports.voyage = voyage;
|
|
573
|
+
exports.voyageContextualizedEmbedding = voyageContextualizedEmbedding;
|
|
574
|
+
exports.voyageEmbedding = voyageEmbedding;
|
|
575
|
+
exports.voyageEmbeddingV2 = voyageEmbeddingV2;
|
|
576
|
+
exports.voyageMultimodalEmbedding = voyageMultimodalEmbedding;
|
|
577
|
+
exports.voyageReranker = voyageReranker;
|
|
578
|
+
//# sourceMappingURL=index.cjs.map
|
|
579
|
+
//# sourceMappingURL=index.cjs.map
|