@ai-sdk/groq 1.2.9 → 2.0.0-alpha.1
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 +187 -21
- package/dist/index.d.mts +23 -20
- package/dist/index.d.ts +23 -20
- package/dist/index.js +247 -260
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +229 -242
- package/dist/index.mjs.map +1 -1
- package/package.json +13 -12
package/dist/index.js
CHANGED
|
@@ -27,16 +27,15 @@ module.exports = __toCommonJS(src_exports);
|
|
|
27
27
|
|
|
28
28
|
// src/groq-provider.ts
|
|
29
29
|
var import_provider4 = require("@ai-sdk/provider");
|
|
30
|
-
var
|
|
30
|
+
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
|
31
31
|
|
|
32
32
|
// src/groq-chat-language-model.ts
|
|
33
33
|
var import_provider3 = require("@ai-sdk/provider");
|
|
34
|
-
var
|
|
35
|
-
var
|
|
34
|
+
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
35
|
+
var import_zod3 = require("zod");
|
|
36
36
|
|
|
37
37
|
// src/convert-to-groq-chat-messages.ts
|
|
38
38
|
var import_provider = require("@ai-sdk/provider");
|
|
39
|
-
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
40
39
|
function convertToGroqChatMessages(prompt) {
|
|
41
40
|
const messages = [];
|
|
42
41
|
for (const { role, content } of prompt) {
|
|
@@ -53,24 +52,24 @@ function convertToGroqChatMessages(prompt) {
|
|
|
53
52
|
messages.push({
|
|
54
53
|
role: "user",
|
|
55
54
|
content: content.map((part) => {
|
|
56
|
-
var _a;
|
|
57
55
|
switch (part.type) {
|
|
58
56
|
case "text": {
|
|
59
57
|
return { type: "text", text: part.text };
|
|
60
58
|
}
|
|
61
|
-
case "
|
|
59
|
+
case "file": {
|
|
60
|
+
if (!part.mediaType.startsWith("image/")) {
|
|
61
|
+
throw new import_provider.UnsupportedFunctionalityError({
|
|
62
|
+
functionality: "Non-image file content parts"
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
|
|
62
66
|
return {
|
|
63
67
|
type: "image_url",
|
|
64
68
|
image_url: {
|
|
65
|
-
url: part.
|
|
69
|
+
url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${part.data}`
|
|
66
70
|
}
|
|
67
71
|
};
|
|
68
72
|
}
|
|
69
|
-
case "file": {
|
|
70
|
-
throw new import_provider.UnsupportedFunctionalityError({
|
|
71
|
-
functionality: "File content parts in user messages"
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
73
|
}
|
|
75
74
|
})
|
|
76
75
|
});
|
|
@@ -137,16 +136,31 @@ function getResponseMetadata({
|
|
|
137
136
|
};
|
|
138
137
|
}
|
|
139
138
|
|
|
140
|
-
// src/groq-
|
|
139
|
+
// src/groq-chat-options.ts
|
|
141
140
|
var import_zod = require("zod");
|
|
142
|
-
var
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
141
|
+
var groqProviderOptions = import_zod.z.object({
|
|
142
|
+
reasoningFormat: import_zod.z.enum(["parsed", "raw", "hidden"]).optional(),
|
|
143
|
+
/**
|
|
144
|
+
* Whether to enable parallel function calling during tool use. Default to true.
|
|
145
|
+
*/
|
|
146
|
+
parallelToolCalls: import_zod.z.boolean().optional(),
|
|
147
|
+
/**
|
|
148
|
+
* A unique identifier representing your end-user, which can help OpenAI to
|
|
149
|
+
* monitor and detect abuse. Learn more.
|
|
150
|
+
*/
|
|
151
|
+
user: import_zod.z.string().optional()
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// src/groq-error.ts
|
|
155
|
+
var import_zod2 = require("zod");
|
|
156
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
157
|
+
var groqErrorDataSchema = import_zod2.z.object({
|
|
158
|
+
error: import_zod2.z.object({
|
|
159
|
+
message: import_zod2.z.string(),
|
|
160
|
+
type: import_zod2.z.string()
|
|
147
161
|
})
|
|
148
162
|
});
|
|
149
|
-
var groqFailedResponseHandler = (0,
|
|
163
|
+
var groqFailedResponseHandler = (0, import_provider_utils.createJsonErrorResponseHandler)({
|
|
150
164
|
errorSchema: groqErrorDataSchema,
|
|
151
165
|
errorToMessage: (data) => data.error.message
|
|
152
166
|
});
|
|
@@ -154,15 +168,14 @@ var groqFailedResponseHandler = (0, import_provider_utils2.createJsonErrorRespon
|
|
|
154
168
|
// src/groq-prepare-tools.ts
|
|
155
169
|
var import_provider2 = require("@ai-sdk/provider");
|
|
156
170
|
function prepareTools({
|
|
157
|
-
|
|
171
|
+
tools,
|
|
172
|
+
toolChoice
|
|
158
173
|
}) {
|
|
159
|
-
|
|
160
|
-
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
|
|
174
|
+
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
161
175
|
const toolWarnings = [];
|
|
162
176
|
if (tools == null) {
|
|
163
|
-
return { tools: void 0,
|
|
177
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
164
178
|
}
|
|
165
|
-
const toolChoice = mode.toolChoice;
|
|
166
179
|
const groqTools = [];
|
|
167
180
|
for (const tool of tools) {
|
|
168
181
|
if (tool.type === "provider-defined") {
|
|
@@ -179,18 +192,18 @@ function prepareTools({
|
|
|
179
192
|
}
|
|
180
193
|
}
|
|
181
194
|
if (toolChoice == null) {
|
|
182
|
-
return { tools: groqTools,
|
|
195
|
+
return { tools: groqTools, toolChoice: void 0, toolWarnings };
|
|
183
196
|
}
|
|
184
197
|
const type = toolChoice.type;
|
|
185
198
|
switch (type) {
|
|
186
199
|
case "auto":
|
|
187
200
|
case "none":
|
|
188
201
|
case "required":
|
|
189
|
-
return { tools: groqTools,
|
|
202
|
+
return { tools: groqTools, toolChoice: type, toolWarnings };
|
|
190
203
|
case "tool":
|
|
191
204
|
return {
|
|
192
205
|
tools: groqTools,
|
|
193
|
-
|
|
206
|
+
toolChoice: {
|
|
194
207
|
type: "function",
|
|
195
208
|
function: {
|
|
196
209
|
name: toolChoice.toolName
|
|
@@ -201,7 +214,7 @@ function prepareTools({
|
|
|
201
214
|
default: {
|
|
202
215
|
const _exhaustiveCheck = type;
|
|
203
216
|
throw new import_provider2.UnsupportedFunctionalityError({
|
|
204
|
-
functionality: `
|
|
217
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
205
218
|
});
|
|
206
219
|
}
|
|
207
220
|
}
|
|
@@ -226,24 +239,20 @@ function mapGroqFinishReason(finishReason) {
|
|
|
226
239
|
|
|
227
240
|
// src/groq-chat-language-model.ts
|
|
228
241
|
var GroqChatLanguageModel = class {
|
|
229
|
-
constructor(modelId,
|
|
230
|
-
this.specificationVersion = "
|
|
231
|
-
this.
|
|
232
|
-
|
|
242
|
+
constructor(modelId, config) {
|
|
243
|
+
this.specificationVersion = "v2";
|
|
244
|
+
this.supportedUrls = {
|
|
245
|
+
"image/*": [/^https?:\/\/.*$/]
|
|
246
|
+
};
|
|
233
247
|
this.modelId = modelId;
|
|
234
|
-
this.settings = settings;
|
|
235
248
|
this.config = config;
|
|
236
249
|
}
|
|
237
250
|
get provider() {
|
|
238
251
|
return this.config.provider;
|
|
239
252
|
}
|
|
240
|
-
|
|
241
|
-
return !this.settings.downloadImages;
|
|
242
|
-
}
|
|
243
|
-
getArgs({
|
|
244
|
-
mode,
|
|
253
|
+
async getArgs({
|
|
245
254
|
prompt,
|
|
246
|
-
|
|
255
|
+
maxOutputTokens,
|
|
247
256
|
temperature,
|
|
248
257
|
topP,
|
|
249
258
|
topK,
|
|
@@ -253,9 +262,10 @@ var GroqChatLanguageModel = class {
|
|
|
253
262
|
responseFormat,
|
|
254
263
|
seed,
|
|
255
264
|
stream,
|
|
256
|
-
|
|
265
|
+
tools,
|
|
266
|
+
toolChoice,
|
|
267
|
+
providerOptions
|
|
257
268
|
}) {
|
|
258
|
-
const type = mode.type;
|
|
259
269
|
const warnings = [];
|
|
260
270
|
if (topK != null) {
|
|
261
271
|
warnings.push({
|
|
@@ -270,169 +280,148 @@ var GroqChatLanguageModel = class {
|
|
|
270
280
|
details: "JSON response format schema is not supported"
|
|
271
281
|
});
|
|
272
282
|
}
|
|
273
|
-
const groqOptions = (0,
|
|
283
|
+
const groqOptions = await (0, import_provider_utils2.parseProviderOptions)({
|
|
274
284
|
provider: "groq",
|
|
275
|
-
providerOptions
|
|
276
|
-
schema:
|
|
277
|
-
reasoningFormat: import_zod2.z.enum(["parsed", "raw", "hidden"]).nullish()
|
|
278
|
-
})
|
|
285
|
+
providerOptions,
|
|
286
|
+
schema: groqProviderOptions
|
|
279
287
|
});
|
|
280
|
-
const
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
288
|
+
const {
|
|
289
|
+
tools: groqTools,
|
|
290
|
+
toolChoice: groqToolChoice,
|
|
291
|
+
toolWarnings
|
|
292
|
+
} = prepareTools({ tools, toolChoice });
|
|
293
|
+
return {
|
|
294
|
+
args: {
|
|
295
|
+
// model id:
|
|
296
|
+
model: this.modelId,
|
|
297
|
+
// model specific settings:
|
|
298
|
+
user: groqOptions == null ? void 0 : groqOptions.user,
|
|
299
|
+
parallel_tool_calls: groqOptions == null ? void 0 : groqOptions.parallelToolCalls,
|
|
300
|
+
// standardized settings:
|
|
301
|
+
max_tokens: maxOutputTokens,
|
|
302
|
+
temperature,
|
|
303
|
+
top_p: topP,
|
|
304
|
+
frequency_penalty: frequencyPenalty,
|
|
305
|
+
presence_penalty: presencePenalty,
|
|
306
|
+
stop: stopSequences,
|
|
307
|
+
seed,
|
|
308
|
+
// response format:
|
|
309
|
+
response_format: (
|
|
310
|
+
// json object response format is not supported for streaming:
|
|
311
|
+
stream === false && (responseFormat == null ? void 0 : responseFormat.type) === "json" ? { type: "json_object" } : void 0
|
|
312
|
+
),
|
|
313
|
+
// provider options:
|
|
314
|
+
reasoning_format: groqOptions == null ? void 0 : groqOptions.reasoningFormat,
|
|
315
|
+
// messages:
|
|
316
|
+
messages: convertToGroqChatMessages(prompt),
|
|
317
|
+
// tools:
|
|
318
|
+
tools: groqTools,
|
|
319
|
+
tool_choice: groqToolChoice
|
|
320
|
+
},
|
|
321
|
+
warnings: [...warnings, ...toolWarnings]
|
|
303
322
|
};
|
|
304
|
-
switch (type) {
|
|
305
|
-
case "regular": {
|
|
306
|
-
const { tools, tool_choice, toolWarnings } = prepareTools({ mode });
|
|
307
|
-
return {
|
|
308
|
-
args: {
|
|
309
|
-
...baseArgs,
|
|
310
|
-
tools,
|
|
311
|
-
tool_choice
|
|
312
|
-
},
|
|
313
|
-
warnings: [...warnings, ...toolWarnings]
|
|
314
|
-
};
|
|
315
|
-
}
|
|
316
|
-
case "object-json": {
|
|
317
|
-
return {
|
|
318
|
-
args: {
|
|
319
|
-
...baseArgs,
|
|
320
|
-
response_format: (
|
|
321
|
-
// json object response format is not supported for streaming:
|
|
322
|
-
stream === false ? { type: "json_object" } : void 0
|
|
323
|
-
)
|
|
324
|
-
},
|
|
325
|
-
warnings
|
|
326
|
-
};
|
|
327
|
-
}
|
|
328
|
-
case "object-tool": {
|
|
329
|
-
return {
|
|
330
|
-
args: {
|
|
331
|
-
...baseArgs,
|
|
332
|
-
tool_choice: {
|
|
333
|
-
type: "function",
|
|
334
|
-
function: { name: mode.tool.name }
|
|
335
|
-
},
|
|
336
|
-
tools: [
|
|
337
|
-
{
|
|
338
|
-
type: "function",
|
|
339
|
-
function: {
|
|
340
|
-
name: mode.tool.name,
|
|
341
|
-
description: mode.tool.description,
|
|
342
|
-
parameters: mode.tool.parameters
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
]
|
|
346
|
-
},
|
|
347
|
-
warnings
|
|
348
|
-
};
|
|
349
|
-
}
|
|
350
|
-
default: {
|
|
351
|
-
const _exhaustiveCheck = type;
|
|
352
|
-
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
323
|
}
|
|
356
324
|
async doGenerate(options) {
|
|
357
325
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
358
|
-
const { args, warnings } = this.getArgs({
|
|
326
|
+
const { args, warnings } = await this.getArgs({
|
|
327
|
+
...options,
|
|
328
|
+
stream: false
|
|
329
|
+
});
|
|
359
330
|
const body = JSON.stringify(args);
|
|
360
331
|
const {
|
|
361
332
|
responseHeaders,
|
|
362
333
|
value: response,
|
|
363
334
|
rawValue: rawResponse
|
|
364
|
-
} = await (0,
|
|
335
|
+
} = await (0, import_provider_utils2.postJsonToApi)({
|
|
365
336
|
url: this.config.url({
|
|
366
337
|
path: "/chat/completions",
|
|
367
338
|
modelId: this.modelId
|
|
368
339
|
}),
|
|
369
|
-
headers: (0,
|
|
340
|
+
headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
|
|
370
341
|
body: args,
|
|
371
342
|
failedResponseHandler: groqFailedResponseHandler,
|
|
372
|
-
successfulResponseHandler: (0,
|
|
343
|
+
successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)(
|
|
373
344
|
groqChatResponseSchema
|
|
374
345
|
),
|
|
375
346
|
abortSignal: options.abortSignal,
|
|
376
347
|
fetch: this.config.fetch
|
|
377
348
|
});
|
|
378
|
-
const { messages: rawPrompt, ...rawSettings } = args;
|
|
379
349
|
const choice = response.choices[0];
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
350
|
+
const content = [];
|
|
351
|
+
const text = choice.message.content;
|
|
352
|
+
if (text != null && text.length > 0) {
|
|
353
|
+
content.push({ type: "text", text });
|
|
354
|
+
}
|
|
355
|
+
const reasoning = choice.message.reasoning;
|
|
356
|
+
if (reasoning != null && reasoning.length > 0) {
|
|
357
|
+
content.push({
|
|
358
|
+
type: "reasoning",
|
|
359
|
+
text: reasoning
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
if (choice.message.tool_calls != null) {
|
|
363
|
+
for (const toolCall of choice.message.tool_calls) {
|
|
364
|
+
content.push({
|
|
365
|
+
type: "tool-call",
|
|
386
366
|
toolCallType: "function",
|
|
387
|
-
toolCallId: (
|
|
367
|
+
toolCallId: (_a = toolCall.id) != null ? _a : (0, import_provider_utils2.generateId)(),
|
|
388
368
|
toolName: toolCall.function.name,
|
|
389
369
|
args: toolCall.function.arguments
|
|
390
|
-
};
|
|
391
|
-
}
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
return {
|
|
374
|
+
content,
|
|
392
375
|
finishReason: mapGroqFinishReason(choice.finish_reason),
|
|
393
376
|
usage: {
|
|
394
|
-
|
|
395
|
-
|
|
377
|
+
inputTokens: (_c = (_b = response.usage) == null ? void 0 : _b.prompt_tokens) != null ? _c : void 0,
|
|
378
|
+
outputTokens: (_e = (_d = response.usage) == null ? void 0 : _d.completion_tokens) != null ? _e : void 0,
|
|
379
|
+
totalTokens: (_g = (_f = response.usage) == null ? void 0 : _f.total_tokens) != null ? _g : void 0
|
|
380
|
+
},
|
|
381
|
+
response: {
|
|
382
|
+
...getResponseMetadata(response),
|
|
383
|
+
headers: responseHeaders,
|
|
384
|
+
body: rawResponse
|
|
396
385
|
},
|
|
397
|
-
rawCall: { rawPrompt, rawSettings },
|
|
398
|
-
rawResponse: { headers: responseHeaders, body: rawResponse },
|
|
399
|
-
response: getResponseMetadata(response),
|
|
400
386
|
warnings,
|
|
401
387
|
request: { body }
|
|
402
388
|
};
|
|
403
389
|
}
|
|
404
390
|
async doStream(options) {
|
|
405
|
-
const { args, warnings } = this.getArgs({ ...options, stream: true });
|
|
391
|
+
const { args, warnings } = await this.getArgs({ ...options, stream: true });
|
|
406
392
|
const body = JSON.stringify({ ...args, stream: true });
|
|
407
|
-
const { responseHeaders, value: response } = await (0,
|
|
393
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
|
|
408
394
|
url: this.config.url({
|
|
409
395
|
path: "/chat/completions",
|
|
410
396
|
modelId: this.modelId
|
|
411
397
|
}),
|
|
412
|
-
headers: (0,
|
|
398
|
+
headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
|
|
413
399
|
body: {
|
|
414
400
|
...args,
|
|
415
401
|
stream: true
|
|
416
402
|
},
|
|
417
403
|
failedResponseHandler: groqFailedResponseHandler,
|
|
418
|
-
successfulResponseHandler: (0,
|
|
404
|
+
successfulResponseHandler: (0, import_provider_utils2.createEventSourceResponseHandler)(groqChatChunkSchema),
|
|
419
405
|
abortSignal: options.abortSignal,
|
|
420
406
|
fetch: this.config.fetch
|
|
421
407
|
});
|
|
422
|
-
const { messages: rawPrompt, ...rawSettings } = args;
|
|
423
408
|
const toolCalls = [];
|
|
424
409
|
let finishReason = "unknown";
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
410
|
+
const usage = {
|
|
411
|
+
inputTokens: void 0,
|
|
412
|
+
outputTokens: void 0,
|
|
413
|
+
totalTokens: void 0
|
|
428
414
|
};
|
|
429
415
|
let isFirstChunk = true;
|
|
430
416
|
let providerMetadata;
|
|
431
417
|
return {
|
|
432
418
|
stream: response.pipeThrough(
|
|
433
419
|
new TransformStream({
|
|
420
|
+
start(controller) {
|
|
421
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
422
|
+
},
|
|
434
423
|
transform(chunk, controller) {
|
|
435
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
|
|
424
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
|
|
436
425
|
if (!chunk.success) {
|
|
437
426
|
finishReason = "error";
|
|
438
427
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
@@ -452,10 +441,9 @@ var GroqChatLanguageModel = class {
|
|
|
452
441
|
});
|
|
453
442
|
}
|
|
454
443
|
if (((_a = value.x_groq) == null ? void 0 : _a.usage) != null) {
|
|
455
|
-
usage =
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
};
|
|
444
|
+
usage.inputTokens = (_b = value.x_groq.usage.prompt_tokens) != null ? _b : void 0;
|
|
445
|
+
usage.outputTokens = (_c = value.x_groq.usage.completion_tokens) != null ? _c : void 0;
|
|
446
|
+
usage.totalTokens = (_d = value.x_groq.usage.total_tokens) != null ? _d : void 0;
|
|
459
447
|
}
|
|
460
448
|
const choice = value.choices[0];
|
|
461
449
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
@@ -468,13 +456,13 @@ var GroqChatLanguageModel = class {
|
|
|
468
456
|
if (delta.reasoning != null && delta.reasoning.length > 0) {
|
|
469
457
|
controller.enqueue({
|
|
470
458
|
type: "reasoning",
|
|
471
|
-
|
|
459
|
+
text: delta.reasoning
|
|
472
460
|
});
|
|
473
461
|
}
|
|
474
462
|
if (delta.content != null && delta.content.length > 0) {
|
|
475
463
|
controller.enqueue({
|
|
476
|
-
type: "text
|
|
477
|
-
|
|
464
|
+
type: "text",
|
|
465
|
+
text: delta.content
|
|
478
466
|
});
|
|
479
467
|
}
|
|
480
468
|
if (delta.tool_calls != null) {
|
|
@@ -493,7 +481,7 @@ var GroqChatLanguageModel = class {
|
|
|
493
481
|
message: `Expected 'id' to be a string.`
|
|
494
482
|
});
|
|
495
483
|
}
|
|
496
|
-
if (((
|
|
484
|
+
if (((_e = toolCallDelta.function) == null ? void 0 : _e.name) == null) {
|
|
497
485
|
throw new import_provider3.InvalidResponseDataError({
|
|
498
486
|
data: toolCallDelta,
|
|
499
487
|
message: `Expected 'function.name' to be a string.`
|
|
@@ -504,12 +492,12 @@ var GroqChatLanguageModel = class {
|
|
|
504
492
|
type: "function",
|
|
505
493
|
function: {
|
|
506
494
|
name: toolCallDelta.function.name,
|
|
507
|
-
arguments: (
|
|
495
|
+
arguments: (_f = toolCallDelta.function.arguments) != null ? _f : ""
|
|
508
496
|
},
|
|
509
497
|
hasFinished: false
|
|
510
498
|
};
|
|
511
499
|
const toolCall2 = toolCalls[index];
|
|
512
|
-
if (((
|
|
500
|
+
if (((_g = toolCall2.function) == null ? void 0 : _g.name) != null && ((_h = toolCall2.function) == null ? void 0 : _h.arguments) != null) {
|
|
513
501
|
if (toolCall2.function.arguments.length > 0) {
|
|
514
502
|
controller.enqueue({
|
|
515
503
|
type: "tool-call-delta",
|
|
@@ -519,11 +507,11 @@ var GroqChatLanguageModel = class {
|
|
|
519
507
|
argsTextDelta: toolCall2.function.arguments
|
|
520
508
|
});
|
|
521
509
|
}
|
|
522
|
-
if ((0,
|
|
510
|
+
if ((0, import_provider_utils2.isParsableJson)(toolCall2.function.arguments)) {
|
|
523
511
|
controller.enqueue({
|
|
524
512
|
type: "tool-call",
|
|
525
513
|
toolCallType: "function",
|
|
526
|
-
toolCallId: (
|
|
514
|
+
toolCallId: (_i = toolCall2.id) != null ? _i : (0, import_provider_utils2.generateId)(),
|
|
527
515
|
toolName: toolCall2.function.name,
|
|
528
516
|
args: toolCall2.function.arguments
|
|
529
517
|
});
|
|
@@ -536,21 +524,21 @@ var GroqChatLanguageModel = class {
|
|
|
536
524
|
if (toolCall.hasFinished) {
|
|
537
525
|
continue;
|
|
538
526
|
}
|
|
539
|
-
if (((
|
|
540
|
-
toolCall.function.arguments += (
|
|
527
|
+
if (((_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null) {
|
|
528
|
+
toolCall.function.arguments += (_l = (_k = toolCallDelta.function) == null ? void 0 : _k.arguments) != null ? _l : "";
|
|
541
529
|
}
|
|
542
530
|
controller.enqueue({
|
|
543
531
|
type: "tool-call-delta",
|
|
544
532
|
toolCallType: "function",
|
|
545
533
|
toolCallId: toolCall.id,
|
|
546
534
|
toolName: toolCall.function.name,
|
|
547
|
-
argsTextDelta: (
|
|
535
|
+
argsTextDelta: (_m = toolCallDelta.function.arguments) != null ? _m : ""
|
|
548
536
|
});
|
|
549
|
-
if (((
|
|
537
|
+
if (((_n = toolCall.function) == null ? void 0 : _n.name) != null && ((_o = toolCall.function) == null ? void 0 : _o.arguments) != null && (0, import_provider_utils2.isParsableJson)(toolCall.function.arguments)) {
|
|
550
538
|
controller.enqueue({
|
|
551
539
|
type: "tool-call",
|
|
552
540
|
toolCallType: "function",
|
|
553
|
-
toolCallId: (
|
|
541
|
+
toolCallId: (_p = toolCall.id) != null ? _p : (0, import_provider_utils2.generateId)(),
|
|
554
542
|
toolName: toolCall.function.name,
|
|
555
543
|
args: toolCall.function.arguments
|
|
556
544
|
});
|
|
@@ -560,85 +548,81 @@ var GroqChatLanguageModel = class {
|
|
|
560
548
|
}
|
|
561
549
|
},
|
|
562
550
|
flush(controller) {
|
|
563
|
-
var _a, _b;
|
|
564
551
|
controller.enqueue({
|
|
565
552
|
type: "finish",
|
|
566
553
|
finishReason,
|
|
567
|
-
usage
|
|
568
|
-
promptTokens: (_a = usage.promptTokens) != null ? _a : NaN,
|
|
569
|
-
completionTokens: (_b = usage.completionTokens) != null ? _b : NaN
|
|
570
|
-
},
|
|
554
|
+
usage,
|
|
571
555
|
...providerMetadata != null ? { providerMetadata } : {}
|
|
572
556
|
});
|
|
573
557
|
}
|
|
574
558
|
})
|
|
575
559
|
),
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
warnings,
|
|
579
|
-
request: { body }
|
|
560
|
+
request: { body },
|
|
561
|
+
response: { headers: responseHeaders }
|
|
580
562
|
};
|
|
581
563
|
}
|
|
582
564
|
};
|
|
583
|
-
var groqChatResponseSchema =
|
|
584
|
-
id:
|
|
585
|
-
created:
|
|
586
|
-
model:
|
|
587
|
-
choices:
|
|
588
|
-
|
|
589
|
-
message:
|
|
590
|
-
content:
|
|
591
|
-
reasoning:
|
|
592
|
-
tool_calls:
|
|
593
|
-
|
|
594
|
-
id:
|
|
595
|
-
type:
|
|
596
|
-
function:
|
|
597
|
-
name:
|
|
598
|
-
arguments:
|
|
565
|
+
var groqChatResponseSchema = import_zod3.z.object({
|
|
566
|
+
id: import_zod3.z.string().nullish(),
|
|
567
|
+
created: import_zod3.z.number().nullish(),
|
|
568
|
+
model: import_zod3.z.string().nullish(),
|
|
569
|
+
choices: import_zod3.z.array(
|
|
570
|
+
import_zod3.z.object({
|
|
571
|
+
message: import_zod3.z.object({
|
|
572
|
+
content: import_zod3.z.string().nullish(),
|
|
573
|
+
reasoning: import_zod3.z.string().nullish(),
|
|
574
|
+
tool_calls: import_zod3.z.array(
|
|
575
|
+
import_zod3.z.object({
|
|
576
|
+
id: import_zod3.z.string().nullish(),
|
|
577
|
+
type: import_zod3.z.literal("function"),
|
|
578
|
+
function: import_zod3.z.object({
|
|
579
|
+
name: import_zod3.z.string(),
|
|
580
|
+
arguments: import_zod3.z.string()
|
|
599
581
|
})
|
|
600
582
|
})
|
|
601
583
|
).nullish()
|
|
602
584
|
}),
|
|
603
|
-
index:
|
|
604
|
-
finish_reason:
|
|
585
|
+
index: import_zod3.z.number(),
|
|
586
|
+
finish_reason: import_zod3.z.string().nullish()
|
|
605
587
|
})
|
|
606
588
|
),
|
|
607
|
-
usage:
|
|
608
|
-
prompt_tokens:
|
|
609
|
-
completion_tokens:
|
|
589
|
+
usage: import_zod3.z.object({
|
|
590
|
+
prompt_tokens: import_zod3.z.number().nullish(),
|
|
591
|
+
completion_tokens: import_zod3.z.number().nullish(),
|
|
592
|
+
total_tokens: import_zod3.z.number().nullish()
|
|
610
593
|
}).nullish()
|
|
611
594
|
});
|
|
612
|
-
var groqChatChunkSchema =
|
|
613
|
-
|
|
614
|
-
id:
|
|
615
|
-
created:
|
|
616
|
-
model:
|
|
617
|
-
choices:
|
|
618
|
-
|
|
619
|
-
delta:
|
|
620
|
-
content:
|
|
621
|
-
reasoning:
|
|
622
|
-
tool_calls:
|
|
623
|
-
|
|
624
|
-
index:
|
|
625
|
-
id:
|
|
626
|
-
type:
|
|
627
|
-
function:
|
|
628
|
-
name:
|
|
629
|
-
arguments:
|
|
595
|
+
var groqChatChunkSchema = import_zod3.z.union([
|
|
596
|
+
import_zod3.z.object({
|
|
597
|
+
id: import_zod3.z.string().nullish(),
|
|
598
|
+
created: import_zod3.z.number().nullish(),
|
|
599
|
+
model: import_zod3.z.string().nullish(),
|
|
600
|
+
choices: import_zod3.z.array(
|
|
601
|
+
import_zod3.z.object({
|
|
602
|
+
delta: import_zod3.z.object({
|
|
603
|
+
content: import_zod3.z.string().nullish(),
|
|
604
|
+
reasoning: import_zod3.z.string().nullish(),
|
|
605
|
+
tool_calls: import_zod3.z.array(
|
|
606
|
+
import_zod3.z.object({
|
|
607
|
+
index: import_zod3.z.number(),
|
|
608
|
+
id: import_zod3.z.string().nullish(),
|
|
609
|
+
type: import_zod3.z.literal("function").optional(),
|
|
610
|
+
function: import_zod3.z.object({
|
|
611
|
+
name: import_zod3.z.string().nullish(),
|
|
612
|
+
arguments: import_zod3.z.string().nullish()
|
|
630
613
|
})
|
|
631
614
|
})
|
|
632
615
|
).nullish()
|
|
633
616
|
}).nullish(),
|
|
634
|
-
finish_reason:
|
|
635
|
-
index:
|
|
617
|
+
finish_reason: import_zod3.z.string().nullable().optional(),
|
|
618
|
+
index: import_zod3.z.number()
|
|
636
619
|
})
|
|
637
620
|
),
|
|
638
|
-
x_groq:
|
|
639
|
-
usage:
|
|
640
|
-
prompt_tokens:
|
|
641
|
-
completion_tokens:
|
|
621
|
+
x_groq: import_zod3.z.object({
|
|
622
|
+
usage: import_zod3.z.object({
|
|
623
|
+
prompt_tokens: import_zod3.z.number().nullish(),
|
|
624
|
+
completion_tokens: import_zod3.z.number().nullish(),
|
|
625
|
+
total_tokens: import_zod3.z.number().nullish()
|
|
642
626
|
}).nullish()
|
|
643
627
|
}).nullish()
|
|
644
628
|
}),
|
|
@@ -646,14 +630,14 @@ var groqChatChunkSchema = import_zod2.z.union([
|
|
|
646
630
|
]);
|
|
647
631
|
|
|
648
632
|
// src/groq-transcription-model.ts
|
|
649
|
-
var
|
|
650
|
-
var
|
|
651
|
-
var groqProviderOptionsSchema =
|
|
652
|
-
language:
|
|
653
|
-
prompt:
|
|
654
|
-
responseFormat:
|
|
655
|
-
temperature:
|
|
656
|
-
timestampGranularities:
|
|
633
|
+
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
634
|
+
var import_zod4 = require("zod");
|
|
635
|
+
var groqProviderOptionsSchema = import_zod4.z.object({
|
|
636
|
+
language: import_zod4.z.string().nullish(),
|
|
637
|
+
prompt: import_zod4.z.string().nullish(),
|
|
638
|
+
responseFormat: import_zod4.z.string().nullish(),
|
|
639
|
+
temperature: import_zod4.z.number().min(0).max(1).nullish(),
|
|
640
|
+
timestampGranularities: import_zod4.z.array(import_zod4.z.string()).nullish()
|
|
657
641
|
});
|
|
658
642
|
var GroqTranscriptionModel = class {
|
|
659
643
|
constructor(modelId, config) {
|
|
@@ -664,20 +648,20 @@ var GroqTranscriptionModel = class {
|
|
|
664
648
|
get provider() {
|
|
665
649
|
return this.config.provider;
|
|
666
650
|
}
|
|
667
|
-
getArgs({
|
|
651
|
+
async getArgs({
|
|
668
652
|
audio,
|
|
669
653
|
mediaType,
|
|
670
654
|
providerOptions
|
|
671
655
|
}) {
|
|
672
656
|
var _a, _b, _c, _d, _e;
|
|
673
657
|
const warnings = [];
|
|
674
|
-
const groqOptions = (0,
|
|
658
|
+
const groqOptions = await (0, import_provider_utils3.parseProviderOptions)({
|
|
675
659
|
provider: "groq",
|
|
676
660
|
providerOptions,
|
|
677
661
|
schema: groqProviderOptionsSchema
|
|
678
662
|
});
|
|
679
663
|
const formData = new FormData();
|
|
680
|
-
const blob = audio instanceof Uint8Array ? new Blob([audio]) : new Blob([(0,
|
|
664
|
+
const blob = audio instanceof Uint8Array ? new Blob([audio]) : new Blob([(0, import_provider_utils3.convertBase64ToUint8Array)(audio)]);
|
|
681
665
|
formData.append("model", this.modelId);
|
|
682
666
|
formData.append("file", new File([blob], "audio", { type: mediaType }));
|
|
683
667
|
if (groqOptions) {
|
|
@@ -703,20 +687,20 @@ var GroqTranscriptionModel = class {
|
|
|
703
687
|
async doGenerate(options) {
|
|
704
688
|
var _a, _b, _c, _d, _e;
|
|
705
689
|
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
706
|
-
const { formData, warnings } = this.getArgs(options);
|
|
690
|
+
const { formData, warnings } = await this.getArgs(options);
|
|
707
691
|
const {
|
|
708
692
|
value: response,
|
|
709
693
|
responseHeaders,
|
|
710
694
|
rawValue: rawResponse
|
|
711
|
-
} = await (0,
|
|
695
|
+
} = await (0, import_provider_utils3.postFormDataToApi)({
|
|
712
696
|
url: this.config.url({
|
|
713
697
|
path: "/audio/transcriptions",
|
|
714
698
|
modelId: this.modelId
|
|
715
699
|
}),
|
|
716
|
-
headers: (0,
|
|
700
|
+
headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers),
|
|
717
701
|
formData,
|
|
718
702
|
failedResponseHandler: groqFailedResponseHandler,
|
|
719
|
-
successfulResponseHandler: (0,
|
|
703
|
+
successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
|
|
720
704
|
groqTranscriptionResponseSchema
|
|
721
705
|
),
|
|
722
706
|
abortSignal: options.abortSignal,
|
|
@@ -741,55 +725,55 @@ var GroqTranscriptionModel = class {
|
|
|
741
725
|
};
|
|
742
726
|
}
|
|
743
727
|
};
|
|
744
|
-
var groqTranscriptionResponseSchema =
|
|
745
|
-
task:
|
|
746
|
-
language:
|
|
747
|
-
duration:
|
|
748
|
-
text:
|
|
749
|
-
segments:
|
|
750
|
-
|
|
751
|
-
id:
|
|
752
|
-
seek:
|
|
753
|
-
start:
|
|
754
|
-
end:
|
|
755
|
-
text:
|
|
756
|
-
tokens:
|
|
757
|
-
temperature:
|
|
758
|
-
avg_logprob:
|
|
759
|
-
compression_ratio:
|
|
760
|
-
no_speech_prob:
|
|
728
|
+
var groqTranscriptionResponseSchema = import_zod4.z.object({
|
|
729
|
+
task: import_zod4.z.string(),
|
|
730
|
+
language: import_zod4.z.string(),
|
|
731
|
+
duration: import_zod4.z.number(),
|
|
732
|
+
text: import_zod4.z.string(),
|
|
733
|
+
segments: import_zod4.z.array(
|
|
734
|
+
import_zod4.z.object({
|
|
735
|
+
id: import_zod4.z.number(),
|
|
736
|
+
seek: import_zod4.z.number(),
|
|
737
|
+
start: import_zod4.z.number(),
|
|
738
|
+
end: import_zod4.z.number(),
|
|
739
|
+
text: import_zod4.z.string(),
|
|
740
|
+
tokens: import_zod4.z.array(import_zod4.z.number()),
|
|
741
|
+
temperature: import_zod4.z.number(),
|
|
742
|
+
avg_logprob: import_zod4.z.number(),
|
|
743
|
+
compression_ratio: import_zod4.z.number(),
|
|
744
|
+
no_speech_prob: import_zod4.z.number()
|
|
761
745
|
})
|
|
762
746
|
),
|
|
763
|
-
x_groq:
|
|
764
|
-
id:
|
|
747
|
+
x_groq: import_zod4.z.object({
|
|
748
|
+
id: import_zod4.z.string()
|
|
765
749
|
})
|
|
766
750
|
});
|
|
767
751
|
|
|
768
752
|
// src/groq-provider.ts
|
|
769
753
|
function createGroq(options = {}) {
|
|
770
754
|
var _a;
|
|
771
|
-
const baseURL = (_a = (0,
|
|
755
|
+
const baseURL = (_a = (0, import_provider_utils4.withoutTrailingSlash)(options.baseURL)) != null ? _a : "https://api.groq.com/openai/v1";
|
|
772
756
|
const getHeaders = () => ({
|
|
773
|
-
Authorization: `Bearer ${(0,
|
|
757
|
+
Authorization: `Bearer ${(0, import_provider_utils4.loadApiKey)({
|
|
774
758
|
apiKey: options.apiKey,
|
|
775
759
|
environmentVariableName: "GROQ_API_KEY",
|
|
776
760
|
description: "Groq"
|
|
777
761
|
})}`,
|
|
778
762
|
...options.headers
|
|
779
763
|
});
|
|
780
|
-
const createChatModel = (modelId
|
|
764
|
+
const createChatModel = (modelId) => new GroqChatLanguageModel(modelId, {
|
|
781
765
|
provider: "groq.chat",
|
|
782
766
|
url: ({ path }) => `${baseURL}${path}`,
|
|
783
767
|
headers: getHeaders,
|
|
784
768
|
fetch: options.fetch
|
|
785
769
|
});
|
|
786
|
-
const createLanguageModel = (modelId
|
|
770
|
+
const createLanguageModel = (modelId) => {
|
|
787
771
|
if (new.target) {
|
|
788
772
|
throw new Error(
|
|
789
773
|
"The Groq model function cannot be called with the new keyword."
|
|
790
774
|
);
|
|
791
775
|
}
|
|
792
|
-
return createChatModel(modelId
|
|
776
|
+
return createChatModel(modelId);
|
|
793
777
|
};
|
|
794
778
|
const createTranscriptionModel = (modelId) => {
|
|
795
779
|
return new GroqTranscriptionModel(modelId, {
|
|
@@ -799,14 +783,17 @@ function createGroq(options = {}) {
|
|
|
799
783
|
fetch: options.fetch
|
|
800
784
|
});
|
|
801
785
|
};
|
|
802
|
-
const provider = function(modelId
|
|
803
|
-
return createLanguageModel(modelId
|
|
786
|
+
const provider = function(modelId) {
|
|
787
|
+
return createLanguageModel(modelId);
|
|
804
788
|
};
|
|
805
789
|
provider.languageModel = createLanguageModel;
|
|
806
790
|
provider.chat = createChatModel;
|
|
807
791
|
provider.textEmbeddingModel = (modelId) => {
|
|
808
792
|
throw new import_provider4.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
|
|
809
793
|
};
|
|
794
|
+
provider.imageModel = (modelId) => {
|
|
795
|
+
throw new import_provider4.NoSuchModelError({ modelId, modelType: "imageModel" });
|
|
796
|
+
};
|
|
810
797
|
provider.transcription = createTranscriptionModel;
|
|
811
798
|
return provider;
|
|
812
799
|
}
|