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