@ai-sdk/mistral 1.2.7 → 2.0.0-alpha.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/CHANGELOG.md +192 -15
- package/README.md +2 -2
- package/dist/index.d.mts +8 -26
- package/dist/index.d.ts +8 -26
- package/dist/index.js +225 -228
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +213 -213
- package/dist/index.mjs.map +1 -1
- package/package.json +14 -13
package/dist/index.js
CHANGED
|
@@ -26,15 +26,15 @@ __export(src_exports, {
|
|
|
26
26
|
module.exports = __toCommonJS(src_exports);
|
|
27
27
|
|
|
28
28
|
// src/mistral-provider.ts
|
|
29
|
-
var
|
|
29
|
+
var import_provider4 = require("@ai-sdk/provider");
|
|
30
|
+
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
|
30
31
|
|
|
31
32
|
// src/mistral-chat-language-model.ts
|
|
32
|
-
var
|
|
33
|
-
var
|
|
33
|
+
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
34
|
+
var import_zod3 = require("zod");
|
|
34
35
|
|
|
35
36
|
// src/convert-to-mistral-chat-messages.ts
|
|
36
37
|
var import_provider = require("@ai-sdk/provider");
|
|
37
|
-
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
38
38
|
function convertToMistralChatMessages(prompt) {
|
|
39
39
|
const messages = [];
|
|
40
40
|
for (let i = 0; i < prompt.length; i++) {
|
|
@@ -49,36 +49,27 @@ function convertToMistralChatMessages(prompt) {
|
|
|
49
49
|
messages.push({
|
|
50
50
|
role: "user",
|
|
51
51
|
content: content.map((part) => {
|
|
52
|
-
var _a;
|
|
53
52
|
switch (part.type) {
|
|
54
53
|
case "text": {
|
|
55
54
|
return { type: "text", text: part.text };
|
|
56
55
|
}
|
|
57
|
-
case "image": {
|
|
58
|
-
return {
|
|
59
|
-
type: "image_url",
|
|
60
|
-
image_url: part.image instanceof URL ? part.image.toString() : `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${(0, import_provider_utils.convertUint8ArrayToBase64)(part.image)}`
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
56
|
case "file": {
|
|
64
|
-
if (
|
|
57
|
+
if (part.mediaType.startsWith("image/")) {
|
|
58
|
+
const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
|
|
59
|
+
return {
|
|
60
|
+
type: "image_url",
|
|
61
|
+
image_url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${part.data}`
|
|
62
|
+
};
|
|
63
|
+
} else if (part.mediaType === "application/pdf") {
|
|
64
|
+
return {
|
|
65
|
+
type: "document_url",
|
|
66
|
+
document_url: part.data.toString()
|
|
67
|
+
};
|
|
68
|
+
} else {
|
|
65
69
|
throw new import_provider.UnsupportedFunctionalityError({
|
|
66
|
-
functionality: "
|
|
70
|
+
functionality: "Only images and PDF file parts are supported"
|
|
67
71
|
});
|
|
68
72
|
}
|
|
69
|
-
switch (part.mimeType) {
|
|
70
|
-
case "application/pdf": {
|
|
71
|
-
return {
|
|
72
|
-
type: "document_url",
|
|
73
|
-
document_url: part.data.toString()
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
default: {
|
|
77
|
-
throw new import_provider.UnsupportedFunctionalityError({
|
|
78
|
-
functionality: "Only PDF files are supported in user messages"
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
73
|
}
|
|
83
74
|
}
|
|
84
75
|
})
|
|
@@ -135,6 +126,19 @@ function convertToMistralChatMessages(prompt) {
|
|
|
135
126
|
return messages;
|
|
136
127
|
}
|
|
137
128
|
|
|
129
|
+
// src/get-response-metadata.ts
|
|
130
|
+
function getResponseMetadata({
|
|
131
|
+
id,
|
|
132
|
+
model,
|
|
133
|
+
created
|
|
134
|
+
}) {
|
|
135
|
+
return {
|
|
136
|
+
id: id != null ? id : void 0,
|
|
137
|
+
modelId: model != null ? model : void 0,
|
|
138
|
+
timestamp: created != null ? new Date(created * 1e3) : void 0
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
138
142
|
// src/map-mistral-finish-reason.ts
|
|
139
143
|
function mapMistralFinishReason(finishReason) {
|
|
140
144
|
switch (finishReason) {
|
|
@@ -150,42 +154,44 @@ function mapMistralFinishReason(finishReason) {
|
|
|
150
154
|
}
|
|
151
155
|
}
|
|
152
156
|
|
|
153
|
-
// src/mistral-
|
|
154
|
-
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
157
|
+
// src/mistral-chat-options.ts
|
|
155
158
|
var import_zod = require("zod");
|
|
156
|
-
var
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
159
|
+
var mistralProviderOptions = import_zod.z.object({
|
|
160
|
+
/**
|
|
161
|
+
Whether to inject a safety prompt before all conversations.
|
|
162
|
+
|
|
163
|
+
Defaults to `false`.
|
|
164
|
+
*/
|
|
165
|
+
safePrompt: import_zod.z.boolean().optional(),
|
|
166
|
+
documentImageLimit: import_zod.z.number().optional(),
|
|
167
|
+
documentPageLimit: import_zod.z.number().optional()
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// src/mistral-error.ts
|
|
171
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
172
|
+
var import_zod2 = require("zod");
|
|
173
|
+
var mistralErrorDataSchema = import_zod2.z.object({
|
|
174
|
+
object: import_zod2.z.literal("error"),
|
|
175
|
+
message: import_zod2.z.string(),
|
|
176
|
+
type: import_zod2.z.string(),
|
|
177
|
+
param: import_zod2.z.string().nullable(),
|
|
178
|
+
code: import_zod2.z.string().nullable()
|
|
162
179
|
});
|
|
163
|
-
var mistralFailedResponseHandler = (0,
|
|
180
|
+
var mistralFailedResponseHandler = (0, import_provider_utils.createJsonErrorResponseHandler)({
|
|
164
181
|
errorSchema: mistralErrorDataSchema,
|
|
165
182
|
errorToMessage: (data) => data.message
|
|
166
183
|
});
|
|
167
184
|
|
|
168
|
-
// src/get-response-metadata.ts
|
|
169
|
-
function getResponseMetadata({
|
|
170
|
-
id,
|
|
171
|
-
model,
|
|
172
|
-
created
|
|
173
|
-
}) {
|
|
174
|
-
return {
|
|
175
|
-
id: id != null ? id : void 0,
|
|
176
|
-
modelId: model != null ? model : void 0,
|
|
177
|
-
timestamp: created != null ? new Date(created * 1e3) : void 0
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
|
|
181
185
|
// src/mistral-prepare-tools.ts
|
|
182
186
|
var import_provider2 = require("@ai-sdk/provider");
|
|
183
|
-
function prepareTools(
|
|
184
|
-
|
|
185
|
-
|
|
187
|
+
function prepareTools({
|
|
188
|
+
tools,
|
|
189
|
+
toolChoice
|
|
190
|
+
}) {
|
|
191
|
+
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
186
192
|
const toolWarnings = [];
|
|
187
193
|
if (tools == null) {
|
|
188
|
-
return { tools: void 0,
|
|
194
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
189
195
|
}
|
|
190
196
|
const mistralTools = [];
|
|
191
197
|
for (const tool of tools) {
|
|
@@ -202,29 +208,28 @@ function prepareTools(mode) {
|
|
|
202
208
|
});
|
|
203
209
|
}
|
|
204
210
|
}
|
|
205
|
-
const toolChoice = mode.toolChoice;
|
|
206
211
|
if (toolChoice == null) {
|
|
207
|
-
return { tools: mistralTools,
|
|
212
|
+
return { tools: mistralTools, toolChoice: void 0, toolWarnings };
|
|
208
213
|
}
|
|
209
214
|
const type = toolChoice.type;
|
|
210
215
|
switch (type) {
|
|
211
216
|
case "auto":
|
|
212
217
|
case "none":
|
|
213
|
-
return { tools: mistralTools,
|
|
218
|
+
return { tools: mistralTools, toolChoice: type, toolWarnings };
|
|
214
219
|
case "required":
|
|
215
|
-
return { tools: mistralTools,
|
|
220
|
+
return { tools: mistralTools, toolChoice: "any", toolWarnings };
|
|
216
221
|
case "tool":
|
|
217
222
|
return {
|
|
218
223
|
tools: mistralTools.filter(
|
|
219
224
|
(tool) => tool.function.name === toolChoice.toolName
|
|
220
225
|
),
|
|
221
|
-
|
|
226
|
+
toolChoice: "any",
|
|
222
227
|
toolWarnings
|
|
223
228
|
};
|
|
224
229
|
default: {
|
|
225
230
|
const _exhaustiveCheck = type;
|
|
226
231
|
throw new import_provider2.UnsupportedFunctionalityError({
|
|
227
|
-
functionality: `
|
|
232
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
228
233
|
});
|
|
229
234
|
}
|
|
230
235
|
}
|
|
@@ -232,24 +237,20 @@ function prepareTools(mode) {
|
|
|
232
237
|
|
|
233
238
|
// src/mistral-chat-language-model.ts
|
|
234
239
|
var MistralChatLanguageModel = class {
|
|
235
|
-
constructor(modelId,
|
|
236
|
-
this.specificationVersion = "
|
|
237
|
-
this.
|
|
238
|
-
|
|
240
|
+
constructor(modelId, config) {
|
|
241
|
+
this.specificationVersion = "v2";
|
|
242
|
+
this.supportedUrls = {
|
|
243
|
+
"application/pdf": [/^https:\/\/.*$/]
|
|
244
|
+
};
|
|
239
245
|
this.modelId = modelId;
|
|
240
|
-
this.settings = settings;
|
|
241
246
|
this.config = config;
|
|
242
247
|
}
|
|
243
248
|
get provider() {
|
|
244
249
|
return this.config.provider;
|
|
245
250
|
}
|
|
246
|
-
|
|
247
|
-
return url.protocol === "https:";
|
|
248
|
-
}
|
|
249
|
-
getArgs({
|
|
250
|
-
mode,
|
|
251
|
+
async getArgs({
|
|
251
252
|
prompt,
|
|
252
|
-
|
|
253
|
+
maxOutputTokens,
|
|
253
254
|
temperature,
|
|
254
255
|
topP,
|
|
255
256
|
topK,
|
|
@@ -258,11 +259,17 @@ var MistralChatLanguageModel = class {
|
|
|
258
259
|
stopSequences,
|
|
259
260
|
responseFormat,
|
|
260
261
|
seed,
|
|
261
|
-
|
|
262
|
+
providerOptions,
|
|
263
|
+
tools,
|
|
264
|
+
toolChoice
|
|
262
265
|
}) {
|
|
263
|
-
var _a
|
|
264
|
-
const type = mode.type;
|
|
266
|
+
var _a;
|
|
265
267
|
const warnings = [];
|
|
268
|
+
const options = (_a = await (0, import_provider_utils2.parseProviderOptions)({
|
|
269
|
+
provider: "mistral",
|
|
270
|
+
providerOptions,
|
|
271
|
+
schema: mistralProviderOptions
|
|
272
|
+
})) != null ? _a : {};
|
|
266
273
|
if (topK != null) {
|
|
267
274
|
warnings.push({
|
|
268
275
|
type: "unsupported-setting",
|
|
@@ -298,126 +305,120 @@ var MistralChatLanguageModel = class {
|
|
|
298
305
|
// model id:
|
|
299
306
|
model: this.modelId,
|
|
300
307
|
// model specific settings:
|
|
301
|
-
safe_prompt:
|
|
308
|
+
safe_prompt: options.safePrompt,
|
|
302
309
|
// standardized settings:
|
|
303
|
-
max_tokens:
|
|
310
|
+
max_tokens: maxOutputTokens,
|
|
304
311
|
temperature,
|
|
305
312
|
top_p: topP,
|
|
306
313
|
random_seed: seed,
|
|
307
314
|
// response format:
|
|
308
315
|
response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? { type: "json_object" } : void 0,
|
|
309
316
|
// mistral-specific provider options:
|
|
310
|
-
document_image_limit:
|
|
311
|
-
document_page_limit:
|
|
317
|
+
document_image_limit: options.documentImageLimit,
|
|
318
|
+
document_page_limit: options.documentPageLimit,
|
|
312
319
|
// messages:
|
|
313
320
|
messages: convertToMistralChatMessages(prompt)
|
|
314
321
|
};
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
}
|
|
332
|
-
case "object-tool": {
|
|
333
|
-
return {
|
|
334
|
-
args: {
|
|
335
|
-
...baseArgs,
|
|
336
|
-
tool_choice: "any",
|
|
337
|
-
tools: [{ type: "function", function: mode.tool }]
|
|
338
|
-
},
|
|
339
|
-
warnings
|
|
340
|
-
};
|
|
341
|
-
}
|
|
342
|
-
default: {
|
|
343
|
-
const _exhaustiveCheck = type;
|
|
344
|
-
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
|
345
|
-
}
|
|
346
|
-
}
|
|
322
|
+
const {
|
|
323
|
+
tools: mistralTools,
|
|
324
|
+
toolChoice: mistralToolChoice,
|
|
325
|
+
toolWarnings
|
|
326
|
+
} = prepareTools({
|
|
327
|
+
tools,
|
|
328
|
+
toolChoice
|
|
329
|
+
});
|
|
330
|
+
return {
|
|
331
|
+
args: {
|
|
332
|
+
...baseArgs,
|
|
333
|
+
tools: mistralTools,
|
|
334
|
+
tool_choice: mistralToolChoice
|
|
335
|
+
},
|
|
336
|
+
warnings: [...warnings, ...toolWarnings]
|
|
337
|
+
};
|
|
347
338
|
}
|
|
348
339
|
async doGenerate(options) {
|
|
349
|
-
|
|
350
|
-
const { args, warnings } = this.getArgs(options);
|
|
340
|
+
const { args: body, warnings } = await this.getArgs(options);
|
|
351
341
|
const {
|
|
352
342
|
responseHeaders,
|
|
353
343
|
value: response,
|
|
354
344
|
rawValue: rawResponse
|
|
355
|
-
} = await (0,
|
|
345
|
+
} = await (0, import_provider_utils2.postJsonToApi)({
|
|
356
346
|
url: `${this.config.baseURL}/chat/completions`,
|
|
357
|
-
headers: (0,
|
|
358
|
-
body
|
|
347
|
+
headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
|
|
348
|
+
body,
|
|
359
349
|
failedResponseHandler: mistralFailedResponseHandler,
|
|
360
|
-
successfulResponseHandler: (0,
|
|
350
|
+
successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)(
|
|
361
351
|
mistralChatResponseSchema
|
|
362
352
|
),
|
|
363
353
|
abortSignal: options.abortSignal,
|
|
364
354
|
fetch: this.config.fetch
|
|
365
355
|
});
|
|
366
|
-
const { messages: rawPrompt, ...rawSettings } = args;
|
|
367
356
|
const choice = response.choices[0];
|
|
357
|
+
const content = [];
|
|
368
358
|
let text = extractTextContent(choice.message.content);
|
|
369
|
-
const lastMessage =
|
|
359
|
+
const lastMessage = body.messages[body.messages.length - 1];
|
|
370
360
|
if (lastMessage.role === "assistant" && (text == null ? void 0 : text.startsWith(lastMessage.content))) {
|
|
371
361
|
text = text.slice(lastMessage.content.length);
|
|
372
362
|
}
|
|
363
|
+
if (text != null && text.length > 0) {
|
|
364
|
+
content.push({ type: "text", text });
|
|
365
|
+
}
|
|
366
|
+
if (choice.message.tool_calls != null) {
|
|
367
|
+
for (const toolCall of choice.message.tool_calls) {
|
|
368
|
+
content.push({
|
|
369
|
+
type: "tool-call",
|
|
370
|
+
toolCallType: "function",
|
|
371
|
+
toolCallId: toolCall.id,
|
|
372
|
+
toolName: toolCall.function.name,
|
|
373
|
+
args: toolCall.function.arguments
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
}
|
|
373
377
|
return {
|
|
374
|
-
|
|
375
|
-
toolCalls: (_a = choice.message.tool_calls) == null ? void 0 : _a.map((toolCall) => ({
|
|
376
|
-
toolCallType: "function",
|
|
377
|
-
toolCallId: toolCall.id,
|
|
378
|
-
toolName: toolCall.function.name,
|
|
379
|
-
args: toolCall.function.arguments
|
|
380
|
-
})),
|
|
378
|
+
content,
|
|
381
379
|
finishReason: mapMistralFinishReason(choice.finish_reason),
|
|
382
380
|
usage: {
|
|
383
|
-
|
|
384
|
-
|
|
381
|
+
inputTokens: response.usage.prompt_tokens,
|
|
382
|
+
outputTokens: response.usage.completion_tokens,
|
|
383
|
+
totalTokens: response.usage.total_tokens
|
|
385
384
|
},
|
|
386
|
-
|
|
387
|
-
|
|
385
|
+
request: { body },
|
|
386
|
+
response: {
|
|
387
|
+
...getResponseMetadata(response),
|
|
388
388
|
headers: responseHeaders,
|
|
389
389
|
body: rawResponse
|
|
390
390
|
},
|
|
391
|
-
request: { body: JSON.stringify(args) },
|
|
392
|
-
response: getResponseMetadata(response),
|
|
393
391
|
warnings
|
|
394
392
|
};
|
|
395
393
|
}
|
|
396
394
|
async doStream(options) {
|
|
397
|
-
const { args, warnings } = this.getArgs(options);
|
|
395
|
+
const { args, warnings } = await this.getArgs(options);
|
|
398
396
|
const body = { ...args, stream: true };
|
|
399
|
-
const { responseHeaders, value: response } = await (0,
|
|
397
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
|
|
400
398
|
url: `${this.config.baseURL}/chat/completions`,
|
|
401
|
-
headers: (0,
|
|
399
|
+
headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
|
|
402
400
|
body,
|
|
403
401
|
failedResponseHandler: mistralFailedResponseHandler,
|
|
404
|
-
successfulResponseHandler: (0,
|
|
402
|
+
successfulResponseHandler: (0, import_provider_utils2.createEventSourceResponseHandler)(
|
|
405
403
|
mistralChatChunkSchema
|
|
406
404
|
),
|
|
407
405
|
abortSignal: options.abortSignal,
|
|
408
406
|
fetch: this.config.fetch
|
|
409
407
|
});
|
|
410
|
-
const { messages: rawPrompt, ...rawSettings } = args;
|
|
411
408
|
let finishReason = "unknown";
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
409
|
+
const usage = {
|
|
410
|
+
inputTokens: void 0,
|
|
411
|
+
outputTokens: void 0,
|
|
412
|
+
totalTokens: void 0
|
|
415
413
|
};
|
|
416
414
|
let chunkNumber = 0;
|
|
417
415
|
let trimLeadingSpace = false;
|
|
418
416
|
return {
|
|
419
417
|
stream: response.pipeThrough(
|
|
420
418
|
new TransformStream({
|
|
419
|
+
start(controller) {
|
|
420
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
421
|
+
},
|
|
421
422
|
transform(chunk, controller) {
|
|
422
423
|
if (!chunk.success) {
|
|
423
424
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
@@ -432,10 +433,9 @@ var MistralChatLanguageModel = class {
|
|
|
432
433
|
});
|
|
433
434
|
}
|
|
434
435
|
if (value.usage != null) {
|
|
435
|
-
usage =
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
};
|
|
436
|
+
usage.inputTokens = value.usage.prompt_tokens;
|
|
437
|
+
usage.outputTokens = value.usage.completion_tokens;
|
|
438
|
+
usage.totalTokens = value.usage.total_tokens;
|
|
439
439
|
}
|
|
440
440
|
const choice = value.choices[0];
|
|
441
441
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
@@ -447,7 +447,7 @@ var MistralChatLanguageModel = class {
|
|
|
447
447
|
const delta = choice.delta;
|
|
448
448
|
const textContent = extractTextContent(delta.content);
|
|
449
449
|
if (chunkNumber <= 2) {
|
|
450
|
-
const lastMessage =
|
|
450
|
+
const lastMessage = body.messages[body.messages.length - 1];
|
|
451
451
|
if (lastMessage.role === "assistant" && textContent === lastMessage.content.trimEnd()) {
|
|
452
452
|
if (textContent.length < lastMessage.content.length) {
|
|
453
453
|
trimLeadingSpace = true;
|
|
@@ -457,8 +457,8 @@ var MistralChatLanguageModel = class {
|
|
|
457
457
|
}
|
|
458
458
|
if (textContent != null) {
|
|
459
459
|
controller.enqueue({
|
|
460
|
-
type: "text
|
|
461
|
-
|
|
460
|
+
type: "text",
|
|
461
|
+
text: trimLeadingSpace ? textContent.trimStart() : textContent
|
|
462
462
|
});
|
|
463
463
|
trimLeadingSpace = false;
|
|
464
464
|
}
|
|
@@ -486,10 +486,8 @@ var MistralChatLanguageModel = class {
|
|
|
486
486
|
}
|
|
487
487
|
})
|
|
488
488
|
),
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
request: { body: JSON.stringify(body) },
|
|
492
|
-
warnings
|
|
489
|
+
request: { body },
|
|
490
|
+
response: { headers: responseHeaders }
|
|
493
491
|
};
|
|
494
492
|
}
|
|
495
493
|
};
|
|
@@ -518,105 +516,97 @@ function extractTextContent(content) {
|
|
|
518
516
|
}
|
|
519
517
|
return textContent.length ? textContent.join("") : void 0;
|
|
520
518
|
}
|
|
521
|
-
var mistralContentSchema =
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
type:
|
|
527
|
-
text:
|
|
519
|
+
var mistralContentSchema = import_zod3.z.union([
|
|
520
|
+
import_zod3.z.string(),
|
|
521
|
+
import_zod3.z.array(
|
|
522
|
+
import_zod3.z.discriminatedUnion("type", [
|
|
523
|
+
import_zod3.z.object({
|
|
524
|
+
type: import_zod3.z.literal("text"),
|
|
525
|
+
text: import_zod3.z.string()
|
|
528
526
|
}),
|
|
529
|
-
|
|
530
|
-
type:
|
|
531
|
-
image_url:
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
url:
|
|
535
|
-
detail:
|
|
527
|
+
import_zod3.z.object({
|
|
528
|
+
type: import_zod3.z.literal("image_url"),
|
|
529
|
+
image_url: import_zod3.z.union([
|
|
530
|
+
import_zod3.z.string(),
|
|
531
|
+
import_zod3.z.object({
|
|
532
|
+
url: import_zod3.z.string(),
|
|
533
|
+
detail: import_zod3.z.string().nullable()
|
|
536
534
|
})
|
|
537
535
|
])
|
|
538
536
|
}),
|
|
539
|
-
|
|
540
|
-
type:
|
|
541
|
-
reference_ids:
|
|
537
|
+
import_zod3.z.object({
|
|
538
|
+
type: import_zod3.z.literal("reference"),
|
|
539
|
+
reference_ids: import_zod3.z.array(import_zod3.z.number())
|
|
542
540
|
})
|
|
543
541
|
])
|
|
544
542
|
)
|
|
545
543
|
]).nullish();
|
|
546
|
-
var
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
544
|
+
var mistralUsageSchema = import_zod3.z.object({
|
|
545
|
+
prompt_tokens: import_zod3.z.number(),
|
|
546
|
+
completion_tokens: import_zod3.z.number(),
|
|
547
|
+
total_tokens: import_zod3.z.number()
|
|
548
|
+
});
|
|
549
|
+
var mistralChatResponseSchema = import_zod3.z.object({
|
|
550
|
+
id: import_zod3.z.string().nullish(),
|
|
551
|
+
created: import_zod3.z.number().nullish(),
|
|
552
|
+
model: import_zod3.z.string().nullish(),
|
|
553
|
+
choices: import_zod3.z.array(
|
|
554
|
+
import_zod3.z.object({
|
|
555
|
+
message: import_zod3.z.object({
|
|
556
|
+
role: import_zod3.z.literal("assistant"),
|
|
554
557
|
content: mistralContentSchema,
|
|
555
|
-
tool_calls:
|
|
556
|
-
|
|
557
|
-
id:
|
|
558
|
-
function:
|
|
558
|
+
tool_calls: import_zod3.z.array(
|
|
559
|
+
import_zod3.z.object({
|
|
560
|
+
id: import_zod3.z.string(),
|
|
561
|
+
function: import_zod3.z.object({ name: import_zod3.z.string(), arguments: import_zod3.z.string() })
|
|
559
562
|
})
|
|
560
563
|
).nullish()
|
|
561
564
|
}),
|
|
562
|
-
index:
|
|
563
|
-
finish_reason:
|
|
565
|
+
index: import_zod3.z.number(),
|
|
566
|
+
finish_reason: import_zod3.z.string().nullish()
|
|
564
567
|
})
|
|
565
568
|
),
|
|
566
|
-
object:
|
|
567
|
-
usage:
|
|
568
|
-
prompt_tokens: import_zod2.z.number(),
|
|
569
|
-
completion_tokens: import_zod2.z.number()
|
|
570
|
-
})
|
|
569
|
+
object: import_zod3.z.literal("chat.completion"),
|
|
570
|
+
usage: mistralUsageSchema
|
|
571
571
|
});
|
|
572
|
-
var mistralChatChunkSchema =
|
|
573
|
-
id:
|
|
574
|
-
created:
|
|
575
|
-
model:
|
|
576
|
-
choices:
|
|
577
|
-
|
|
578
|
-
delta:
|
|
579
|
-
role:
|
|
572
|
+
var mistralChatChunkSchema = import_zod3.z.object({
|
|
573
|
+
id: import_zod3.z.string().nullish(),
|
|
574
|
+
created: import_zod3.z.number().nullish(),
|
|
575
|
+
model: import_zod3.z.string().nullish(),
|
|
576
|
+
choices: import_zod3.z.array(
|
|
577
|
+
import_zod3.z.object({
|
|
578
|
+
delta: import_zod3.z.object({
|
|
579
|
+
role: import_zod3.z.enum(["assistant"]).optional(),
|
|
580
580
|
content: mistralContentSchema,
|
|
581
|
-
tool_calls:
|
|
582
|
-
|
|
583
|
-
id:
|
|
584
|
-
function:
|
|
581
|
+
tool_calls: import_zod3.z.array(
|
|
582
|
+
import_zod3.z.object({
|
|
583
|
+
id: import_zod3.z.string(),
|
|
584
|
+
function: import_zod3.z.object({ name: import_zod3.z.string(), arguments: import_zod3.z.string() })
|
|
585
585
|
})
|
|
586
586
|
).nullish()
|
|
587
587
|
}),
|
|
588
|
-
finish_reason:
|
|
589
|
-
index:
|
|
588
|
+
finish_reason: import_zod3.z.string().nullish(),
|
|
589
|
+
index: import_zod3.z.number()
|
|
590
590
|
})
|
|
591
591
|
),
|
|
592
|
-
usage:
|
|
593
|
-
prompt_tokens: import_zod2.z.number(),
|
|
594
|
-
completion_tokens: import_zod2.z.number()
|
|
595
|
-
}).nullish()
|
|
592
|
+
usage: mistralUsageSchema.nullish()
|
|
596
593
|
});
|
|
597
594
|
|
|
598
595
|
// src/mistral-embedding-model.ts
|
|
599
596
|
var import_provider3 = require("@ai-sdk/provider");
|
|
600
|
-
var
|
|
601
|
-
var
|
|
597
|
+
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
598
|
+
var import_zod4 = require("zod");
|
|
602
599
|
var MistralEmbeddingModel = class {
|
|
603
|
-
constructor(modelId,
|
|
604
|
-
this.specificationVersion = "
|
|
600
|
+
constructor(modelId, config) {
|
|
601
|
+
this.specificationVersion = "v2";
|
|
602
|
+
this.maxEmbeddingsPerCall = 32;
|
|
603
|
+
this.supportsParallelCalls = false;
|
|
605
604
|
this.modelId = modelId;
|
|
606
|
-
this.settings = settings;
|
|
607
605
|
this.config = config;
|
|
608
606
|
}
|
|
609
607
|
get provider() {
|
|
610
608
|
return this.config.provider;
|
|
611
609
|
}
|
|
612
|
-
get maxEmbeddingsPerCall() {
|
|
613
|
-
var _a;
|
|
614
|
-
return (_a = this.settings.maxEmbeddingsPerCall) != null ? _a : 32;
|
|
615
|
-
}
|
|
616
|
-
get supportsParallelCalls() {
|
|
617
|
-
var _a;
|
|
618
|
-
return (_a = this.settings.supportsParallelCalls) != null ? _a : false;
|
|
619
|
-
}
|
|
620
610
|
async doEmbed({
|
|
621
611
|
values,
|
|
622
612
|
abortSignal,
|
|
@@ -630,16 +620,20 @@ var MistralEmbeddingModel = class {
|
|
|
630
620
|
values
|
|
631
621
|
});
|
|
632
622
|
}
|
|
633
|
-
const {
|
|
623
|
+
const {
|
|
624
|
+
responseHeaders,
|
|
625
|
+
value: response,
|
|
626
|
+
rawValue
|
|
627
|
+
} = await (0, import_provider_utils3.postJsonToApi)({
|
|
634
628
|
url: `${this.config.baseURL}/embeddings`,
|
|
635
|
-
headers: (0,
|
|
629
|
+
headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), headers),
|
|
636
630
|
body: {
|
|
637
631
|
model: this.modelId,
|
|
638
632
|
input: values,
|
|
639
633
|
encoding_format: "float"
|
|
640
634
|
},
|
|
641
635
|
failedResponseHandler: mistralFailedResponseHandler,
|
|
642
|
-
successfulResponseHandler: (0,
|
|
636
|
+
successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
|
|
643
637
|
MistralTextEmbeddingResponseSchema
|
|
644
638
|
),
|
|
645
639
|
abortSignal,
|
|
@@ -648,52 +642,55 @@ var MistralEmbeddingModel = class {
|
|
|
648
642
|
return {
|
|
649
643
|
embeddings: response.data.map((item) => item.embedding),
|
|
650
644
|
usage: response.usage ? { tokens: response.usage.prompt_tokens } : void 0,
|
|
651
|
-
|
|
645
|
+
response: { headers: responseHeaders, body: rawValue }
|
|
652
646
|
};
|
|
653
647
|
}
|
|
654
648
|
};
|
|
655
|
-
var MistralTextEmbeddingResponseSchema =
|
|
656
|
-
data:
|
|
657
|
-
usage:
|
|
649
|
+
var MistralTextEmbeddingResponseSchema = import_zod4.z.object({
|
|
650
|
+
data: import_zod4.z.array(import_zod4.z.object({ embedding: import_zod4.z.array(import_zod4.z.number()) })),
|
|
651
|
+
usage: import_zod4.z.object({ prompt_tokens: import_zod4.z.number() }).nullish()
|
|
658
652
|
});
|
|
659
653
|
|
|
660
654
|
// src/mistral-provider.ts
|
|
661
655
|
function createMistral(options = {}) {
|
|
662
656
|
var _a;
|
|
663
|
-
const baseURL = (_a = (0,
|
|
657
|
+
const baseURL = (_a = (0, import_provider_utils4.withoutTrailingSlash)(options.baseURL)) != null ? _a : "https://api.mistral.ai/v1";
|
|
664
658
|
const getHeaders = () => ({
|
|
665
|
-
Authorization: `Bearer ${(0,
|
|
659
|
+
Authorization: `Bearer ${(0, import_provider_utils4.loadApiKey)({
|
|
666
660
|
apiKey: options.apiKey,
|
|
667
661
|
environmentVariableName: "MISTRAL_API_KEY",
|
|
668
662
|
description: "Mistral"
|
|
669
663
|
})}`,
|
|
670
664
|
...options.headers
|
|
671
665
|
});
|
|
672
|
-
const createChatModel = (modelId
|
|
666
|
+
const createChatModel = (modelId) => new MistralChatLanguageModel(modelId, {
|
|
673
667
|
provider: "mistral.chat",
|
|
674
668
|
baseURL,
|
|
675
669
|
headers: getHeaders,
|
|
676
670
|
fetch: options.fetch
|
|
677
671
|
});
|
|
678
|
-
const createEmbeddingModel = (modelId
|
|
672
|
+
const createEmbeddingModel = (modelId) => new MistralEmbeddingModel(modelId, {
|
|
679
673
|
provider: "mistral.embedding",
|
|
680
674
|
baseURL,
|
|
681
675
|
headers: getHeaders,
|
|
682
676
|
fetch: options.fetch
|
|
683
677
|
});
|
|
684
|
-
const provider = function(modelId
|
|
678
|
+
const provider = function(modelId) {
|
|
685
679
|
if (new.target) {
|
|
686
680
|
throw new Error(
|
|
687
681
|
"The Mistral model function cannot be called with the new keyword."
|
|
688
682
|
);
|
|
689
683
|
}
|
|
690
|
-
return createChatModel(modelId
|
|
684
|
+
return createChatModel(modelId);
|
|
691
685
|
};
|
|
692
686
|
provider.languageModel = createChatModel;
|
|
693
687
|
provider.chat = createChatModel;
|
|
694
688
|
provider.embedding = createEmbeddingModel;
|
|
695
689
|
provider.textEmbedding = createEmbeddingModel;
|
|
696
690
|
provider.textEmbeddingModel = createEmbeddingModel;
|
|
691
|
+
provider.imageModel = (modelId) => {
|
|
692
|
+
throw new import_provider4.NoSuchModelError({ modelId, modelType: "imageModel" });
|
|
693
|
+
};
|
|
697
694
|
return provider;
|
|
698
695
|
}
|
|
699
696
|
var mistral = createMistral();
|