@ai-sdk/mistral 3.0.48 → 3.0.50
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 +13 -0
- package/README.md +1 -1
- package/dist/index.d.mts +16 -2
- package/dist/index.d.ts +16 -2
- package/dist/index.js +144 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +145 -1
- package/dist/index.mjs.map +1 -1
- package/docs/20-mistral.mdx +71 -1
- package/package.json +2 -2
- package/src/index.ts +4 -0
- package/src/mistral-provider.ts +23 -0
- package/src/mistral-speech-model-options.ts +16 -0
- package/src/mistral-speech-model.ts +162 -0
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import type { SharedV3Warning, SpeechModelV3 } from '@ai-sdk/provider';
|
|
2
|
+
import {
|
|
3
|
+
combineHeaders,
|
|
4
|
+
createJsonResponseHandler,
|
|
5
|
+
parseProviderOptions,
|
|
6
|
+
postToApi,
|
|
7
|
+
type FetchFunction,
|
|
8
|
+
} from '@ai-sdk/provider-utils';
|
|
9
|
+
import { z } from 'zod/v4';
|
|
10
|
+
import { mistralFailedResponseHandler } from './mistral-error';
|
|
11
|
+
import {
|
|
12
|
+
mistralSpeechModelOptions,
|
|
13
|
+
type MistralSpeechModelId,
|
|
14
|
+
} from './mistral-speech-model-options';
|
|
15
|
+
|
|
16
|
+
interface MistralSpeechModelConfig {
|
|
17
|
+
provider: string;
|
|
18
|
+
baseURL: string;
|
|
19
|
+
headers?: () => Record<string, string | undefined>;
|
|
20
|
+
fetch?: FetchFunction;
|
|
21
|
+
_internal?: {
|
|
22
|
+
currentDate?: () => Date;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type MistralSpeechOutputFormat = 'pcm' | 'wav' | 'mp3' | 'flac' | 'opus';
|
|
27
|
+
|
|
28
|
+
export class MistralSpeechModel implements SpeechModelV3 {
|
|
29
|
+
readonly specificationVersion = 'v3';
|
|
30
|
+
|
|
31
|
+
get provider(): string {
|
|
32
|
+
return this.config.provider;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
constructor(
|
|
36
|
+
readonly modelId: MistralSpeechModelId,
|
|
37
|
+
private readonly config: MistralSpeechModelConfig,
|
|
38
|
+
) {}
|
|
39
|
+
|
|
40
|
+
private async getArgs({
|
|
41
|
+
text,
|
|
42
|
+
voice,
|
|
43
|
+
outputFormat = 'mp3',
|
|
44
|
+
instructions,
|
|
45
|
+
speed,
|
|
46
|
+
language,
|
|
47
|
+
providerOptions,
|
|
48
|
+
}: Parameters<SpeechModelV3['doGenerate']>[0]) {
|
|
49
|
+
const warnings: SharedV3Warning[] = [];
|
|
50
|
+
const mistralOptions = await parseProviderOptions({
|
|
51
|
+
provider: 'mistral',
|
|
52
|
+
providerOptions,
|
|
53
|
+
schema: mistralSpeechModelOptions,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
let responseFormat: MistralSpeechOutputFormat = 'mp3';
|
|
57
|
+
if (['pcm', 'wav', 'mp3', 'flac', 'opus'].includes(outputFormat)) {
|
|
58
|
+
responseFormat = outputFormat as MistralSpeechOutputFormat;
|
|
59
|
+
} else {
|
|
60
|
+
warnings.push({
|
|
61
|
+
type: 'unsupported',
|
|
62
|
+
feature: 'outputFormat',
|
|
63
|
+
details: `Unsupported output format: ${outputFormat}. Using mp3 instead.`,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (instructions != null) {
|
|
68
|
+
warnings.push({
|
|
69
|
+
type: 'unsupported',
|
|
70
|
+
feature: 'instructions',
|
|
71
|
+
details:
|
|
72
|
+
'Mistral speech models do not support the `instructions` option. ' +
|
|
73
|
+
'Use a reference audio clip to guide delivery.',
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (speed != null) {
|
|
78
|
+
warnings.push({
|
|
79
|
+
type: 'unsupported',
|
|
80
|
+
feature: 'speed',
|
|
81
|
+
details:
|
|
82
|
+
'Mistral speech models do not support the `speed` option. It was ignored.',
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (language != null) {
|
|
87
|
+
warnings.push({
|
|
88
|
+
type: 'unsupported',
|
|
89
|
+
feature: 'language',
|
|
90
|
+
details:
|
|
91
|
+
'Mistral speech models do not support the `language` option. ' +
|
|
92
|
+
'Language is inferred from the input text and voice.',
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const refAudio = mistralOptions?.refAudio;
|
|
97
|
+
const requestBody = {
|
|
98
|
+
model: this.modelId,
|
|
99
|
+
input: text,
|
|
100
|
+
voice_id: refAudio == null ? voice : undefined,
|
|
101
|
+
ref_audio: refAudio,
|
|
102
|
+
response_format: responseFormat,
|
|
103
|
+
stream: false,
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const requestBodyValues = {
|
|
107
|
+
...requestBody,
|
|
108
|
+
ref_audio: refAudio == null ? undefined : '[redacted]',
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
return { requestBody, requestBodyValues, warnings };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async doGenerate(
|
|
115
|
+
options: Parameters<SpeechModelV3['doGenerate']>[0],
|
|
116
|
+
): Promise<Awaited<ReturnType<SpeechModelV3['doGenerate']>>> {
|
|
117
|
+
const currentDate = this.config._internal?.currentDate?.() ?? new Date();
|
|
118
|
+
const { requestBody, requestBodyValues, warnings } =
|
|
119
|
+
await this.getArgs(options);
|
|
120
|
+
|
|
121
|
+
const {
|
|
122
|
+
value: response,
|
|
123
|
+
responseHeaders,
|
|
124
|
+
rawValue: rawResponse,
|
|
125
|
+
} = await postToApi({
|
|
126
|
+
url: `${this.config.baseURL}/audio/speech`,
|
|
127
|
+
headers: combineHeaders(
|
|
128
|
+
{ 'Content-Type': 'application/json' },
|
|
129
|
+
this.config.headers?.(),
|
|
130
|
+
options.headers,
|
|
131
|
+
),
|
|
132
|
+
body: {
|
|
133
|
+
content: JSON.stringify(requestBody),
|
|
134
|
+
values: requestBodyValues,
|
|
135
|
+
},
|
|
136
|
+
failedResponseHandler: mistralFailedResponseHandler,
|
|
137
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
138
|
+
mistralSpeechResponseSchema,
|
|
139
|
+
),
|
|
140
|
+
abortSignal: options.abortSignal,
|
|
141
|
+
fetch: this.config.fetch,
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
return {
|
|
145
|
+
audio: response.audio_data,
|
|
146
|
+
warnings,
|
|
147
|
+
request: {
|
|
148
|
+
body: JSON.stringify(requestBodyValues),
|
|
149
|
+
},
|
|
150
|
+
response: {
|
|
151
|
+
timestamp: currentDate,
|
|
152
|
+
modelId: this.modelId,
|
|
153
|
+
headers: responseHeaders,
|
|
154
|
+
body: rawResponse,
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const mistralSpeechResponseSchema = z.object({
|
|
161
|
+
audio_data: z.string(),
|
|
162
|
+
});
|