@ai-sdk/mistral 2.0.0-canary.0 → 2.0.0-canary.10
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 +109 -0
- package/dist/index.d.mts +8 -16
- package/dist/index.d.ts +8 -16
- package/dist/index.js +216 -212
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +204 -197
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -10
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
// src/mistral-provider.ts
|
|
2
|
+
import {
|
|
3
|
+
NoSuchModelError
|
|
4
|
+
} from "@ai-sdk/provider";
|
|
2
5
|
import {
|
|
3
6
|
loadApiKey,
|
|
4
7
|
withoutTrailingSlash
|
|
@@ -9,15 +12,15 @@ import {
|
|
|
9
12
|
combineHeaders,
|
|
10
13
|
createEventSourceResponseHandler,
|
|
11
14
|
createJsonResponseHandler,
|
|
15
|
+
parseProviderOptions,
|
|
12
16
|
postJsonToApi
|
|
13
17
|
} from "@ai-sdk/provider-utils";
|
|
14
|
-
import { z as
|
|
18
|
+
import { z as z3 } from "zod";
|
|
15
19
|
|
|
16
20
|
// src/convert-to-mistral-chat-messages.ts
|
|
17
21
|
import {
|
|
18
22
|
UnsupportedFunctionalityError
|
|
19
23
|
} from "@ai-sdk/provider";
|
|
20
|
-
import { convertUint8ArrayToBase64 } from "@ai-sdk/provider-utils";
|
|
21
24
|
function convertToMistralChatMessages(prompt) {
|
|
22
25
|
const messages = [];
|
|
23
26
|
for (let i = 0; i < prompt.length; i++) {
|
|
@@ -32,36 +35,27 @@ function convertToMistralChatMessages(prompt) {
|
|
|
32
35
|
messages.push({
|
|
33
36
|
role: "user",
|
|
34
37
|
content: content.map((part) => {
|
|
35
|
-
var _a;
|
|
36
38
|
switch (part.type) {
|
|
37
39
|
case "text": {
|
|
38
40
|
return { type: "text", text: part.text };
|
|
39
41
|
}
|
|
40
|
-
case "image": {
|
|
41
|
-
return {
|
|
42
|
-
type: "image_url",
|
|
43
|
-
image_url: part.image instanceof URL ? part.image.toString() : `data:${(_a = part.mimeType) != null ? _a : "image/jpeg"};base64,${convertUint8ArrayToBase64(part.image)}`
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
42
|
case "file": {
|
|
47
|
-
if (
|
|
43
|
+
if (part.mediaType.startsWith("image/")) {
|
|
44
|
+
const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
|
|
45
|
+
return {
|
|
46
|
+
type: "image_url",
|
|
47
|
+
image_url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${part.data}`
|
|
48
|
+
};
|
|
49
|
+
} else if (part.mediaType === "application/pdf") {
|
|
50
|
+
return {
|
|
51
|
+
type: "document_url",
|
|
52
|
+
document_url: part.data.toString()
|
|
53
|
+
};
|
|
54
|
+
} else {
|
|
48
55
|
throw new UnsupportedFunctionalityError({
|
|
49
|
-
functionality: "
|
|
56
|
+
functionality: "Only images and PDF file parts are supported"
|
|
50
57
|
});
|
|
51
58
|
}
|
|
52
|
-
switch (part.mimeType) {
|
|
53
|
-
case "application/pdf": {
|
|
54
|
-
return {
|
|
55
|
-
type: "document_url",
|
|
56
|
-
document_url: part.data.toString()
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
default: {
|
|
60
|
-
throw new UnsupportedFunctionalityError({
|
|
61
|
-
functionality: "Only PDF files are supported in user messages"
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
59
|
}
|
|
66
60
|
}
|
|
67
61
|
})
|
|
@@ -118,6 +112,19 @@ function convertToMistralChatMessages(prompt) {
|
|
|
118
112
|
return messages;
|
|
119
113
|
}
|
|
120
114
|
|
|
115
|
+
// src/get-response-metadata.ts
|
|
116
|
+
function getResponseMetadata({
|
|
117
|
+
id,
|
|
118
|
+
model,
|
|
119
|
+
created
|
|
120
|
+
}) {
|
|
121
|
+
return {
|
|
122
|
+
id: id != null ? id : void 0,
|
|
123
|
+
modelId: model != null ? model : void 0,
|
|
124
|
+
timestamp: created != null ? new Date(created * 1e3) : void 0
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
121
128
|
// src/map-mistral-finish-reason.ts
|
|
122
129
|
function mapMistralFinishReason(finishReason) {
|
|
123
130
|
switch (finishReason) {
|
|
@@ -133,44 +140,46 @@ function mapMistralFinishReason(finishReason) {
|
|
|
133
140
|
}
|
|
134
141
|
}
|
|
135
142
|
|
|
143
|
+
// src/mistral-chat-options.ts
|
|
144
|
+
import { z } from "zod";
|
|
145
|
+
var mistralProviderOptions = z.object({
|
|
146
|
+
/**
|
|
147
|
+
Whether to inject a safety prompt before all conversations.
|
|
148
|
+
|
|
149
|
+
Defaults to `false`.
|
|
150
|
+
*/
|
|
151
|
+
safePrompt: z.boolean().nullish(),
|
|
152
|
+
documentImageLimit: z.number().nullish(),
|
|
153
|
+
documentPageLimit: z.number().nullish()
|
|
154
|
+
});
|
|
155
|
+
|
|
136
156
|
// src/mistral-error.ts
|
|
137
157
|
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
|
|
138
|
-
import { z } from "zod";
|
|
139
|
-
var mistralErrorDataSchema =
|
|
140
|
-
object:
|
|
141
|
-
message:
|
|
142
|
-
type:
|
|
143
|
-
param:
|
|
144
|
-
code:
|
|
158
|
+
import { z as z2 } from "zod";
|
|
159
|
+
var mistralErrorDataSchema = z2.object({
|
|
160
|
+
object: z2.literal("error"),
|
|
161
|
+
message: z2.string(),
|
|
162
|
+
type: z2.string(),
|
|
163
|
+
param: z2.string().nullable(),
|
|
164
|
+
code: z2.string().nullable()
|
|
145
165
|
});
|
|
146
166
|
var mistralFailedResponseHandler = createJsonErrorResponseHandler({
|
|
147
167
|
errorSchema: mistralErrorDataSchema,
|
|
148
168
|
errorToMessage: (data) => data.message
|
|
149
169
|
});
|
|
150
170
|
|
|
151
|
-
// src/get-response-metadata.ts
|
|
152
|
-
function getResponseMetadata({
|
|
153
|
-
id,
|
|
154
|
-
model,
|
|
155
|
-
created
|
|
156
|
-
}) {
|
|
157
|
-
return {
|
|
158
|
-
id: id != null ? id : void 0,
|
|
159
|
-
modelId: model != null ? model : void 0,
|
|
160
|
-
timestamp: created != null ? new Date(created * 1e3) : void 0
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
|
|
164
171
|
// src/mistral-prepare-tools.ts
|
|
165
172
|
import {
|
|
166
173
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
167
174
|
} from "@ai-sdk/provider";
|
|
168
|
-
function prepareTools(
|
|
169
|
-
|
|
170
|
-
|
|
175
|
+
function prepareTools({
|
|
176
|
+
tools,
|
|
177
|
+
toolChoice
|
|
178
|
+
}) {
|
|
179
|
+
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
171
180
|
const toolWarnings = [];
|
|
172
181
|
if (tools == null) {
|
|
173
|
-
return { tools: void 0,
|
|
182
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
174
183
|
}
|
|
175
184
|
const mistralTools = [];
|
|
176
185
|
for (const tool of tools) {
|
|
@@ -187,29 +196,28 @@ function prepareTools(mode) {
|
|
|
187
196
|
});
|
|
188
197
|
}
|
|
189
198
|
}
|
|
190
|
-
const toolChoice = mode.toolChoice;
|
|
191
199
|
if (toolChoice == null) {
|
|
192
|
-
return { tools: mistralTools,
|
|
200
|
+
return { tools: mistralTools, toolChoice: void 0, toolWarnings };
|
|
193
201
|
}
|
|
194
202
|
const type = toolChoice.type;
|
|
195
203
|
switch (type) {
|
|
196
204
|
case "auto":
|
|
197
205
|
case "none":
|
|
198
|
-
return { tools: mistralTools,
|
|
206
|
+
return { tools: mistralTools, toolChoice: type, toolWarnings };
|
|
199
207
|
case "required":
|
|
200
|
-
return { tools: mistralTools,
|
|
208
|
+
return { tools: mistralTools, toolChoice: "any", toolWarnings };
|
|
201
209
|
case "tool":
|
|
202
210
|
return {
|
|
203
211
|
tools: mistralTools.filter(
|
|
204
212
|
(tool) => tool.function.name === toolChoice.toolName
|
|
205
213
|
),
|
|
206
|
-
|
|
214
|
+
toolChoice: "any",
|
|
207
215
|
toolWarnings
|
|
208
216
|
};
|
|
209
217
|
default: {
|
|
210
218
|
const _exhaustiveCheck = type;
|
|
211
219
|
throw new UnsupportedFunctionalityError2({
|
|
212
|
-
functionality: `
|
|
220
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
213
221
|
});
|
|
214
222
|
}
|
|
215
223
|
}
|
|
@@ -217,24 +225,22 @@ function prepareTools(mode) {
|
|
|
217
225
|
|
|
218
226
|
// src/mistral-chat-language-model.ts
|
|
219
227
|
var MistralChatLanguageModel = class {
|
|
220
|
-
constructor(modelId,
|
|
221
|
-
this.specificationVersion = "
|
|
222
|
-
this.defaultObjectGenerationMode = "json";
|
|
223
|
-
this.supportsImageUrls = false;
|
|
228
|
+
constructor(modelId, config) {
|
|
229
|
+
this.specificationVersion = "v2";
|
|
224
230
|
this.modelId = modelId;
|
|
225
|
-
this.settings = settings;
|
|
226
231
|
this.config = config;
|
|
227
232
|
}
|
|
228
233
|
get provider() {
|
|
229
234
|
return this.config.provider;
|
|
230
235
|
}
|
|
231
|
-
|
|
232
|
-
return
|
|
236
|
+
async getSupportedUrls() {
|
|
237
|
+
return {
|
|
238
|
+
"application/pdf": [/^https:\/\/.*$/]
|
|
239
|
+
};
|
|
233
240
|
}
|
|
234
241
|
getArgs({
|
|
235
|
-
mode,
|
|
236
242
|
prompt,
|
|
237
|
-
|
|
243
|
+
maxOutputTokens,
|
|
238
244
|
temperature,
|
|
239
245
|
topP,
|
|
240
246
|
topK,
|
|
@@ -243,11 +249,17 @@ var MistralChatLanguageModel = class {
|
|
|
243
249
|
stopSequences,
|
|
244
250
|
responseFormat,
|
|
245
251
|
seed,
|
|
246
|
-
|
|
252
|
+
providerOptions,
|
|
253
|
+
tools,
|
|
254
|
+
toolChoice
|
|
247
255
|
}) {
|
|
248
|
-
var _a
|
|
249
|
-
const type = mode.type;
|
|
256
|
+
var _a;
|
|
250
257
|
const warnings = [];
|
|
258
|
+
const options = (_a = parseProviderOptions({
|
|
259
|
+
provider: "mistral",
|
|
260
|
+
providerOptions,
|
|
261
|
+
schema: mistralProviderOptions
|
|
262
|
+
})) != null ? _a : {};
|
|
251
263
|
if (topK != null) {
|
|
252
264
|
warnings.push({
|
|
253
265
|
type: "unsupported-setting",
|
|
@@ -283,56 +295,39 @@ var MistralChatLanguageModel = class {
|
|
|
283
295
|
// model id:
|
|
284
296
|
model: this.modelId,
|
|
285
297
|
// model specific settings:
|
|
286
|
-
safe_prompt:
|
|
298
|
+
safe_prompt: options.safePrompt,
|
|
287
299
|
// standardized settings:
|
|
288
|
-
max_tokens:
|
|
300
|
+
max_tokens: maxOutputTokens,
|
|
289
301
|
temperature,
|
|
290
302
|
top_p: topP,
|
|
291
303
|
random_seed: seed,
|
|
292
304
|
// response format:
|
|
293
305
|
response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? { type: "json_object" } : void 0,
|
|
294
306
|
// mistral-specific provider options:
|
|
295
|
-
document_image_limit:
|
|
296
|
-
document_page_limit:
|
|
307
|
+
document_image_limit: options.documentImageLimit,
|
|
308
|
+
document_page_limit: options.documentPageLimit,
|
|
297
309
|
// messages:
|
|
298
310
|
messages: convertToMistralChatMessages(prompt)
|
|
299
311
|
};
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
}
|
|
317
|
-
case "object-tool": {
|
|
318
|
-
return {
|
|
319
|
-
args: {
|
|
320
|
-
...baseArgs,
|
|
321
|
-
tool_choice: "any",
|
|
322
|
-
tools: [{ type: "function", function: mode.tool }]
|
|
323
|
-
},
|
|
324
|
-
warnings
|
|
325
|
-
};
|
|
326
|
-
}
|
|
327
|
-
default: {
|
|
328
|
-
const _exhaustiveCheck = type;
|
|
329
|
-
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
|
330
|
-
}
|
|
331
|
-
}
|
|
312
|
+
const {
|
|
313
|
+
tools: mistralTools,
|
|
314
|
+
toolChoice: mistralToolChoice,
|
|
315
|
+
toolWarnings
|
|
316
|
+
} = prepareTools({
|
|
317
|
+
tools,
|
|
318
|
+
toolChoice
|
|
319
|
+
});
|
|
320
|
+
return {
|
|
321
|
+
args: {
|
|
322
|
+
...baseArgs,
|
|
323
|
+
tools: mistralTools,
|
|
324
|
+
tool_choice: mistralToolChoice
|
|
325
|
+
},
|
|
326
|
+
warnings: [...warnings, ...toolWarnings]
|
|
327
|
+
};
|
|
332
328
|
}
|
|
333
329
|
async doGenerate(options) {
|
|
334
|
-
|
|
335
|
-
const { args, warnings } = this.getArgs(options);
|
|
330
|
+
const { args: body, warnings } = this.getArgs(options);
|
|
336
331
|
const {
|
|
337
332
|
responseHeaders,
|
|
338
333
|
value: response,
|
|
@@ -340,7 +335,7 @@ var MistralChatLanguageModel = class {
|
|
|
340
335
|
} = await postJsonToApi({
|
|
341
336
|
url: `${this.config.baseURL}/chat/completions`,
|
|
342
337
|
headers: combineHeaders(this.config.headers(), options.headers),
|
|
343
|
-
body
|
|
338
|
+
body,
|
|
344
339
|
failedResponseHandler: mistralFailedResponseHandler,
|
|
345
340
|
successfulResponseHandler: createJsonResponseHandler(
|
|
346
341
|
mistralChatResponseSchema
|
|
@@ -348,33 +343,40 @@ var MistralChatLanguageModel = class {
|
|
|
348
343
|
abortSignal: options.abortSignal,
|
|
349
344
|
fetch: this.config.fetch
|
|
350
345
|
});
|
|
351
|
-
const { messages: rawPrompt, ...rawSettings } = args;
|
|
352
346
|
const choice = response.choices[0];
|
|
347
|
+
const content = [];
|
|
353
348
|
let text = extractTextContent(choice.message.content);
|
|
354
|
-
const lastMessage =
|
|
349
|
+
const lastMessage = body.messages[body.messages.length - 1];
|
|
355
350
|
if (lastMessage.role === "assistant" && (text == null ? void 0 : text.startsWith(lastMessage.content))) {
|
|
356
351
|
text = text.slice(lastMessage.content.length);
|
|
357
352
|
}
|
|
353
|
+
if (text != null && text.length > 0) {
|
|
354
|
+
content.push({ type: "text", text });
|
|
355
|
+
}
|
|
356
|
+
if (choice.message.tool_calls != null) {
|
|
357
|
+
for (const toolCall of choice.message.tool_calls) {
|
|
358
|
+
content.push({
|
|
359
|
+
type: "tool-call",
|
|
360
|
+
toolCallType: "function",
|
|
361
|
+
toolCallId: toolCall.id,
|
|
362
|
+
toolName: toolCall.function.name,
|
|
363
|
+
args: toolCall.function.arguments
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
}
|
|
358
367
|
return {
|
|
359
|
-
|
|
360
|
-
toolCalls: (_a = choice.message.tool_calls) == null ? void 0 : _a.map((toolCall) => ({
|
|
361
|
-
toolCallType: "function",
|
|
362
|
-
toolCallId: toolCall.id,
|
|
363
|
-
toolName: toolCall.function.name,
|
|
364
|
-
args: toolCall.function.arguments
|
|
365
|
-
})),
|
|
368
|
+
content,
|
|
366
369
|
finishReason: mapMistralFinishReason(choice.finish_reason),
|
|
367
370
|
usage: {
|
|
368
|
-
|
|
369
|
-
|
|
371
|
+
inputTokens: response.usage.prompt_tokens,
|
|
372
|
+
outputTokens: response.usage.completion_tokens
|
|
370
373
|
},
|
|
371
|
-
|
|
372
|
-
|
|
374
|
+
request: { body },
|
|
375
|
+
response: {
|
|
376
|
+
...getResponseMetadata(response),
|
|
373
377
|
headers: responseHeaders,
|
|
374
378
|
body: rawResponse
|
|
375
379
|
},
|
|
376
|
-
request: { body: JSON.stringify(args) },
|
|
377
|
-
response: getResponseMetadata(response),
|
|
378
380
|
warnings
|
|
379
381
|
};
|
|
380
382
|
}
|
|
@@ -392,17 +394,19 @@ var MistralChatLanguageModel = class {
|
|
|
392
394
|
abortSignal: options.abortSignal,
|
|
393
395
|
fetch: this.config.fetch
|
|
394
396
|
});
|
|
395
|
-
const { messages: rawPrompt, ...rawSettings } = args;
|
|
396
397
|
let finishReason = "unknown";
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
398
|
+
const usage = {
|
|
399
|
+
inputTokens: void 0,
|
|
400
|
+
outputTokens: void 0
|
|
400
401
|
};
|
|
401
402
|
let chunkNumber = 0;
|
|
402
403
|
let trimLeadingSpace = false;
|
|
403
404
|
return {
|
|
404
405
|
stream: response.pipeThrough(
|
|
405
406
|
new TransformStream({
|
|
407
|
+
start(controller) {
|
|
408
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
409
|
+
},
|
|
406
410
|
transform(chunk, controller) {
|
|
407
411
|
if (!chunk.success) {
|
|
408
412
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
@@ -417,10 +421,8 @@ var MistralChatLanguageModel = class {
|
|
|
417
421
|
});
|
|
418
422
|
}
|
|
419
423
|
if (value.usage != null) {
|
|
420
|
-
usage =
|
|
421
|
-
|
|
422
|
-
completionTokens: value.usage.completion_tokens
|
|
423
|
-
};
|
|
424
|
+
usage.inputTokens = value.usage.prompt_tokens;
|
|
425
|
+
usage.outputTokens = value.usage.completion_tokens;
|
|
424
426
|
}
|
|
425
427
|
const choice = value.choices[0];
|
|
426
428
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
@@ -432,7 +434,7 @@ var MistralChatLanguageModel = class {
|
|
|
432
434
|
const delta = choice.delta;
|
|
433
435
|
const textContent = extractTextContent(delta.content);
|
|
434
436
|
if (chunkNumber <= 2) {
|
|
435
|
-
const lastMessage =
|
|
437
|
+
const lastMessage = body.messages[body.messages.length - 1];
|
|
436
438
|
if (lastMessage.role === "assistant" && textContent === lastMessage.content.trimEnd()) {
|
|
437
439
|
if (textContent.length < lastMessage.content.length) {
|
|
438
440
|
trimLeadingSpace = true;
|
|
@@ -442,8 +444,8 @@ var MistralChatLanguageModel = class {
|
|
|
442
444
|
}
|
|
443
445
|
if (textContent != null) {
|
|
444
446
|
controller.enqueue({
|
|
445
|
-
type: "text
|
|
446
|
-
|
|
447
|
+
type: "text",
|
|
448
|
+
text: trimLeadingSpace ? textContent.trimStart() : textContent
|
|
447
449
|
});
|
|
448
450
|
trimLeadingSpace = false;
|
|
449
451
|
}
|
|
@@ -471,10 +473,8 @@ var MistralChatLanguageModel = class {
|
|
|
471
473
|
}
|
|
472
474
|
})
|
|
473
475
|
),
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
request: { body: JSON.stringify(body) },
|
|
477
|
-
warnings
|
|
476
|
+
request: { body },
|
|
477
|
+
response: { headers: responseHeaders }
|
|
478
478
|
};
|
|
479
479
|
}
|
|
480
480
|
};
|
|
@@ -503,80 +503,80 @@ function extractTextContent(content) {
|
|
|
503
503
|
}
|
|
504
504
|
return textContent.length ? textContent.join("") : void 0;
|
|
505
505
|
}
|
|
506
|
-
var mistralContentSchema =
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
type:
|
|
512
|
-
text:
|
|
506
|
+
var mistralContentSchema = z3.union([
|
|
507
|
+
z3.string(),
|
|
508
|
+
z3.array(
|
|
509
|
+
z3.discriminatedUnion("type", [
|
|
510
|
+
z3.object({
|
|
511
|
+
type: z3.literal("text"),
|
|
512
|
+
text: z3.string()
|
|
513
513
|
}),
|
|
514
|
-
|
|
515
|
-
type:
|
|
516
|
-
image_url:
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
url:
|
|
520
|
-
detail:
|
|
514
|
+
z3.object({
|
|
515
|
+
type: z3.literal("image_url"),
|
|
516
|
+
image_url: z3.union([
|
|
517
|
+
z3.string(),
|
|
518
|
+
z3.object({
|
|
519
|
+
url: z3.string(),
|
|
520
|
+
detail: z3.string().nullable()
|
|
521
521
|
})
|
|
522
522
|
])
|
|
523
523
|
}),
|
|
524
|
-
|
|
525
|
-
type:
|
|
526
|
-
reference_ids:
|
|
524
|
+
z3.object({
|
|
525
|
+
type: z3.literal("reference"),
|
|
526
|
+
reference_ids: z3.array(z3.number())
|
|
527
527
|
})
|
|
528
528
|
])
|
|
529
529
|
)
|
|
530
530
|
]).nullish();
|
|
531
|
-
var mistralChatResponseSchema =
|
|
532
|
-
id:
|
|
533
|
-
created:
|
|
534
|
-
model:
|
|
535
|
-
choices:
|
|
536
|
-
|
|
537
|
-
message:
|
|
538
|
-
role:
|
|
531
|
+
var mistralChatResponseSchema = z3.object({
|
|
532
|
+
id: z3.string().nullish(),
|
|
533
|
+
created: z3.number().nullish(),
|
|
534
|
+
model: z3.string().nullish(),
|
|
535
|
+
choices: z3.array(
|
|
536
|
+
z3.object({
|
|
537
|
+
message: z3.object({
|
|
538
|
+
role: z3.literal("assistant"),
|
|
539
539
|
content: mistralContentSchema,
|
|
540
|
-
tool_calls:
|
|
541
|
-
|
|
542
|
-
id:
|
|
543
|
-
function:
|
|
540
|
+
tool_calls: z3.array(
|
|
541
|
+
z3.object({
|
|
542
|
+
id: z3.string(),
|
|
543
|
+
function: z3.object({ name: z3.string(), arguments: z3.string() })
|
|
544
544
|
})
|
|
545
545
|
).nullish()
|
|
546
546
|
}),
|
|
547
|
-
index:
|
|
548
|
-
finish_reason:
|
|
547
|
+
index: z3.number(),
|
|
548
|
+
finish_reason: z3.string().nullish()
|
|
549
549
|
})
|
|
550
550
|
),
|
|
551
|
-
object:
|
|
552
|
-
usage:
|
|
553
|
-
prompt_tokens:
|
|
554
|
-
completion_tokens:
|
|
551
|
+
object: z3.literal("chat.completion"),
|
|
552
|
+
usage: z3.object({
|
|
553
|
+
prompt_tokens: z3.number(),
|
|
554
|
+
completion_tokens: z3.number()
|
|
555
555
|
})
|
|
556
556
|
});
|
|
557
|
-
var mistralChatChunkSchema =
|
|
558
|
-
id:
|
|
559
|
-
created:
|
|
560
|
-
model:
|
|
561
|
-
choices:
|
|
562
|
-
|
|
563
|
-
delta:
|
|
564
|
-
role:
|
|
557
|
+
var mistralChatChunkSchema = z3.object({
|
|
558
|
+
id: z3.string().nullish(),
|
|
559
|
+
created: z3.number().nullish(),
|
|
560
|
+
model: z3.string().nullish(),
|
|
561
|
+
choices: z3.array(
|
|
562
|
+
z3.object({
|
|
563
|
+
delta: z3.object({
|
|
564
|
+
role: z3.enum(["assistant"]).optional(),
|
|
565
565
|
content: mistralContentSchema,
|
|
566
|
-
tool_calls:
|
|
567
|
-
|
|
568
|
-
id:
|
|
569
|
-
function:
|
|
566
|
+
tool_calls: z3.array(
|
|
567
|
+
z3.object({
|
|
568
|
+
id: z3.string(),
|
|
569
|
+
function: z3.object({ name: z3.string(), arguments: z3.string() })
|
|
570
570
|
})
|
|
571
571
|
).nullish()
|
|
572
572
|
}),
|
|
573
|
-
finish_reason:
|
|
574
|
-
index:
|
|
573
|
+
finish_reason: z3.string().nullish(),
|
|
574
|
+
index: z3.number()
|
|
575
575
|
})
|
|
576
576
|
),
|
|
577
|
-
usage:
|
|
578
|
-
prompt_tokens:
|
|
579
|
-
completion_tokens:
|
|
577
|
+
usage: z3.object({
|
|
578
|
+
prompt_tokens: z3.number(),
|
|
579
|
+
completion_tokens: z3.number()
|
|
580
580
|
}).nullish()
|
|
581
581
|
});
|
|
582
582
|
|
|
@@ -589,10 +589,10 @@ import {
|
|
|
589
589
|
createJsonResponseHandler as createJsonResponseHandler2,
|
|
590
590
|
postJsonToApi as postJsonToApi2
|
|
591
591
|
} from "@ai-sdk/provider-utils";
|
|
592
|
-
import { z as
|
|
592
|
+
import { z as z4 } from "zod";
|
|
593
593
|
var MistralEmbeddingModel = class {
|
|
594
594
|
constructor(modelId, settings, config) {
|
|
595
|
-
this.specificationVersion = "
|
|
595
|
+
this.specificationVersion = "v2";
|
|
596
596
|
this.modelId = modelId;
|
|
597
597
|
this.settings = settings;
|
|
598
598
|
this.config = config;
|
|
@@ -621,7 +621,11 @@ var MistralEmbeddingModel = class {
|
|
|
621
621
|
values
|
|
622
622
|
});
|
|
623
623
|
}
|
|
624
|
-
const {
|
|
624
|
+
const {
|
|
625
|
+
responseHeaders,
|
|
626
|
+
value: response,
|
|
627
|
+
rawValue
|
|
628
|
+
} = await postJsonToApi2({
|
|
625
629
|
url: `${this.config.baseURL}/embeddings`,
|
|
626
630
|
headers: combineHeaders2(this.config.headers(), headers),
|
|
627
631
|
body: {
|
|
@@ -639,13 +643,13 @@ var MistralEmbeddingModel = class {
|
|
|
639
643
|
return {
|
|
640
644
|
embeddings: response.data.map((item) => item.embedding),
|
|
641
645
|
usage: response.usage ? { tokens: response.usage.prompt_tokens } : void 0,
|
|
642
|
-
|
|
646
|
+
response: { headers: responseHeaders, body: rawValue }
|
|
643
647
|
};
|
|
644
648
|
}
|
|
645
649
|
};
|
|
646
|
-
var MistralTextEmbeddingResponseSchema =
|
|
647
|
-
data:
|
|
648
|
-
usage:
|
|
650
|
+
var MistralTextEmbeddingResponseSchema = z4.object({
|
|
651
|
+
data: z4.array(z4.object({ embedding: z4.array(z4.number()) })),
|
|
652
|
+
usage: z4.object({ prompt_tokens: z4.number() }).nullish()
|
|
649
653
|
});
|
|
650
654
|
|
|
651
655
|
// src/mistral-provider.ts
|
|
@@ -660,7 +664,7 @@ function createMistral(options = {}) {
|
|
|
660
664
|
})}`,
|
|
661
665
|
...options.headers
|
|
662
666
|
});
|
|
663
|
-
const createChatModel = (modelId
|
|
667
|
+
const createChatModel = (modelId) => new MistralChatLanguageModel(modelId, {
|
|
664
668
|
provider: "mistral.chat",
|
|
665
669
|
baseURL,
|
|
666
670
|
headers: getHeaders,
|
|
@@ -672,19 +676,22 @@ function createMistral(options = {}) {
|
|
|
672
676
|
headers: getHeaders,
|
|
673
677
|
fetch: options.fetch
|
|
674
678
|
});
|
|
675
|
-
const provider = function(modelId
|
|
679
|
+
const provider = function(modelId) {
|
|
676
680
|
if (new.target) {
|
|
677
681
|
throw new Error(
|
|
678
682
|
"The Mistral model function cannot be called with the new keyword."
|
|
679
683
|
);
|
|
680
684
|
}
|
|
681
|
-
return createChatModel(modelId
|
|
685
|
+
return createChatModel(modelId);
|
|
682
686
|
};
|
|
683
687
|
provider.languageModel = createChatModel;
|
|
684
688
|
provider.chat = createChatModel;
|
|
685
689
|
provider.embedding = createEmbeddingModel;
|
|
686
690
|
provider.textEmbedding = createEmbeddingModel;
|
|
687
691
|
provider.textEmbeddingModel = createEmbeddingModel;
|
|
692
|
+
provider.imageModel = (modelId) => {
|
|
693
|
+
throw new NoSuchModelError({ modelId, modelType: "imageModel" });
|
|
694
|
+
};
|
|
688
695
|
return provider;
|
|
689
696
|
}
|
|
690
697
|
var mistral = createMistral();
|