@ai-sdk/mistral 4.0.68 → 4.0.69
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 +9 -0
- package/dist/index.d.ts +6 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +36 -35
- package/dist/index.js.map +1 -1
- package/docs/20-mistral.mdx +48 -4
- package/package.json +2 -2
- package/src/mistral-chat-language-model-options.ts +47 -42
- package/src/mistral-chat-language-model.ts +104 -87
- package/src/mistral-conversation-language-model-options.ts +4 -2
- package/src/mistral-conversation-language-model.ts +107 -90
- package/src/mistral-embedding-model.ts +10 -4
- package/src/mistral-error.ts +18 -9
- package/src/mistral-speech-model-options.ts +18 -9
- package/src/mistral-speech-model.ts +9 -3
- package/src/mistral-transcription-model.ts +32 -26
package/docs/20-mistral.mdx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: Mistral AI
|
|
3
|
-
description: Learn how to use Mistral.
|
|
3
|
+
description: Learn how to use Mistral AI with AI Gateway and the direct provider.
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# Mistral AI Provider
|
|
@@ -8,12 +8,56 @@ description: Learn how to use Mistral.
|
|
|
8
8
|
The [Mistral AI](https://mistral.ai/) provider contains language model,
|
|
9
9
|
embedding model, speech model, and transcription model support for Mistral
|
|
10
10
|
APIs.
|
|
11
|
+
You can use Mistral AI models through [AI Gateway](/providers/ai-sdk-providers/ai-gateway) (recommended) or connect directly with `@ai-sdk/mistral`.
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
```ts provider="mistral" model="mistral/mistral-medium-3.5"
|
|
14
|
+
import { generateText } from 'ai';
|
|
15
|
+
__PROVIDER_IMPORT__;
|
|
16
|
+
|
|
17
|
+
const { text } = await generateText({
|
|
18
|
+
model: __MODEL__,
|
|
19
|
+
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
console.log(text);
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Using AI Gateway
|
|
26
|
+
|
|
27
|
+
The AI SDK uses AI Gateway by default for model strings. Gateway provides access to models across providers through a single API key, with usage and cost tracking. Install `ai`:
|
|
13
28
|
|
|
14
|
-
|
|
29
|
+
<InstallPackages packages="ai" />
|
|
30
|
+
|
|
31
|
+
Set an [AI Gateway API key](https://vercel.com/docs/ai-gateway/getting-started) in your environment:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
export AI_GATEWAY_API_KEY=your_api_key_here
|
|
35
|
+
```
|
|
15
36
|
|
|
16
|
-
|
|
37
|
+
Vercel deployments can also use [OIDC authentication](/providers/ai-sdk-providers/ai-gateway#oidc-authentication-vercel-deployments).
|
|
38
|
+
See the [AI Gateway provider documentation](/providers/ai-sdk-providers/ai-gateway) for configuration and the [model catalog](https://vercel.com/ai-gateway/models) for available models.
|
|
39
|
+
|
|
40
|
+
Gateway feature and provider-option support depends on the model and the provider serving the request. See [Gateway provider options](https://vercel.com/docs/ai-gateway/models-and-providers/provider-options).
|
|
41
|
+
|
|
42
|
+
## Using `@ai-sdk/mistral`
|
|
43
|
+
|
|
44
|
+
For direct access to Mistral AI, install `ai` and `@ai-sdk/mistral`:
|
|
45
|
+
|
|
46
|
+
<InstallPackages packages="ai @ai-sdk/mistral" />
|
|
47
|
+
|
|
48
|
+
Set your Mistral AI API credential in the `MISTRAL_API_KEY` environment variable:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
export MISTRAL_API_KEY=your_api_key_here
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
AI Gateway does not expose every API in `@ai-sdk/mistral`. Use the direct provider for provider-specific APIs or custom connection settings.
|
|
55
|
+
|
|
56
|
+
<Note>
|
|
57
|
+
The following sections document the direct `@ai-sdk/mistral` provider,
|
|
58
|
+
including its model factories and settings. Gateway model and feature
|
|
59
|
+
availability can differ.
|
|
60
|
+
</Note>
|
|
17
61
|
|
|
18
62
|
## Provider Instance
|
|
19
63
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/mistral",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.69",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@ai-sdk/provider": "4.0.31",
|
|
33
|
-
"@ai-sdk/provider-utils": "5.0.
|
|
33
|
+
"@ai-sdk/provider-utils": "5.0.65"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@ai-sdk/test-server": "2.0.4",
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { lazySchema, zodSchema } from '@ai-sdk/provider-utils';
|
|
1
2
|
import { z } from 'zod/v4';
|
|
2
3
|
|
|
3
4
|
// https://docs.mistral.ai/models
|
|
@@ -37,54 +38,58 @@ export type MistralChatModelId =
|
|
|
37
38
|
| 'zai-glm-5-2'
|
|
38
39
|
| (string & {});
|
|
39
40
|
|
|
40
|
-
export const mistralLanguageModelChatOptions =
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
41
|
+
export const mistralLanguageModelChatOptions = () =>
|
|
42
|
+
z.object({
|
|
43
|
+
/**
|
|
44
|
+
* Whether to inject a safety prompt before all conversations.
|
|
45
|
+
*
|
|
46
|
+
* Defaults to `false`.
|
|
47
|
+
*/
|
|
48
|
+
safePrompt: z.boolean().optional(),
|
|
47
49
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
+
documentImageLimit: z.number().optional(),
|
|
51
|
+
documentPageLimit: z.number().optional(),
|
|
50
52
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
/**
|
|
54
|
+
* Whether to use structured outputs.
|
|
55
|
+
*
|
|
56
|
+
* @default true
|
|
57
|
+
*/
|
|
58
|
+
structuredOutputs: z.boolean().optional(),
|
|
57
59
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Whether to use strict JSON schema validation.
|
|
62
|
+
*
|
|
63
|
+
* @default false
|
|
64
|
+
*/
|
|
65
|
+
strictJsonSchema: z.boolean().optional(),
|
|
64
66
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
67
|
+
/**
|
|
68
|
+
* Whether to enable parallel function calling during tool use.
|
|
69
|
+
* When set to false, the model will use at most one tool per response.
|
|
70
|
+
*
|
|
71
|
+
* @default true
|
|
72
|
+
*/
|
|
73
|
+
parallelToolCalls: z.boolean().optional(),
|
|
72
74
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
75
|
+
/**
|
|
76
|
+
* A stable identifier used to route requests with shared prompt prefixes to
|
|
77
|
+
* the same cache.
|
|
78
|
+
*/
|
|
79
|
+
promptCacheKey: z.string().optional(),
|
|
78
80
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
});
|
|
81
|
+
/**
|
|
82
|
+
* Controls the reasoning effort for models that support adjustable reasoning.
|
|
83
|
+
*
|
|
84
|
+
* - `'high'`: Enable reasoning
|
|
85
|
+
* - `'none'`: Disable reasoning
|
|
86
|
+
*/
|
|
87
|
+
reasoningEffort: z.enum(['high', 'none']).optional(),
|
|
88
|
+
});
|
|
89
|
+
export const mistralLanguageModelChatOptionsValidator = lazySchema(() =>
|
|
90
|
+
zodSchema(mistralLanguageModelChatOptions()),
|
|
91
|
+
);
|
|
87
92
|
|
|
88
93
|
export type MistralLanguageModelChatOptions = z.infer<
|
|
89
|
-
typeof mistralLanguageModelChatOptions
|
|
94
|
+
ReturnType<typeof mistralLanguageModelChatOptions>
|
|
90
95
|
>;
|
|
@@ -9,6 +9,9 @@ import type {
|
|
|
9
9
|
SharedV4Warning,
|
|
10
10
|
} from '@ai-sdk/provider';
|
|
11
11
|
import {
|
|
12
|
+
lazySchema,
|
|
13
|
+
zodSchema,
|
|
14
|
+
type InferSchema,
|
|
12
15
|
combineHeaders,
|
|
13
16
|
createEventSourceResponseHandler,
|
|
14
17
|
createJsonResponseHandler,
|
|
@@ -36,7 +39,7 @@ import { convertToMistralChatMessages } from './convert-to-mistral-chat-messages
|
|
|
36
39
|
import { getResponseMetadata } from './get-response-metadata';
|
|
37
40
|
import { mapMistralFinishReason } from './map-mistral-finish-reason';
|
|
38
41
|
import {
|
|
39
|
-
|
|
42
|
+
mistralLanguageModelChatOptionsValidator,
|
|
40
43
|
type MistralChatModelId,
|
|
41
44
|
} from './mistral-chat-language-model-options';
|
|
42
45
|
import { mistralFailedResponseHandler } from './mistral-error';
|
|
@@ -127,7 +130,7 @@ export class MistralChatLanguageModel implements LanguageModelV4 {
|
|
|
127
130
|
(await parseProviderOptions({
|
|
128
131
|
provider: 'mistral',
|
|
129
132
|
providerOptions,
|
|
130
|
-
schema:
|
|
133
|
+
schema: mistralLanguageModelChatOptionsValidator,
|
|
131
134
|
})) ?? {};
|
|
132
135
|
|
|
133
136
|
if (topK != null) {
|
|
@@ -365,7 +368,7 @@ export class MistralChatLanguageModel implements LanguageModelV4 {
|
|
|
365
368
|
return {
|
|
366
369
|
stream: response.pipeThrough(
|
|
367
370
|
new TransformStream<
|
|
368
|
-
ParseResult<
|
|
371
|
+
ParseResult<InferSchema<typeof mistralChatChunkSchema>>,
|
|
369
372
|
LanguageModelV4StreamPart
|
|
370
373
|
>({
|
|
371
374
|
start(controller) {
|
|
@@ -505,7 +508,9 @@ function extractReasoningContent(
|
|
|
505
508
|
.join('');
|
|
506
509
|
}
|
|
507
510
|
|
|
508
|
-
function extractTextContent(
|
|
511
|
+
function extractTextContent(
|
|
512
|
+
content: z.infer<ReturnType<typeof mistralContentSchema>>,
|
|
513
|
+
) {
|
|
509
514
|
if (typeof content === 'string') {
|
|
510
515
|
return content;
|
|
511
516
|
}
|
|
@@ -538,98 +543,110 @@ function extractTextContent(content: z.infer<typeof mistralContentSchema>) {
|
|
|
538
543
|
return textContent.length ? textContent.join('') : undefined;
|
|
539
544
|
}
|
|
540
545
|
|
|
541
|
-
const mistralContentSchema =
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
z.
|
|
546
|
-
z.
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
z.
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
z.
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
546
|
+
const mistralContentSchema = () =>
|
|
547
|
+
z
|
|
548
|
+
.union([
|
|
549
|
+
z.string(),
|
|
550
|
+
z.array(
|
|
551
|
+
z.discriminatedUnion('type', [
|
|
552
|
+
z.object({
|
|
553
|
+
type: z.literal('text'),
|
|
554
|
+
text: z.string(),
|
|
555
|
+
}),
|
|
556
|
+
z.object({
|
|
557
|
+
type: z.literal('image_url'),
|
|
558
|
+
image_url: z.union([
|
|
559
|
+
z.string(),
|
|
560
|
+
z.object({
|
|
561
|
+
url: z.string(),
|
|
562
|
+
detail: z.string().nullable(),
|
|
563
|
+
}),
|
|
564
|
+
]),
|
|
565
|
+
}),
|
|
566
|
+
z.object({
|
|
567
|
+
type: z.literal('reference'),
|
|
568
|
+
reference_ids: z.array(z.union([z.string(), z.number()])),
|
|
569
|
+
}),
|
|
570
|
+
z.object({
|
|
571
|
+
type: z.literal('thinking'),
|
|
572
|
+
thinking: z.array(
|
|
573
|
+
z.object({
|
|
574
|
+
type: z.literal('text'),
|
|
575
|
+
text: z.string(),
|
|
576
|
+
}),
|
|
577
|
+
),
|
|
578
|
+
}),
|
|
579
|
+
]),
|
|
580
|
+
),
|
|
581
|
+
])
|
|
582
|
+
.nullish();
|
|
577
583
|
|
|
578
584
|
// limited version of the schema, focussed on what is needed for the implementation
|
|
579
585
|
// this approach limits breakages when the API changes and increases efficiency
|
|
580
|
-
const mistralChatResponseSchema =
|
|
581
|
-
|
|
582
|
-
created: z.number().nullish(),
|
|
583
|
-
model: z.string().nullish(),
|
|
584
|
-
choices: z.array(
|
|
586
|
+
const mistralChatResponseSchema = lazySchema(() =>
|
|
587
|
+
zodSchema(
|
|
585
588
|
z.object({
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
589
|
+
id: z.string().nullish(),
|
|
590
|
+
created: z.number().nullish(),
|
|
591
|
+
model: z.string().nullish(),
|
|
592
|
+
choices: z.array(
|
|
593
|
+
z.object({
|
|
594
|
+
message: z.object({
|
|
595
|
+
role: z.literal('assistant'),
|
|
596
|
+
content: mistralContentSchema(),
|
|
597
|
+
tool_calls: z
|
|
598
|
+
.array(
|
|
599
|
+
z.object({
|
|
600
|
+
id: z.string(),
|
|
601
|
+
function: z.object({
|
|
602
|
+
name: z.string(),
|
|
603
|
+
arguments: z.string(),
|
|
604
|
+
}),
|
|
605
|
+
}),
|
|
606
|
+
)
|
|
607
|
+
.nullish(),
|
|
608
|
+
}),
|
|
609
|
+
index: z.number(),
|
|
610
|
+
finish_reason: z.string().nullish(),
|
|
611
|
+
}),
|
|
612
|
+
),
|
|
613
|
+
object: z.literal('chat.completion'),
|
|
614
|
+
usage: mistralUsageSchema,
|
|
600
615
|
}),
|
|
601
616
|
),
|
|
602
|
-
|
|
603
|
-
usage: mistralUsageSchema,
|
|
604
|
-
});
|
|
617
|
+
);
|
|
605
618
|
|
|
606
619
|
// limited version of the schema, focussed on what is needed for the implementation
|
|
607
620
|
// this approach limits breakages when the API changes and increases efficiency
|
|
608
|
-
const mistralChatChunkSchema =
|
|
609
|
-
|
|
610
|
-
created: z.number().nullish(),
|
|
611
|
-
model: z.string().nullish(),
|
|
612
|
-
choices: z.array(
|
|
621
|
+
const mistralChatChunkSchema = lazySchema(() =>
|
|
622
|
+
zodSchema(
|
|
613
623
|
z.object({
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
624
|
+
id: z.string().nullish(),
|
|
625
|
+
created: z.number().nullish(),
|
|
626
|
+
model: z.string().nullish(),
|
|
627
|
+
choices: z.array(
|
|
628
|
+
z.object({
|
|
629
|
+
delta: z.object({
|
|
630
|
+
role: z.enum(['assistant']).optional(),
|
|
631
|
+
content: mistralContentSchema(),
|
|
632
|
+
tool_calls: z
|
|
633
|
+
.array(
|
|
634
|
+
z.object({
|
|
635
|
+
index: z.number().nullish(),
|
|
636
|
+
id: z.string().nullish(),
|
|
637
|
+
function: z.object({
|
|
638
|
+
name: z.string().nullish(),
|
|
639
|
+
arguments: z.string().nullish(),
|
|
640
|
+
}),
|
|
641
|
+
}),
|
|
642
|
+
)
|
|
643
|
+
.nullish(),
|
|
644
|
+
}),
|
|
645
|
+
finish_reason: z.string().nullish(),
|
|
646
|
+
index: z.number(),
|
|
647
|
+
}),
|
|
648
|
+
),
|
|
649
|
+
usage: mistralUsageSchema.nullish(),
|
|
632
650
|
}),
|
|
633
651
|
),
|
|
634
|
-
|
|
635
|
-
});
|
|
652
|
+
);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { lazySchema, zodSchema } from '@ai-sdk/provider-utils';
|
|
1
2
|
import {
|
|
2
3
|
mistralLanguageModelChatOptions,
|
|
3
4
|
type MistralChatModelId,
|
|
@@ -6,8 +7,9 @@ import {
|
|
|
6
7
|
|
|
7
8
|
export type MistralConversationModelId = MistralChatModelId;
|
|
8
9
|
|
|
9
|
-
export const mistralLanguageModelConversationOptions =
|
|
10
|
-
mistralLanguageModelChatOptions
|
|
10
|
+
export const mistralLanguageModelConversationOptions = lazySchema(() =>
|
|
11
|
+
zodSchema(mistralLanguageModelChatOptions()),
|
|
12
|
+
);
|
|
11
13
|
|
|
12
14
|
export type MistralLanguageModelConversationOptions =
|
|
13
15
|
MistralLanguageModelChatOptions;
|