@ai-sdk/mistral 4.0.54 → 4.0.55
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 +11 -0
- package/dist/index.d.ts +108 -103
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1068 -1361
- package/dist/index.js.map +1 -1
- package/package.json +8 -8
- package/src/map-mistral-finish-reason.ts +2 -0
- package/src/mistral-chat-language-model.ts +5 -2
package/dist/index.js
CHANGED
|
@@ -1,1395 +1,1102 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
NoSuchModelError
|
|
4
|
-
} from "@ai-sdk/provider";
|
|
5
|
-
import {
|
|
6
|
-
loadApiKey,
|
|
7
|
-
withoutTrailingSlash,
|
|
8
|
-
withUserAgentSuffix
|
|
9
|
-
} from "@ai-sdk/provider-utils";
|
|
10
|
-
|
|
11
|
-
// src/mistral-chat-language-model.ts
|
|
12
|
-
import {
|
|
13
|
-
combineHeaders,
|
|
14
|
-
createEventSourceResponseHandler,
|
|
15
|
-
createJsonResponseHandler,
|
|
16
|
-
generateId,
|
|
17
|
-
injectJsonInstructionIntoMessages,
|
|
18
|
-
isCustomReasoning,
|
|
19
|
-
mapReasoningToProviderEffort,
|
|
20
|
-
parseProviderOptions,
|
|
21
|
-
postJsonToApi,
|
|
22
|
-
serializeModelOptions,
|
|
23
|
-
StreamingToolCallTracker,
|
|
24
|
-
WORKFLOW_SERIALIZE,
|
|
25
|
-
WORKFLOW_DESERIALIZE
|
|
26
|
-
} from "@ai-sdk/provider-utils";
|
|
27
|
-
import { z as z4 } from "zod/v4";
|
|
28
|
-
|
|
29
|
-
// src/convert-mistral-usage.ts
|
|
30
|
-
import { createNullLanguageModelUsage } from "@ai-sdk/provider-utils";
|
|
1
|
+
import { InvalidArgumentError, NoSuchModelError, TooManyEmbeddingValuesForCallError, UnsupportedFunctionalityError } from "@ai-sdk/provider";
|
|
2
|
+
import { StreamingToolCallTracker, WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE, combineHeaders, convertBase64ToUint8Array, convertToBase64, createEventSourceResponseHandler, createJsonErrorResponseHandler, createJsonResponseHandler, createLanguageModelResponseMetadata as getResponseMetadata, createNullLanguageModelUsage, generateId, getTopLevelMediaType, injectJsonInstructionIntoMessages, isCustomReasoning, lazySchema, loadApiKey, mapReasoningToProviderEffort, mediaTypeToExtension, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, resolveFullMediaType, serializeModelOptions, withUserAgentSuffix, withoutTrailingSlash, zodSchema } from "@ai-sdk/provider-utils";
|
|
31
3
|
import { z } from "zod/v4";
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
4
|
+
//#region src/convert-mistral-usage.ts
|
|
5
|
+
const mistralPromptTokensDetailsSchema = z.object({
|
|
6
|
+
messages: z.array(z.json()).nullish(),
|
|
7
|
+
cached_tokens: z.number().nullish(),
|
|
8
|
+
audio_tokens: z.number().nullish()
|
|
36
9
|
}).catchall(z.json());
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
prompt_token_details: mistralPromptTokensDetailsSchema.nullish(),
|
|
50
|
-
completion_tokens_details: mistralCompletionTokensDetailsSchema.nullish()
|
|
10
|
+
const mistralCompletionTokensDetailsSchema = z.object({ reasoning_tokens: z.number().nullish() }).catchall(z.json());
|
|
11
|
+
const mistralUsageSchema = z.object({
|
|
12
|
+
prompt_tokens: z.number(),
|
|
13
|
+
completion_tokens: z.number(),
|
|
14
|
+
total_tokens: z.number(),
|
|
15
|
+
prompt_audio_seconds: z.number().nullish(),
|
|
16
|
+
request_count: z.number().nullish(),
|
|
17
|
+
service_tier: z.string().nullish(),
|
|
18
|
+
num_cached_tokens: z.number().nullish(),
|
|
19
|
+
prompt_tokens_details: mistralPromptTokensDetailsSchema.nullish(),
|
|
20
|
+
prompt_token_details: mistralPromptTokensDetailsSchema.nullish(),
|
|
21
|
+
completion_tokens_details: mistralCompletionTokensDetailsSchema.nullish()
|
|
51
22
|
}).catchall(z.json());
|
|
52
23
|
function convertMistralUsage(usage) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
raw: usage
|
|
72
|
-
};
|
|
24
|
+
if (usage == null) return createNullLanguageModelUsage();
|
|
25
|
+
const promptTokens = usage.prompt_tokens;
|
|
26
|
+
const completionTokens = usage.completion_tokens;
|
|
27
|
+
const cacheReadTokens = usage.num_cached_tokens ?? usage.prompt_tokens_details?.cached_tokens ?? usage.prompt_token_details?.cached_tokens ?? 0;
|
|
28
|
+
return {
|
|
29
|
+
inputTokens: {
|
|
30
|
+
total: promptTokens,
|
|
31
|
+
noCache: promptTokens - cacheReadTokens,
|
|
32
|
+
cacheRead: cacheReadTokens || void 0,
|
|
33
|
+
cacheWrite: void 0
|
|
34
|
+
},
|
|
35
|
+
outputTokens: {
|
|
36
|
+
total: completionTokens,
|
|
37
|
+
text: completionTokens,
|
|
38
|
+
reasoning: void 0
|
|
39
|
+
},
|
|
40
|
+
raw: usage
|
|
41
|
+
};
|
|
73
42
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
import {
|
|
77
|
-
UnsupportedFunctionalityError
|
|
78
|
-
} from "@ai-sdk/provider";
|
|
79
|
-
import {
|
|
80
|
-
convertToBase64,
|
|
81
|
-
getTopLevelMediaType,
|
|
82
|
-
resolveFullMediaType
|
|
83
|
-
} from "@ai-sdk/provider-utils";
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/convert-to-mistral-chat-messages.ts
|
|
84
45
|
function formatFileUrl({ part }) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (part.data.type === "data") {
|
|
89
|
-
return `data:${resolveFullMediaType({ part })};base64,${convertToBase64(part.data.data)}`;
|
|
90
|
-
}
|
|
91
|
-
throw new UnsupportedFunctionalityError({
|
|
92
|
-
functionality: `file part data type ${part.data.type}`
|
|
93
|
-
});
|
|
46
|
+
if (part.data.type === "url") return part.data.url.toString();
|
|
47
|
+
if (part.data.type === "data") return `data:${resolveFullMediaType({ part })};base64,${convertToBase64(part.data.data)}`;
|
|
48
|
+
throw new UnsupportedFunctionalityError({ functionality: `file part data type ${part.data.type}` });
|
|
94
49
|
}
|
|
95
50
|
function convertToMistralChatMessages(prompt) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
const output = toolResponse.output;
|
|
212
|
-
let contentValue;
|
|
213
|
-
switch (output.type) {
|
|
214
|
-
case "text":
|
|
215
|
-
case "error-text":
|
|
216
|
-
contentValue = output.value;
|
|
217
|
-
break;
|
|
218
|
-
case "execution-denied":
|
|
219
|
-
contentValue = output.reason ?? "Tool call execution denied.";
|
|
220
|
-
break;
|
|
221
|
-
case "content":
|
|
222
|
-
case "json":
|
|
223
|
-
case "error-json":
|
|
224
|
-
contentValue = JSON.stringify(output.value);
|
|
225
|
-
break;
|
|
226
|
-
}
|
|
227
|
-
messages.push({
|
|
228
|
-
role: "tool",
|
|
229
|
-
name: toolResponse.toolName,
|
|
230
|
-
tool_call_id: toolResponse.toolCallId,
|
|
231
|
-
content: contentValue
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
break;
|
|
235
|
-
}
|
|
236
|
-
default: {
|
|
237
|
-
const _exhaustiveCheck = role;
|
|
238
|
-
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
return messages;
|
|
51
|
+
const messages = [];
|
|
52
|
+
for (let i = 0; i < prompt.length; i++) {
|
|
53
|
+
const { role, content } = prompt[i];
|
|
54
|
+
const isLastMessage = i === prompt.length - 1;
|
|
55
|
+
switch (role) {
|
|
56
|
+
case "system":
|
|
57
|
+
messages.push({
|
|
58
|
+
role: "system",
|
|
59
|
+
content
|
|
60
|
+
});
|
|
61
|
+
break;
|
|
62
|
+
case "user":
|
|
63
|
+
messages.push({
|
|
64
|
+
role: "user",
|
|
65
|
+
content: content.map((part) => {
|
|
66
|
+
switch (part.type) {
|
|
67
|
+
case "text": return {
|
|
68
|
+
type: "text",
|
|
69
|
+
text: part.text
|
|
70
|
+
};
|
|
71
|
+
case "file": switch (part.data.type) {
|
|
72
|
+
case "reference": throw new UnsupportedFunctionalityError({ functionality: "file parts with provider references" });
|
|
73
|
+
case "text": throw new UnsupportedFunctionalityError({ functionality: "text file parts" });
|
|
74
|
+
case "url":
|
|
75
|
+
case "data": if (getTopLevelMediaType(part.mediaType) === "image") return {
|
|
76
|
+
type: "image_url",
|
|
77
|
+
image_url: formatFileUrl({ part })
|
|
78
|
+
};
|
|
79
|
+
else {
|
|
80
|
+
if (part.data.type === "data") {
|
|
81
|
+
if (resolveFullMediaType({ part }) !== "application/pdf") throw new UnsupportedFunctionalityError({ functionality: "Only images and PDF file parts are supported" });
|
|
82
|
+
} else if (part.mediaType !== "application/pdf") throw new UnsupportedFunctionalityError({ functionality: "Only images and PDF file parts are supported" });
|
|
83
|
+
return {
|
|
84
|
+
type: "document_url",
|
|
85
|
+
document_url: formatFileUrl({ part })
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
})
|
|
91
|
+
});
|
|
92
|
+
break;
|
|
93
|
+
case "assistant": {
|
|
94
|
+
let text = "";
|
|
95
|
+
let hasReasoning = false;
|
|
96
|
+
const contentParts = [];
|
|
97
|
+
const toolCalls = [];
|
|
98
|
+
for (const part of content) switch (part.type) {
|
|
99
|
+
case "text":
|
|
100
|
+
text += part.text;
|
|
101
|
+
contentParts.push({
|
|
102
|
+
type: "text",
|
|
103
|
+
text: part.text
|
|
104
|
+
});
|
|
105
|
+
break;
|
|
106
|
+
case "tool-call":
|
|
107
|
+
toolCalls.push({
|
|
108
|
+
id: part.toolCallId,
|
|
109
|
+
type: "function",
|
|
110
|
+
function: {
|
|
111
|
+
name: part.toolName,
|
|
112
|
+
arguments: JSON.stringify(part.input)
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
break;
|
|
116
|
+
case "reasoning":
|
|
117
|
+
hasReasoning = true;
|
|
118
|
+
contentParts.push({
|
|
119
|
+
type: "thinking",
|
|
120
|
+
thinking: [{
|
|
121
|
+
type: "text",
|
|
122
|
+
text: part.text
|
|
123
|
+
}],
|
|
124
|
+
closed: true
|
|
125
|
+
});
|
|
126
|
+
break;
|
|
127
|
+
default: throw new Error(`Unsupported content type in assistant message: ${part.type}`);
|
|
128
|
+
}
|
|
129
|
+
messages.push({
|
|
130
|
+
role: "assistant",
|
|
131
|
+
content: hasReasoning ? contentParts : text,
|
|
132
|
+
prefix: isLastMessage ? true : void 0,
|
|
133
|
+
tool_calls: toolCalls.length > 0 ? toolCalls : void 0
|
|
134
|
+
});
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
case "tool":
|
|
138
|
+
for (const toolResponse of content) {
|
|
139
|
+
if (toolResponse.type === "tool-approval-response") continue;
|
|
140
|
+
const output = toolResponse.output;
|
|
141
|
+
let contentValue;
|
|
142
|
+
switch (output.type) {
|
|
143
|
+
case "text":
|
|
144
|
+
case "error-text":
|
|
145
|
+
contentValue = output.value;
|
|
146
|
+
break;
|
|
147
|
+
case "execution-denied":
|
|
148
|
+
contentValue = output.reason ?? "Tool call execution denied.";
|
|
149
|
+
break;
|
|
150
|
+
case "content":
|
|
151
|
+
case "json":
|
|
152
|
+
case "error-json": contentValue = JSON.stringify(output.value);
|
|
153
|
+
}
|
|
154
|
+
messages.push({
|
|
155
|
+
role: "tool",
|
|
156
|
+
name: toolResponse.toolName,
|
|
157
|
+
tool_call_id: toolResponse.toolCallId,
|
|
158
|
+
content: contentValue
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
break;
|
|
162
|
+
default: throw new Error(`Unsupported role: ${role}`);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return messages;
|
|
243
166
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
import { createLanguageModelResponseMetadata } from "@ai-sdk/provider-utils";
|
|
247
|
-
|
|
248
|
-
// src/map-mistral-finish-reason.ts
|
|
167
|
+
//#endregion
|
|
168
|
+
//#region src/map-mistral-finish-reason.ts
|
|
249
169
|
function mapMistralFinishReason(finishReason) {
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
default:
|
|
259
|
-
return "other";
|
|
260
|
-
}
|
|
170
|
+
switch (finishReason) {
|
|
171
|
+
case "stop": return "stop";
|
|
172
|
+
case "length":
|
|
173
|
+
case "model_length": return "length";
|
|
174
|
+
case "tool_calls": return "tool-calls";
|
|
175
|
+
case "error": return "error";
|
|
176
|
+
default: return "other";
|
|
177
|
+
}
|
|
261
178
|
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
reasoningEffort: z2.enum(["high", "none"]).optional()
|
|
179
|
+
//#endregion
|
|
180
|
+
//#region src/mistral-chat-language-model-options.ts
|
|
181
|
+
const mistralLanguageModelChatOptions = z.object({
|
|
182
|
+
/**
|
|
183
|
+
* Whether to inject a safety prompt before all conversations.
|
|
184
|
+
*
|
|
185
|
+
* Defaults to `false`.
|
|
186
|
+
*/
|
|
187
|
+
safePrompt: z.boolean().optional(),
|
|
188
|
+
documentImageLimit: z.number().optional(),
|
|
189
|
+
documentPageLimit: z.number().optional(),
|
|
190
|
+
/**
|
|
191
|
+
* Whether to use structured outputs.
|
|
192
|
+
*
|
|
193
|
+
* @default true
|
|
194
|
+
*/
|
|
195
|
+
structuredOutputs: z.boolean().optional(),
|
|
196
|
+
/**
|
|
197
|
+
* Whether to use strict JSON schema validation.
|
|
198
|
+
*
|
|
199
|
+
* @default false
|
|
200
|
+
*/
|
|
201
|
+
strictJsonSchema: z.boolean().optional(),
|
|
202
|
+
/**
|
|
203
|
+
* Whether to enable parallel function calling during tool use.
|
|
204
|
+
* When set to false, the model will use at most one tool per response.
|
|
205
|
+
*
|
|
206
|
+
* @default true
|
|
207
|
+
*/
|
|
208
|
+
parallelToolCalls: z.boolean().optional(),
|
|
209
|
+
/**
|
|
210
|
+
* A stable identifier used to route requests with shared prompt prefixes to
|
|
211
|
+
* the same cache.
|
|
212
|
+
*/
|
|
213
|
+
promptCacheKey: z.string().optional(),
|
|
214
|
+
/**
|
|
215
|
+
* Controls the reasoning effort for models that support adjustable reasoning.
|
|
216
|
+
*
|
|
217
|
+
* - `'high'`: Enable reasoning
|
|
218
|
+
* - `'none'`: Disable reasoning
|
|
219
|
+
*/
|
|
220
|
+
reasoningEffort: z.enum(["high", "none"]).optional()
|
|
305
221
|
});
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
param: z3.string().nullable(),
|
|
315
|
-
code: z3.string().nullable()
|
|
222
|
+
//#endregion
|
|
223
|
+
//#region src/mistral-error.ts
|
|
224
|
+
const mistralErrorDataSchema = z.object({
|
|
225
|
+
object: z.literal("error"),
|
|
226
|
+
message: z.string(),
|
|
227
|
+
type: z.string(),
|
|
228
|
+
param: z.string().nullable(),
|
|
229
|
+
code: z.string().nullable()
|
|
316
230
|
});
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
231
|
+
const mistralFailedResponseHandler = createJsonErrorResponseHandler({
|
|
232
|
+
errorSchema: mistralErrorDataSchema,
|
|
233
|
+
errorToMessage: (data) => data.message
|
|
320
234
|
});
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
),
|
|
371
|
-
toolChoice: "any",
|
|
372
|
-
toolWarnings
|
|
373
|
-
};
|
|
374
|
-
default: {
|
|
375
|
-
const _exhaustiveCheck = type;
|
|
376
|
-
throw new UnsupportedFunctionalityError2({
|
|
377
|
-
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
378
|
-
});
|
|
379
|
-
}
|
|
380
|
-
}
|
|
235
|
+
//#endregion
|
|
236
|
+
//#region src/mistral-prepare-tools.ts
|
|
237
|
+
function prepareTools({ tools, toolChoice }) {
|
|
238
|
+
tools = tools?.length ? tools : void 0;
|
|
239
|
+
const toolWarnings = [];
|
|
240
|
+
if (tools == null) return {
|
|
241
|
+
tools: void 0,
|
|
242
|
+
toolChoice: void 0,
|
|
243
|
+
toolWarnings
|
|
244
|
+
};
|
|
245
|
+
const mistralTools = [];
|
|
246
|
+
for (const tool of tools) if (tool.type === "provider") toolWarnings.push({
|
|
247
|
+
type: "unsupported",
|
|
248
|
+
feature: `provider-defined tool ${tool.id}`
|
|
249
|
+
});
|
|
250
|
+
else mistralTools.push({
|
|
251
|
+
type: "function",
|
|
252
|
+
function: {
|
|
253
|
+
name: tool.name,
|
|
254
|
+
description: tool.description,
|
|
255
|
+
parameters: tool.inputSchema,
|
|
256
|
+
...tool.strict != null ? { strict: tool.strict } : {}
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
if (toolChoice == null) return {
|
|
260
|
+
tools: mistralTools,
|
|
261
|
+
toolChoice: void 0,
|
|
262
|
+
toolWarnings
|
|
263
|
+
};
|
|
264
|
+
const type = toolChoice.type;
|
|
265
|
+
switch (type) {
|
|
266
|
+
case "auto":
|
|
267
|
+
case "none": return {
|
|
268
|
+
tools: mistralTools,
|
|
269
|
+
toolChoice: type,
|
|
270
|
+
toolWarnings
|
|
271
|
+
};
|
|
272
|
+
case "required": return {
|
|
273
|
+
tools: mistralTools,
|
|
274
|
+
toolChoice: "any",
|
|
275
|
+
toolWarnings
|
|
276
|
+
};
|
|
277
|
+
case "tool": return {
|
|
278
|
+
tools: mistralTools.filter((tool) => tool.function.name === toolChoice.toolName),
|
|
279
|
+
toolChoice: "any",
|
|
280
|
+
toolWarnings
|
|
281
|
+
};
|
|
282
|
+
default: throw new UnsupportedFunctionalityError({ functionality: `tool choice type: ${type}` });
|
|
283
|
+
}
|
|
381
284
|
}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
285
|
+
//#endregion
|
|
286
|
+
//#region src/mistral-chat-language-model.ts
|
|
287
|
+
const reasoningEffortModelIds = /* @__PURE__ */ new Set([
|
|
288
|
+
"glm-5-2",
|
|
289
|
+
"labs-leanstral-1-5",
|
|
290
|
+
"labs-leanstral-1-5-1",
|
|
291
|
+
"magistral-medium-latest",
|
|
292
|
+
"magistral-small-latest",
|
|
293
|
+
"mistral-medium",
|
|
294
|
+
"mistral-medium-2604",
|
|
295
|
+
"mistral-medium-3",
|
|
296
|
+
"mistral-medium-3-5",
|
|
297
|
+
"mistral-medium-3.5",
|
|
298
|
+
"mistral-medium-latest",
|
|
299
|
+
"mistral-small-2603",
|
|
300
|
+
"mistral-small-latest",
|
|
301
|
+
"mistral-vibe-cli-fast",
|
|
302
|
+
"mistral-vibe-cli-latest",
|
|
303
|
+
"mistral-vibe-cli-with-tools",
|
|
304
|
+
"zai-glm-5-2"
|
|
402
305
|
]);
|
|
403
|
-
var MistralChatLanguageModel = class
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
controller.enqueue({
|
|
685
|
-
type: "text-delta",
|
|
686
|
-
id: "0",
|
|
687
|
-
delta: textContent
|
|
688
|
-
});
|
|
689
|
-
}
|
|
690
|
-
if (delta?.tool_calls != null) {
|
|
691
|
-
for (const toolCall of delta.tool_calls) {
|
|
692
|
-
toolCallTracker.processDelta(toolCall);
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
if (choice.finish_reason != null) {
|
|
696
|
-
finishReason = {
|
|
697
|
-
unified: mapMistralFinishReason(choice.finish_reason),
|
|
698
|
-
raw: choice.finish_reason
|
|
699
|
-
};
|
|
700
|
-
}
|
|
701
|
-
},
|
|
702
|
-
flush(controller) {
|
|
703
|
-
if (activeReasoningId != null) {
|
|
704
|
-
controller.enqueue({
|
|
705
|
-
type: "reasoning-end",
|
|
706
|
-
id: activeReasoningId
|
|
707
|
-
});
|
|
708
|
-
}
|
|
709
|
-
if (activeText) {
|
|
710
|
-
controller.enqueue({ type: "text-end", id: "0" });
|
|
711
|
-
}
|
|
712
|
-
toolCallTracker.flush();
|
|
713
|
-
controller.enqueue({
|
|
714
|
-
type: "finish",
|
|
715
|
-
finishReason,
|
|
716
|
-
usage: convertMistralUsage(usage)
|
|
717
|
-
});
|
|
718
|
-
}
|
|
719
|
-
})
|
|
720
|
-
),
|
|
721
|
-
request: { body },
|
|
722
|
-
response: { headers: responseHeaders }
|
|
723
|
-
};
|
|
724
|
-
}
|
|
306
|
+
var MistralChatLanguageModel = class MistralChatLanguageModel {
|
|
307
|
+
static [WORKFLOW_SERIALIZE](model) {
|
|
308
|
+
return serializeModelOptions({
|
|
309
|
+
modelId: model.modelId,
|
|
310
|
+
config: model.config
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
static [WORKFLOW_DESERIALIZE](options) {
|
|
314
|
+
return new MistralChatLanguageModel(options.modelId, options.config);
|
|
315
|
+
}
|
|
316
|
+
constructor(modelId, config) {
|
|
317
|
+
this.specificationVersion = "v4";
|
|
318
|
+
this.supportedUrls = { "application/pdf": [/^https:\/\/.*$/] };
|
|
319
|
+
this.modelId = modelId;
|
|
320
|
+
this.config = config;
|
|
321
|
+
this.generateId = config.generateId ?? generateId;
|
|
322
|
+
}
|
|
323
|
+
get provider() {
|
|
324
|
+
return this.config.provider;
|
|
325
|
+
}
|
|
326
|
+
async getArgs({ prompt, maxOutputTokens, temperature, topP, topK, frequencyPenalty, presencePenalty, reasoning, stopSequences, responseFormat, seed, providerOptions, tools, toolChoice }) {
|
|
327
|
+
const warnings = [];
|
|
328
|
+
const options = await parseProviderOptions({
|
|
329
|
+
provider: "mistral",
|
|
330
|
+
providerOptions,
|
|
331
|
+
schema: mistralLanguageModelChatOptions
|
|
332
|
+
}) ?? {};
|
|
333
|
+
if (topK != null) warnings.push({
|
|
334
|
+
type: "unsupported",
|
|
335
|
+
feature: "topK"
|
|
336
|
+
});
|
|
337
|
+
const supportsReasoningEffort = reasoningEffortModelIds.has(this.modelId);
|
|
338
|
+
let resolvedReasoningEffort;
|
|
339
|
+
if (supportsReasoningEffort) resolvedReasoningEffort = options.reasoningEffort ?? (isCustomReasoning(reasoning) ? reasoning === "none" ? "none" : mapReasoningToProviderEffort({
|
|
340
|
+
reasoning,
|
|
341
|
+
effortMap: {
|
|
342
|
+
minimal: "high",
|
|
343
|
+
low: "high",
|
|
344
|
+
medium: "high",
|
|
345
|
+
high: "high",
|
|
346
|
+
xhigh: "high"
|
|
347
|
+
},
|
|
348
|
+
warnings
|
|
349
|
+
}) : void 0);
|
|
350
|
+
else if (isCustomReasoning(reasoning)) warnings.push({
|
|
351
|
+
type: "unsupported",
|
|
352
|
+
feature: "reasoning",
|
|
353
|
+
details: "This model does not support reasoning configuration."
|
|
354
|
+
});
|
|
355
|
+
const structuredOutputs = options.structuredOutputs ?? true;
|
|
356
|
+
const strictJsonSchema = options.strictJsonSchema ?? false;
|
|
357
|
+
if (responseFormat?.type === "json" && (!structuredOutputs || responseFormat.schema == null)) prompt = injectJsonInstructionIntoMessages({
|
|
358
|
+
messages: prompt,
|
|
359
|
+
schema: responseFormat.schema
|
|
360
|
+
});
|
|
361
|
+
const baseArgs = {
|
|
362
|
+
model: this.modelId,
|
|
363
|
+
safe_prompt: options.safePrompt,
|
|
364
|
+
max_tokens: maxOutputTokens,
|
|
365
|
+
temperature,
|
|
366
|
+
top_p: topP,
|
|
367
|
+
...frequencyPenalty != null ? { frequency_penalty: frequencyPenalty } : {},
|
|
368
|
+
...presencePenalty != null ? { presence_penalty: presencePenalty } : {},
|
|
369
|
+
stop: stopSequences,
|
|
370
|
+
random_seed: seed,
|
|
371
|
+
reasoning_effort: resolvedReasoningEffort,
|
|
372
|
+
response_format: responseFormat?.type === "json" ? structuredOutputs && responseFormat?.schema != null ? {
|
|
373
|
+
type: "json_schema",
|
|
374
|
+
json_schema: {
|
|
375
|
+
schema: responseFormat.schema,
|
|
376
|
+
strict: strictJsonSchema,
|
|
377
|
+
name: responseFormat.name ?? "response",
|
|
378
|
+
description: responseFormat.description
|
|
379
|
+
}
|
|
380
|
+
} : { type: "json_object" } : void 0,
|
|
381
|
+
document_image_limit: options.documentImageLimit,
|
|
382
|
+
document_page_limit: options.documentPageLimit,
|
|
383
|
+
...options.promptCacheKey !== void 0 ? { prompt_cache_key: options.promptCacheKey } : {},
|
|
384
|
+
messages: convertToMistralChatMessages(prompt)
|
|
385
|
+
};
|
|
386
|
+
const { tools: mistralTools, toolChoice: mistralToolChoice, toolWarnings } = prepareTools({
|
|
387
|
+
tools,
|
|
388
|
+
toolChoice
|
|
389
|
+
});
|
|
390
|
+
return {
|
|
391
|
+
args: {
|
|
392
|
+
...baseArgs,
|
|
393
|
+
tools: mistralTools,
|
|
394
|
+
tool_choice: mistralToolChoice,
|
|
395
|
+
...mistralTools != null && options.parallelToolCalls !== void 0 ? { parallel_tool_calls: options.parallelToolCalls } : {}
|
|
396
|
+
},
|
|
397
|
+
warnings: [...warnings, ...toolWarnings]
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
async doGenerate(options) {
|
|
401
|
+
const { args: body, warnings } = await this.getArgs(options);
|
|
402
|
+
const { responseHeaders, value: response, rawValue: rawResponse } = await postJsonToApi({
|
|
403
|
+
url: `${this.config.baseURL}/chat/completions`,
|
|
404
|
+
headers: combineHeaders(this.config.headers?.(), options.headers),
|
|
405
|
+
body,
|
|
406
|
+
failedResponseHandler: mistralFailedResponseHandler,
|
|
407
|
+
successfulResponseHandler: createJsonResponseHandler(mistralChatResponseSchema),
|
|
408
|
+
abortSignal: options.abortSignal,
|
|
409
|
+
fetch: this.config.fetch
|
|
410
|
+
});
|
|
411
|
+
const choice = response.choices[0];
|
|
412
|
+
const content = [];
|
|
413
|
+
if (choice.message.content != null && Array.isArray(choice.message.content)) {
|
|
414
|
+
for (const part of choice.message.content) if (part.type === "thinking") {
|
|
415
|
+
const reasoningText = extractReasoningContent(part.thinking);
|
|
416
|
+
if (reasoningText.length > 0) content.push({
|
|
417
|
+
type: "reasoning",
|
|
418
|
+
text: reasoningText
|
|
419
|
+
});
|
|
420
|
+
} else if (part.type === "text") {
|
|
421
|
+
if (part.text.length > 0) content.push({
|
|
422
|
+
type: "text",
|
|
423
|
+
text: part.text
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
} else {
|
|
427
|
+
const text = extractTextContent(choice.message.content);
|
|
428
|
+
if (text != null && text.length > 0) content.push({
|
|
429
|
+
type: "text",
|
|
430
|
+
text
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
if (choice.message.tool_calls != null) for (const toolCall of choice.message.tool_calls) content.push({
|
|
434
|
+
type: "tool-call",
|
|
435
|
+
toolCallId: toolCall.id,
|
|
436
|
+
toolName: toolCall.function.name,
|
|
437
|
+
input: toolCall.function.arguments
|
|
438
|
+
});
|
|
439
|
+
return {
|
|
440
|
+
content,
|
|
441
|
+
finishReason: {
|
|
442
|
+
unified: mapMistralFinishReason(choice.finish_reason),
|
|
443
|
+
raw: choice.finish_reason ?? void 0
|
|
444
|
+
},
|
|
445
|
+
usage: convertMistralUsage(response.usage),
|
|
446
|
+
request: { body },
|
|
447
|
+
response: {
|
|
448
|
+
...getResponseMetadata(response),
|
|
449
|
+
headers: responseHeaders,
|
|
450
|
+
body: rawResponse
|
|
451
|
+
},
|
|
452
|
+
warnings
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
async doStream(options) {
|
|
456
|
+
const { args, warnings } = await this.getArgs(options);
|
|
457
|
+
const body = {
|
|
458
|
+
...args,
|
|
459
|
+
stream: true
|
|
460
|
+
};
|
|
461
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
462
|
+
url: `${this.config.baseURL}/chat/completions`,
|
|
463
|
+
headers: combineHeaders(this.config.headers?.(), options.headers),
|
|
464
|
+
body,
|
|
465
|
+
failedResponseHandler: mistralFailedResponseHandler,
|
|
466
|
+
successfulResponseHandler: createEventSourceResponseHandler(mistralChatChunkSchema),
|
|
467
|
+
abortSignal: options.abortSignal,
|
|
468
|
+
fetch: this.config.fetch
|
|
469
|
+
});
|
|
470
|
+
let finishReason = {
|
|
471
|
+
unified: "other",
|
|
472
|
+
raw: void 0
|
|
473
|
+
};
|
|
474
|
+
let usage = void 0;
|
|
475
|
+
let isFirstChunk = true;
|
|
476
|
+
let activeText = false;
|
|
477
|
+
let activeReasoningId = null;
|
|
478
|
+
let toolCallTracker;
|
|
479
|
+
const generateId = this.generateId;
|
|
480
|
+
return {
|
|
481
|
+
stream: response.pipeThrough(new TransformStream({
|
|
482
|
+
start(controller) {
|
|
483
|
+
toolCallTracker = new StreamingToolCallTracker(controller, { generateId });
|
|
484
|
+
controller.enqueue({
|
|
485
|
+
type: "stream-start",
|
|
486
|
+
warnings
|
|
487
|
+
});
|
|
488
|
+
},
|
|
489
|
+
transform(chunk, controller) {
|
|
490
|
+
if (options.includeRawChunks) controller.enqueue({
|
|
491
|
+
type: "raw",
|
|
492
|
+
rawValue: chunk.rawValue
|
|
493
|
+
});
|
|
494
|
+
if (!chunk.success) {
|
|
495
|
+
controller.enqueue({
|
|
496
|
+
type: "error",
|
|
497
|
+
error: chunk.error
|
|
498
|
+
});
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
const value = chunk.value;
|
|
502
|
+
if (isFirstChunk) {
|
|
503
|
+
isFirstChunk = false;
|
|
504
|
+
controller.enqueue({
|
|
505
|
+
type: "response-metadata",
|
|
506
|
+
...getResponseMetadata(value)
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
if (value.usage != null) usage = value.usage;
|
|
510
|
+
const choice = value.choices[0];
|
|
511
|
+
const delta = choice.delta;
|
|
512
|
+
const textContent = extractTextContent(delta.content);
|
|
513
|
+
if (delta.content != null && Array.isArray(delta.content)) {
|
|
514
|
+
for (const part of delta.content) if (part.type === "thinking") {
|
|
515
|
+
const reasoningDelta = extractReasoningContent(part.thinking);
|
|
516
|
+
if (reasoningDelta.length > 0) {
|
|
517
|
+
if (activeReasoningId == null) {
|
|
518
|
+
if (activeText) {
|
|
519
|
+
controller.enqueue({
|
|
520
|
+
type: "text-end",
|
|
521
|
+
id: "0"
|
|
522
|
+
});
|
|
523
|
+
activeText = false;
|
|
524
|
+
}
|
|
525
|
+
activeReasoningId = generateId();
|
|
526
|
+
controller.enqueue({
|
|
527
|
+
type: "reasoning-start",
|
|
528
|
+
id: activeReasoningId
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
controller.enqueue({
|
|
532
|
+
type: "reasoning-delta",
|
|
533
|
+
id: activeReasoningId,
|
|
534
|
+
delta: reasoningDelta
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
if (textContent != null && textContent.length > 0) {
|
|
540
|
+
if (!activeText) {
|
|
541
|
+
if (activeReasoningId != null) {
|
|
542
|
+
controller.enqueue({
|
|
543
|
+
type: "reasoning-end",
|
|
544
|
+
id: activeReasoningId
|
|
545
|
+
});
|
|
546
|
+
activeReasoningId = null;
|
|
547
|
+
}
|
|
548
|
+
controller.enqueue({
|
|
549
|
+
type: "text-start",
|
|
550
|
+
id: "0"
|
|
551
|
+
});
|
|
552
|
+
activeText = true;
|
|
553
|
+
}
|
|
554
|
+
controller.enqueue({
|
|
555
|
+
type: "text-delta",
|
|
556
|
+
id: "0",
|
|
557
|
+
delta: textContent
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
if (delta?.tool_calls != null) for (const toolCall of delta.tool_calls) toolCallTracker.processDelta(toolCall);
|
|
561
|
+
if (choice.finish_reason != null) finishReason = {
|
|
562
|
+
unified: mapMistralFinishReason(choice.finish_reason),
|
|
563
|
+
raw: choice.finish_reason
|
|
564
|
+
};
|
|
565
|
+
},
|
|
566
|
+
flush(controller) {
|
|
567
|
+
if (activeReasoningId != null) controller.enqueue({
|
|
568
|
+
type: "reasoning-end",
|
|
569
|
+
id: activeReasoningId
|
|
570
|
+
});
|
|
571
|
+
if (activeText) controller.enqueue({
|
|
572
|
+
type: "text-end",
|
|
573
|
+
id: "0"
|
|
574
|
+
});
|
|
575
|
+
toolCallTracker.flush();
|
|
576
|
+
controller.enqueue({
|
|
577
|
+
type: "finish",
|
|
578
|
+
finishReason,
|
|
579
|
+
usage: convertMistralUsage(usage)
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
})),
|
|
583
|
+
request: { body },
|
|
584
|
+
response: { headers: responseHeaders }
|
|
585
|
+
};
|
|
586
|
+
}
|
|
725
587
|
};
|
|
726
588
|
function extractReasoningContent(thinking) {
|
|
727
|
-
|
|
589
|
+
return thinking.filter((chunk) => chunk.type === "text").map((chunk) => chunk.text).join("");
|
|
728
590
|
}
|
|
729
591
|
function extractTextContent(content) {
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
break;
|
|
747
|
-
default: {
|
|
748
|
-
const _exhaustiveCheck = type;
|
|
749
|
-
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
|
750
|
-
}
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
return textContent.length ? textContent.join("") : void 0;
|
|
592
|
+
if (typeof content === "string") return content;
|
|
593
|
+
if (content == null) return;
|
|
594
|
+
const textContent = [];
|
|
595
|
+
for (const chunk of content) {
|
|
596
|
+
const { type } = chunk;
|
|
597
|
+
switch (type) {
|
|
598
|
+
case "text":
|
|
599
|
+
textContent.push(chunk.text);
|
|
600
|
+
break;
|
|
601
|
+
case "thinking":
|
|
602
|
+
case "image_url":
|
|
603
|
+
case "reference": break;
|
|
604
|
+
default: throw new Error(`Unsupported type: ${type}`);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
return textContent.length ? textContent.join("") : void 0;
|
|
754
608
|
}
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
id: z4.string(),
|
|
801
|
-
function: z4.object({ name: z4.string(), arguments: z4.string() })
|
|
802
|
-
})
|
|
803
|
-
).nullish()
|
|
804
|
-
}),
|
|
805
|
-
index: z4.number(),
|
|
806
|
-
finish_reason: z4.string().nullish()
|
|
807
|
-
})
|
|
808
|
-
),
|
|
809
|
-
object: z4.literal("chat.completion"),
|
|
810
|
-
usage: mistralUsageSchema
|
|
609
|
+
const mistralContentSchema = z.union([z.string(), z.array(z.discriminatedUnion("type", [
|
|
610
|
+
z.object({
|
|
611
|
+
type: z.literal("text"),
|
|
612
|
+
text: z.string()
|
|
613
|
+
}),
|
|
614
|
+
z.object({
|
|
615
|
+
type: z.literal("image_url"),
|
|
616
|
+
image_url: z.union([z.string(), z.object({
|
|
617
|
+
url: z.string(),
|
|
618
|
+
detail: z.string().nullable()
|
|
619
|
+
})])
|
|
620
|
+
}),
|
|
621
|
+
z.object({
|
|
622
|
+
type: z.literal("reference"),
|
|
623
|
+
reference_ids: z.array(z.union([z.string(), z.number()]))
|
|
624
|
+
}),
|
|
625
|
+
z.object({
|
|
626
|
+
type: z.literal("thinking"),
|
|
627
|
+
thinking: z.array(z.object({
|
|
628
|
+
type: z.literal("text"),
|
|
629
|
+
text: z.string()
|
|
630
|
+
}))
|
|
631
|
+
})
|
|
632
|
+
]))]).nullish();
|
|
633
|
+
const mistralChatResponseSchema = z.object({
|
|
634
|
+
id: z.string().nullish(),
|
|
635
|
+
created: z.number().nullish(),
|
|
636
|
+
model: z.string().nullish(),
|
|
637
|
+
choices: z.array(z.object({
|
|
638
|
+
message: z.object({
|
|
639
|
+
role: z.literal("assistant"),
|
|
640
|
+
content: mistralContentSchema,
|
|
641
|
+
tool_calls: z.array(z.object({
|
|
642
|
+
id: z.string(),
|
|
643
|
+
function: z.object({
|
|
644
|
+
name: z.string(),
|
|
645
|
+
arguments: z.string()
|
|
646
|
+
})
|
|
647
|
+
})).nullish()
|
|
648
|
+
}),
|
|
649
|
+
index: z.number(),
|
|
650
|
+
finish_reason: z.string().nullish()
|
|
651
|
+
})),
|
|
652
|
+
object: z.literal("chat.completion"),
|
|
653
|
+
usage: mistralUsageSchema
|
|
811
654
|
});
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
index: z4.number()
|
|
834
|
-
})
|
|
835
|
-
),
|
|
836
|
-
usage: mistralUsageSchema.nullish()
|
|
655
|
+
const mistralChatChunkSchema = z.object({
|
|
656
|
+
id: z.string().nullish(),
|
|
657
|
+
created: z.number().nullish(),
|
|
658
|
+
model: z.string().nullish(),
|
|
659
|
+
choices: z.array(z.object({
|
|
660
|
+
delta: z.object({
|
|
661
|
+
role: z.enum(["assistant"]).optional(),
|
|
662
|
+
content: mistralContentSchema,
|
|
663
|
+
tool_calls: z.array(z.object({
|
|
664
|
+
index: z.number().nullish(),
|
|
665
|
+
id: z.string().nullish(),
|
|
666
|
+
function: z.object({
|
|
667
|
+
name: z.string().nullish(),
|
|
668
|
+
arguments: z.string().nullish()
|
|
669
|
+
})
|
|
670
|
+
})).nullish()
|
|
671
|
+
}),
|
|
672
|
+
finish_reason: z.string().nullish(),
|
|
673
|
+
index: z.number()
|
|
674
|
+
})),
|
|
675
|
+
usage: mistralUsageSchema.nullish()
|
|
837
676
|
});
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
*/
|
|
860
|
-
metadata: z5.record(z5.string(), z5.any()).optional(),
|
|
861
|
-
/**
|
|
862
|
-
* The dimension of the output embeddings when supported by the model.
|
|
863
|
-
*/
|
|
864
|
-
outputDimension: z5.number().int().positive().optional(),
|
|
865
|
-
/**
|
|
866
|
-
* The data type of the output embeddings when supported by the model.
|
|
867
|
-
*/
|
|
868
|
-
outputDtype: z5.enum(["float", "int8", "uint8", "binary", "ubinary"]).optional()
|
|
677
|
+
//#endregion
|
|
678
|
+
//#region src/mistral-embedding-model-options.ts
|
|
679
|
+
const mistralEmbeddingModelOptions = z.object({
|
|
680
|
+
/**
|
|
681
|
+
* Additional metadata to attach to the embedding request.
|
|
682
|
+
*/
|
|
683
|
+
metadata: z.record(z.string(), z.any()).optional(),
|
|
684
|
+
/**
|
|
685
|
+
* The dimension of the output embeddings when supported by the model.
|
|
686
|
+
*/
|
|
687
|
+
outputDimension: z.number().int().positive().optional(),
|
|
688
|
+
/**
|
|
689
|
+
* The data type of the output embeddings when supported by the model.
|
|
690
|
+
*/
|
|
691
|
+
outputDtype: z.enum([
|
|
692
|
+
"float",
|
|
693
|
+
"int8",
|
|
694
|
+
"uint8",
|
|
695
|
+
"binary",
|
|
696
|
+
"ubinary"
|
|
697
|
+
]).optional()
|
|
869
698
|
});
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
var MistralEmbeddingModel = class
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
abortSignal,
|
|
931
|
-
fetch: this.config.fetch
|
|
932
|
-
});
|
|
933
|
-
return {
|
|
934
|
-
warnings: [],
|
|
935
|
-
embeddings: response.data.map((item) => item.embedding),
|
|
936
|
-
usage: response.usage ? { tokens: response.usage.prompt_tokens } : void 0,
|
|
937
|
-
response: { headers: responseHeaders, body: rawValue }
|
|
938
|
-
};
|
|
939
|
-
}
|
|
699
|
+
//#endregion
|
|
700
|
+
//#region src/mistral-embedding-model.ts
|
|
701
|
+
var MistralEmbeddingModel = class MistralEmbeddingModel {
|
|
702
|
+
get provider() {
|
|
703
|
+
return this.config.provider;
|
|
704
|
+
}
|
|
705
|
+
static [WORKFLOW_SERIALIZE](model) {
|
|
706
|
+
return serializeModelOptions({
|
|
707
|
+
modelId: model.modelId,
|
|
708
|
+
config: model.config
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
static [WORKFLOW_DESERIALIZE](options) {
|
|
712
|
+
return new MistralEmbeddingModel(options.modelId, options.config);
|
|
713
|
+
}
|
|
714
|
+
constructor(modelId, config) {
|
|
715
|
+
this.specificationVersion = "v4";
|
|
716
|
+
this.maxEmbeddingsPerCall = 32;
|
|
717
|
+
this.supportsParallelCalls = false;
|
|
718
|
+
this.modelId = modelId;
|
|
719
|
+
this.config = config;
|
|
720
|
+
}
|
|
721
|
+
async doEmbed({ values, abortSignal, headers, providerOptions }) {
|
|
722
|
+
if (values.length > this.maxEmbeddingsPerCall) throw new TooManyEmbeddingValuesForCallError({
|
|
723
|
+
provider: this.provider,
|
|
724
|
+
modelId: this.modelId,
|
|
725
|
+
maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
|
|
726
|
+
values
|
|
727
|
+
});
|
|
728
|
+
const mistralOptions = await parseProviderOptions({
|
|
729
|
+
provider: "mistral",
|
|
730
|
+
providerOptions,
|
|
731
|
+
schema: mistralEmbeddingModelOptions
|
|
732
|
+
}) ?? {};
|
|
733
|
+
const { responseHeaders, value: response, rawValue } = await postJsonToApi({
|
|
734
|
+
url: `${this.config.baseURL}/embeddings`,
|
|
735
|
+
headers: combineHeaders(this.config.headers?.(), headers),
|
|
736
|
+
body: {
|
|
737
|
+
model: this.modelId,
|
|
738
|
+
input: values,
|
|
739
|
+
metadata: mistralOptions.metadata,
|
|
740
|
+
output_dimension: mistralOptions.outputDimension,
|
|
741
|
+
output_dtype: mistralOptions.outputDtype,
|
|
742
|
+
encoding_format: "float"
|
|
743
|
+
},
|
|
744
|
+
failedResponseHandler: mistralFailedResponseHandler,
|
|
745
|
+
successfulResponseHandler: createJsonResponseHandler(MistralTextEmbeddingResponseSchema),
|
|
746
|
+
abortSignal,
|
|
747
|
+
fetch: this.config.fetch
|
|
748
|
+
});
|
|
749
|
+
return {
|
|
750
|
+
warnings: [],
|
|
751
|
+
embeddings: response.data.map((item) => item.embedding),
|
|
752
|
+
usage: response.usage ? { tokens: response.usage.prompt_tokens } : void 0,
|
|
753
|
+
response: {
|
|
754
|
+
headers: responseHeaders,
|
|
755
|
+
body: rawValue
|
|
756
|
+
}
|
|
757
|
+
};
|
|
758
|
+
}
|
|
940
759
|
};
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
760
|
+
const MistralTextEmbeddingResponseSchema = z.object({
|
|
761
|
+
data: z.array(z.object({ embedding: z.array(z.number()) })),
|
|
762
|
+
usage: z.object({ prompt_tokens: z.number() }).nullish()
|
|
944
763
|
});
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
responseHeaders,
|
|
1055
|
-
rawValue: rawResponse
|
|
1056
|
-
} = await postToApi({
|
|
1057
|
-
url: `${this.config.baseURL}/audio/speech`,
|
|
1058
|
-
headers: combineHeaders3(
|
|
1059
|
-
{ "Content-Type": "application/json" },
|
|
1060
|
-
this.config.headers?.(),
|
|
1061
|
-
options.headers
|
|
1062
|
-
),
|
|
1063
|
-
body: {
|
|
1064
|
-
content: JSON.stringify(requestBody),
|
|
1065
|
-
values: requestBodyValues
|
|
1066
|
-
},
|
|
1067
|
-
failedResponseHandler: mistralFailedResponseHandler,
|
|
1068
|
-
successfulResponseHandler: createJsonResponseHandler3(
|
|
1069
|
-
mistralSpeechResponseSchema
|
|
1070
|
-
),
|
|
1071
|
-
abortSignal: options.abortSignal,
|
|
1072
|
-
fetch: this.config.fetch
|
|
1073
|
-
});
|
|
1074
|
-
return {
|
|
1075
|
-
audio: response.audio_data,
|
|
1076
|
-
warnings,
|
|
1077
|
-
request: {
|
|
1078
|
-
body: JSON.stringify(requestBodyValues)
|
|
1079
|
-
},
|
|
1080
|
-
response: {
|
|
1081
|
-
timestamp: currentDate,
|
|
1082
|
-
modelId: this.modelId,
|
|
1083
|
-
headers: responseHeaders,
|
|
1084
|
-
body: rawResponse
|
|
1085
|
-
}
|
|
1086
|
-
};
|
|
1087
|
-
}
|
|
764
|
+
//#endregion
|
|
765
|
+
//#region src/mistral-speech-model-options.ts
|
|
766
|
+
const mistralSpeechModelOptions = z.object({
|
|
767
|
+
/**
|
|
768
|
+
* Base64-encoded reference audio for one-off voice cloning.
|
|
769
|
+
*
|
|
770
|
+
* When provided, this takes precedence over the standard `voice` option.
|
|
771
|
+
*/
|
|
772
|
+
refAudio: z.string().min(1).optional() });
|
|
773
|
+
//#endregion
|
|
774
|
+
//#region src/mistral-speech-model.ts
|
|
775
|
+
var MistralSpeechModel = class MistralSpeechModel {
|
|
776
|
+
static [WORKFLOW_SERIALIZE](model) {
|
|
777
|
+
return serializeModelOptions({
|
|
778
|
+
modelId: model.modelId,
|
|
779
|
+
config: model.config
|
|
780
|
+
});
|
|
781
|
+
}
|
|
782
|
+
static [WORKFLOW_DESERIALIZE](options) {
|
|
783
|
+
return new MistralSpeechModel(options.modelId, options.config);
|
|
784
|
+
}
|
|
785
|
+
get provider() {
|
|
786
|
+
return this.config.provider;
|
|
787
|
+
}
|
|
788
|
+
constructor(modelId, config) {
|
|
789
|
+
this.modelId = modelId;
|
|
790
|
+
this.config = config;
|
|
791
|
+
this.specificationVersion = "v4";
|
|
792
|
+
}
|
|
793
|
+
async getArgs({ text, voice, outputFormat = "mp3", instructions, speed, language, providerOptions }) {
|
|
794
|
+
const warnings = [];
|
|
795
|
+
const mistralOptions = await parseProviderOptions({
|
|
796
|
+
provider: "mistral",
|
|
797
|
+
providerOptions,
|
|
798
|
+
schema: mistralSpeechModelOptions
|
|
799
|
+
});
|
|
800
|
+
let responseFormat = "mp3";
|
|
801
|
+
if ([
|
|
802
|
+
"pcm",
|
|
803
|
+
"wav",
|
|
804
|
+
"mp3",
|
|
805
|
+
"flac",
|
|
806
|
+
"opus"
|
|
807
|
+
].includes(outputFormat)) responseFormat = outputFormat;
|
|
808
|
+
else warnings.push({
|
|
809
|
+
type: "unsupported",
|
|
810
|
+
feature: "outputFormat",
|
|
811
|
+
details: `Unsupported output format: ${outputFormat}. Using mp3 instead.`
|
|
812
|
+
});
|
|
813
|
+
if (instructions != null) warnings.push({
|
|
814
|
+
type: "unsupported",
|
|
815
|
+
feature: "instructions",
|
|
816
|
+
details: "Mistral speech models do not support the `instructions` option. Use a reference audio clip to guide delivery."
|
|
817
|
+
});
|
|
818
|
+
if (speed != null) warnings.push({
|
|
819
|
+
type: "unsupported",
|
|
820
|
+
feature: "speed",
|
|
821
|
+
details: "Mistral speech models do not support the `speed` option. It was ignored."
|
|
822
|
+
});
|
|
823
|
+
if (language != null) warnings.push({
|
|
824
|
+
type: "unsupported",
|
|
825
|
+
feature: "language",
|
|
826
|
+
details: "Mistral speech models do not support the `language` option. Language is inferred from the input text and voice."
|
|
827
|
+
});
|
|
828
|
+
const refAudio = mistralOptions?.refAudio;
|
|
829
|
+
const requestBody = {
|
|
830
|
+
model: this.modelId,
|
|
831
|
+
input: text,
|
|
832
|
+
voice_id: refAudio == null ? voice : void 0,
|
|
833
|
+
ref_audio: refAudio,
|
|
834
|
+
response_format: responseFormat,
|
|
835
|
+
stream: false
|
|
836
|
+
};
|
|
837
|
+
return {
|
|
838
|
+
requestBody,
|
|
839
|
+
requestBodyValues: {
|
|
840
|
+
...requestBody,
|
|
841
|
+
ref_audio: refAudio == null ? void 0 : "[redacted]"
|
|
842
|
+
},
|
|
843
|
+
warnings
|
|
844
|
+
};
|
|
845
|
+
}
|
|
846
|
+
async doGenerate(options) {
|
|
847
|
+
const currentDate = this.config._internal?.currentDate?.() ?? /* @__PURE__ */ new Date();
|
|
848
|
+
const { requestBody, requestBodyValues, warnings } = await this.getArgs(options);
|
|
849
|
+
const { value: response, responseHeaders, rawValue: rawResponse } = await postToApi({
|
|
850
|
+
url: `${this.config.baseURL}/audio/speech`,
|
|
851
|
+
headers: combineHeaders({ "Content-Type": "application/json" }, this.config.headers?.(), options.headers),
|
|
852
|
+
body: {
|
|
853
|
+
content: JSON.stringify(requestBody),
|
|
854
|
+
values: requestBodyValues
|
|
855
|
+
},
|
|
856
|
+
failedResponseHandler: mistralFailedResponseHandler,
|
|
857
|
+
successfulResponseHandler: createJsonResponseHandler(mistralSpeechResponseSchema),
|
|
858
|
+
abortSignal: options.abortSignal,
|
|
859
|
+
fetch: this.config.fetch
|
|
860
|
+
});
|
|
861
|
+
return {
|
|
862
|
+
audio: response.audio_data,
|
|
863
|
+
warnings,
|
|
864
|
+
request: { body: JSON.stringify(requestBodyValues) },
|
|
865
|
+
response: {
|
|
866
|
+
timestamp: currentDate,
|
|
867
|
+
modelId: this.modelId,
|
|
868
|
+
headers: responseHeaders,
|
|
869
|
+
body: rawResponse
|
|
870
|
+
}
|
|
871
|
+
};
|
|
872
|
+
}
|
|
1088
873
|
};
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
);
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
responseHeaders,
|
|
1216
|
-
rawValue: rawResponse
|
|
1217
|
-
} = await postFormDataToApi({
|
|
1218
|
-
url: `${this.config.baseURL}/audio/transcriptions`,
|
|
1219
|
-
headers: combineHeaders4(this.config.headers?.(), options.headers),
|
|
1220
|
-
formData,
|
|
1221
|
-
failedResponseHandler: mistralFailedResponseHandler,
|
|
1222
|
-
successfulResponseHandler: createJsonResponseHandler4(
|
|
1223
|
-
mistralTranscriptionResponseSchema
|
|
1224
|
-
),
|
|
1225
|
-
abortSignal: options.abortSignal,
|
|
1226
|
-
fetch: this.config.fetch
|
|
1227
|
-
});
|
|
1228
|
-
const segments = response.segments?.map((segment) => ({
|
|
1229
|
-
text: segment.text,
|
|
1230
|
-
startSecond: segment.start,
|
|
1231
|
-
endSecond: segment.end
|
|
1232
|
-
})) ?? [];
|
|
1233
|
-
const mistralMetadata = {};
|
|
1234
|
-
if (response.usage != null) {
|
|
1235
|
-
mistralMetadata.usage = {
|
|
1236
|
-
...response.usage.prompt_tokens != null && {
|
|
1237
|
-
promptTokens: response.usage.prompt_tokens
|
|
1238
|
-
},
|
|
1239
|
-
...response.usage.completion_tokens != null && {
|
|
1240
|
-
completionTokens: response.usage.completion_tokens
|
|
1241
|
-
},
|
|
1242
|
-
...response.usage.total_tokens != null && {
|
|
1243
|
-
totalTokens: response.usage.total_tokens
|
|
1244
|
-
},
|
|
1245
|
-
...response.usage.prompt_audio_seconds != null && {
|
|
1246
|
-
promptAudioSeconds: response.usage.prompt_audio_seconds
|
|
1247
|
-
},
|
|
1248
|
-
...response.usage.request_count != null && {
|
|
1249
|
-
requestCount: response.usage.request_count
|
|
1250
|
-
}
|
|
1251
|
-
};
|
|
1252
|
-
}
|
|
1253
|
-
const providerSegments = response.segments?.filter(
|
|
1254
|
-
(segment) => segment.type != null || segment.score != null || segment.speaker_id != null
|
|
1255
|
-
).map((segment) => ({
|
|
1256
|
-
text: segment.text,
|
|
1257
|
-
startSecond: segment.start,
|
|
1258
|
-
endSecond: segment.end,
|
|
1259
|
-
...segment.type != null && { type: segment.type },
|
|
1260
|
-
...segment.score != null && { score: segment.score },
|
|
1261
|
-
...segment.speaker_id != null && {
|
|
1262
|
-
speakerId: segment.speaker_id
|
|
1263
|
-
}
|
|
1264
|
-
}));
|
|
1265
|
-
if (providerSegments != null && providerSegments.length > 0) {
|
|
1266
|
-
mistralMetadata.segments = providerSegments;
|
|
1267
|
-
}
|
|
1268
|
-
return {
|
|
1269
|
-
text: response.text,
|
|
1270
|
-
segments,
|
|
1271
|
-
language: response.language ?? void 0,
|
|
1272
|
-
durationInSeconds: response.usage?.prompt_audio_seconds ?? segments.at(-1)?.endSecond ?? void 0,
|
|
1273
|
-
warnings,
|
|
1274
|
-
response: {
|
|
1275
|
-
timestamp: currentDate,
|
|
1276
|
-
modelId: response.model,
|
|
1277
|
-
headers: responseHeaders,
|
|
1278
|
-
body: rawResponse
|
|
1279
|
-
},
|
|
1280
|
-
...Object.keys(mistralMetadata).length > 0 && {
|
|
1281
|
-
providerMetadata: {
|
|
1282
|
-
mistral: mistralMetadata
|
|
1283
|
-
}
|
|
1284
|
-
}
|
|
1285
|
-
};
|
|
1286
|
-
}
|
|
874
|
+
const mistralSpeechResponseSchema = z.object({ audio_data: z.string() });
|
|
875
|
+
//#endregion
|
|
876
|
+
//#region src/mistral-transcription-model-options.ts
|
|
877
|
+
const mistralTranscriptionModelOptions = lazySchema(() => zodSchema(z.object({
|
|
878
|
+
/**
|
|
879
|
+
* The language of the audio, e.g. "en". Providing the language can boost
|
|
880
|
+
* accuracy.
|
|
881
|
+
*/
|
|
882
|
+
language: z.string().min(1).optional(),
|
|
883
|
+
/**
|
|
884
|
+
* The sampling temperature.
|
|
885
|
+
*/
|
|
886
|
+
temperature: z.number().optional(),
|
|
887
|
+
/**
|
|
888
|
+
* The timestamp granularities to include in the transcription response.
|
|
889
|
+
*/
|
|
890
|
+
timestampGranularities: z.array(z.enum(["segment", "word"])).min(1).optional(),
|
|
891
|
+
/**
|
|
892
|
+
* Whether to identify speakers in the transcription.
|
|
893
|
+
*/
|
|
894
|
+
diarize: z.boolean().optional(),
|
|
895
|
+
/**
|
|
896
|
+
* Words or phrases to guide the model toward correct spellings of names,
|
|
897
|
+
* technical terms, or domain-specific vocabulary.
|
|
898
|
+
*
|
|
899
|
+
* Mistral requires each item to omit commas and whitespace. Use
|
|
900
|
+
* underscores to represent multi-word phrases.
|
|
901
|
+
*/
|
|
902
|
+
contextBias: z.array(z.string().min(1).regex(/^[^\s,]+$/, "Context bias items must not contain commas or whitespace.")).max(100).optional()
|
|
903
|
+
})));
|
|
904
|
+
//#endregion
|
|
905
|
+
//#region src/mistral-transcription-model.ts
|
|
906
|
+
var MistralTranscriptionModel = class MistralTranscriptionModel {
|
|
907
|
+
static [WORKFLOW_SERIALIZE](model) {
|
|
908
|
+
return serializeModelOptions({
|
|
909
|
+
modelId: model.modelId,
|
|
910
|
+
config: model.config
|
|
911
|
+
});
|
|
912
|
+
}
|
|
913
|
+
static [WORKFLOW_DESERIALIZE](options) {
|
|
914
|
+
return new MistralTranscriptionModel(options.modelId, options.config);
|
|
915
|
+
}
|
|
916
|
+
get provider() {
|
|
917
|
+
return this.config.provider;
|
|
918
|
+
}
|
|
919
|
+
constructor(modelId, config) {
|
|
920
|
+
this.modelId = modelId;
|
|
921
|
+
this.config = config;
|
|
922
|
+
this.specificationVersion = "v4";
|
|
923
|
+
}
|
|
924
|
+
async getArgs({ audio, mediaType, providerOptions }) {
|
|
925
|
+
const warnings = [];
|
|
926
|
+
const mistralOptions = await parseProviderOptions({
|
|
927
|
+
provider: "mistral",
|
|
928
|
+
providerOptions,
|
|
929
|
+
schema: mistralTranscriptionModelOptions
|
|
930
|
+
});
|
|
931
|
+
if (mistralOptions?.language != null && mistralOptions.timestampGranularities != null) throw new InvalidArgumentError({
|
|
932
|
+
argument: "providerOptions",
|
|
933
|
+
message: "providerOptions.mistral.language cannot be combined with providerOptions.mistral.timestampGranularities"
|
|
934
|
+
});
|
|
935
|
+
const formData = new FormData();
|
|
936
|
+
const blob = audio instanceof Uint8Array ? new Blob([audio]) : new Blob([convertBase64ToUint8Array(audio)]);
|
|
937
|
+
formData.append("model", this.modelId);
|
|
938
|
+
formData.append("file", new File([blob], "audio", { type: mediaType }), `audio.${mediaTypeToExtension(mediaType)}`);
|
|
939
|
+
if (mistralOptions != null) {
|
|
940
|
+
appendFormValue(formData, "language", mistralOptions.language);
|
|
941
|
+
appendFormValue(formData, "temperature", mistralOptions.temperature);
|
|
942
|
+
appendFormValue(formData, "timestamp_granularities", mistralOptions.timestampGranularities);
|
|
943
|
+
appendFormValue(formData, "diarize", mistralOptions.diarize);
|
|
944
|
+
appendFormValue(formData, "context_bias", mistralOptions.contextBias);
|
|
945
|
+
}
|
|
946
|
+
return {
|
|
947
|
+
formData,
|
|
948
|
+
warnings
|
|
949
|
+
};
|
|
950
|
+
}
|
|
951
|
+
async doGenerate(options) {
|
|
952
|
+
const currentDate = this.config._internal?.currentDate?.() ?? /* @__PURE__ */ new Date();
|
|
953
|
+
const { formData, warnings } = await this.getArgs(options);
|
|
954
|
+
const { value: response, responseHeaders, rawValue: rawResponse } = await postFormDataToApi({
|
|
955
|
+
url: `${this.config.baseURL}/audio/transcriptions`,
|
|
956
|
+
headers: combineHeaders(this.config.headers?.(), options.headers),
|
|
957
|
+
formData,
|
|
958
|
+
failedResponseHandler: mistralFailedResponseHandler,
|
|
959
|
+
successfulResponseHandler: createJsonResponseHandler(mistralTranscriptionResponseSchema),
|
|
960
|
+
abortSignal: options.abortSignal,
|
|
961
|
+
fetch: this.config.fetch
|
|
962
|
+
});
|
|
963
|
+
const segments = response.segments?.map((segment) => ({
|
|
964
|
+
text: segment.text,
|
|
965
|
+
startSecond: segment.start,
|
|
966
|
+
endSecond: segment.end
|
|
967
|
+
})) ?? [];
|
|
968
|
+
const mistralMetadata = {};
|
|
969
|
+
if (response.usage != null) mistralMetadata.usage = {
|
|
970
|
+
...response.usage.prompt_tokens != null && { promptTokens: response.usage.prompt_tokens },
|
|
971
|
+
...response.usage.completion_tokens != null && { completionTokens: response.usage.completion_tokens },
|
|
972
|
+
...response.usage.total_tokens != null && { totalTokens: response.usage.total_tokens },
|
|
973
|
+
...response.usage.prompt_audio_seconds != null && { promptAudioSeconds: response.usage.prompt_audio_seconds },
|
|
974
|
+
...response.usage.request_count != null && { requestCount: response.usage.request_count }
|
|
975
|
+
};
|
|
976
|
+
const providerSegments = response.segments?.filter((segment) => segment.type != null || segment.score != null || segment.speaker_id != null).map((segment) => ({
|
|
977
|
+
text: segment.text,
|
|
978
|
+
startSecond: segment.start,
|
|
979
|
+
endSecond: segment.end,
|
|
980
|
+
...segment.type != null && { type: segment.type },
|
|
981
|
+
...segment.score != null && { score: segment.score },
|
|
982
|
+
...segment.speaker_id != null && { speakerId: segment.speaker_id }
|
|
983
|
+
}));
|
|
984
|
+
if (providerSegments != null && providerSegments.length > 0) mistralMetadata.segments = providerSegments;
|
|
985
|
+
return {
|
|
986
|
+
text: response.text,
|
|
987
|
+
segments,
|
|
988
|
+
language: response.language ?? void 0,
|
|
989
|
+
durationInSeconds: response.usage?.prompt_audio_seconds ?? segments.at(-1)?.endSecond ?? void 0,
|
|
990
|
+
warnings,
|
|
991
|
+
response: {
|
|
992
|
+
timestamp: currentDate,
|
|
993
|
+
modelId: response.model,
|
|
994
|
+
headers: responseHeaders,
|
|
995
|
+
body: rawResponse
|
|
996
|
+
},
|
|
997
|
+
...Object.keys(mistralMetadata).length > 0 && { providerMetadata: { mistral: mistralMetadata } }
|
|
998
|
+
};
|
|
999
|
+
}
|
|
1287
1000
|
};
|
|
1288
1001
|
function appendFormValue(formData, key, value) {
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
}
|
|
1296
|
-
return;
|
|
1297
|
-
}
|
|
1298
|
-
formData.append(key, String(value));
|
|
1002
|
+
if (value == null) return;
|
|
1003
|
+
if (Array.isArray(value)) {
|
|
1004
|
+
for (const item of value) formData.append(key, item);
|
|
1005
|
+
return;
|
|
1006
|
+
}
|
|
1007
|
+
formData.append(key, String(value));
|
|
1299
1008
|
}
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
request_count: z10.number().nullish()
|
|
1320
|
-
}).nullish()
|
|
1009
|
+
const mistralTranscriptionResponseSchema = z.object({
|
|
1010
|
+
model: z.string(),
|
|
1011
|
+
text: z.string(),
|
|
1012
|
+
language: z.string().nullish(),
|
|
1013
|
+
segments: z.array(z.object({
|
|
1014
|
+
type: z.literal("transcription_segment").nullish(),
|
|
1015
|
+
text: z.string(),
|
|
1016
|
+
start: z.number(),
|
|
1017
|
+
end: z.number(),
|
|
1018
|
+
score: z.number().nullish(),
|
|
1019
|
+
speaker_id: z.string().nullish()
|
|
1020
|
+
})).nullish(),
|
|
1021
|
+
usage: z.object({
|
|
1022
|
+
prompt_tokens: z.number().nullish(),
|
|
1023
|
+
completion_tokens: z.number().nullish(),
|
|
1024
|
+
total_tokens: z.number().nullish(),
|
|
1025
|
+
prompt_audio_seconds: z.number().nullish(),
|
|
1026
|
+
request_count: z.number().nullish()
|
|
1027
|
+
}).nullish()
|
|
1321
1028
|
});
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1029
|
+
//#endregion
|
|
1030
|
+
//#region src/version.ts
|
|
1031
|
+
const VERSION = "4.0.55";
|
|
1032
|
+
//#endregion
|
|
1033
|
+
//#region src/mistral-provider.ts
|
|
1034
|
+
/**
|
|
1035
|
+
* Create a Mistral AI provider instance.
|
|
1036
|
+
*/
|
|
1327
1037
|
function createMistral(options = {}) {
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
provider.imageModel = (modelId) => {
|
|
1385
|
-
throw new NoSuchModelError({ modelId, modelType: "imageModel" });
|
|
1386
|
-
};
|
|
1387
|
-
return provider;
|
|
1038
|
+
const baseURL = withoutTrailingSlash(options.baseURL) ?? "https://api.mistral.ai/v1";
|
|
1039
|
+
const getHeaders = () => withUserAgentSuffix({
|
|
1040
|
+
Authorization: `Bearer ${loadApiKey({
|
|
1041
|
+
apiKey: options.apiKey,
|
|
1042
|
+
environmentVariableName: "MISTRAL_API_KEY",
|
|
1043
|
+
description: "Mistral"
|
|
1044
|
+
})}`,
|
|
1045
|
+
...options.headers
|
|
1046
|
+
}, `ai-sdk-mistral/${VERSION}`);
|
|
1047
|
+
const createChatModel = (modelId) => new MistralChatLanguageModel(modelId, {
|
|
1048
|
+
provider: "mistral.chat",
|
|
1049
|
+
baseURL,
|
|
1050
|
+
headers: getHeaders,
|
|
1051
|
+
fetch: options.fetch,
|
|
1052
|
+
generateId: options.generateId
|
|
1053
|
+
});
|
|
1054
|
+
const createEmbeddingModel = (modelId) => new MistralEmbeddingModel(modelId, {
|
|
1055
|
+
provider: "mistral.embedding",
|
|
1056
|
+
baseURL,
|
|
1057
|
+
headers: getHeaders,
|
|
1058
|
+
fetch: options.fetch
|
|
1059
|
+
});
|
|
1060
|
+
const createSpeechModel = (modelId) => new MistralSpeechModel(modelId, {
|
|
1061
|
+
provider: "mistral.speech",
|
|
1062
|
+
baseURL,
|
|
1063
|
+
headers: getHeaders,
|
|
1064
|
+
fetch: options.fetch
|
|
1065
|
+
});
|
|
1066
|
+
const createTranscriptionModel = (modelId) => new MistralTranscriptionModel(modelId, {
|
|
1067
|
+
provider: "mistral.transcription",
|
|
1068
|
+
baseURL,
|
|
1069
|
+
headers: getHeaders,
|
|
1070
|
+
fetch: options.fetch
|
|
1071
|
+
});
|
|
1072
|
+
const provider = function(modelId) {
|
|
1073
|
+
if (new.target) throw new Error("The Mistral model function cannot be called with the new keyword.");
|
|
1074
|
+
return createChatModel(modelId);
|
|
1075
|
+
};
|
|
1076
|
+
provider.specificationVersion = "v4";
|
|
1077
|
+
provider.languageModel = createChatModel;
|
|
1078
|
+
provider.chat = createChatModel;
|
|
1079
|
+
provider.embedding = createEmbeddingModel;
|
|
1080
|
+
provider.embeddingModel = createEmbeddingModel;
|
|
1081
|
+
provider.textEmbedding = createEmbeddingModel;
|
|
1082
|
+
provider.textEmbeddingModel = createEmbeddingModel;
|
|
1083
|
+
provider.speech = createSpeechModel;
|
|
1084
|
+
provider.speechModel = createSpeechModel;
|
|
1085
|
+
provider.transcription = createTranscriptionModel;
|
|
1086
|
+
provider.transcriptionModel = createTranscriptionModel;
|
|
1087
|
+
provider.imageModel = (modelId) => {
|
|
1088
|
+
throw new NoSuchModelError({
|
|
1089
|
+
modelId,
|
|
1090
|
+
modelType: "imageModel"
|
|
1091
|
+
});
|
|
1092
|
+
};
|
|
1093
|
+
return provider;
|
|
1388
1094
|
}
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
};
|
|
1095
|
+
/**
|
|
1096
|
+
* Default Mistral provider instance.
|
|
1097
|
+
*/
|
|
1098
|
+
const mistral = createMistral();
|
|
1099
|
+
//#endregion
|
|
1100
|
+
export { VERSION, createMistral, mistral };
|
|
1101
|
+
|
|
1395
1102
|
//# sourceMappingURL=index.js.map
|