@ai-sdk/groq 2.0.0-canary.8 → 2.0.0
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 +451 -0
- package/README.md +2 -2
- package/dist/index.d.mts +20 -34
- package/dist/index.d.ts +20 -34
- package/dist/index.js +335 -139
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +290 -88
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -8
package/dist/index.js
CHANGED
|
@@ -27,12 +27,12 @@ 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
34
|
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
35
|
-
var
|
|
35
|
+
var import_v43 = require("zod/v4");
|
|
36
36
|
|
|
37
37
|
// src/convert-to-groq-chat-messages.ts
|
|
38
38
|
var import_provider = require("@ai-sdk/provider");
|
|
@@ -90,7 +90,7 @@ function convertToGroqChatMessages(prompt) {
|
|
|
90
90
|
type: "function",
|
|
91
91
|
function: {
|
|
92
92
|
name: part.toolName,
|
|
93
|
-
arguments: JSON.stringify(part.
|
|
93
|
+
arguments: JSON.stringify(part.input)
|
|
94
94
|
}
|
|
95
95
|
});
|
|
96
96
|
break;
|
|
@@ -106,10 +106,23 @@ function convertToGroqChatMessages(prompt) {
|
|
|
106
106
|
}
|
|
107
107
|
case "tool": {
|
|
108
108
|
for (const toolResponse of content) {
|
|
109
|
+
const output = toolResponse.output;
|
|
110
|
+
let contentValue;
|
|
111
|
+
switch (output.type) {
|
|
112
|
+
case "text":
|
|
113
|
+
case "error-text":
|
|
114
|
+
contentValue = output.value;
|
|
115
|
+
break;
|
|
116
|
+
case "content":
|
|
117
|
+
case "json":
|
|
118
|
+
case "error-json":
|
|
119
|
+
contentValue = JSON.stringify(output.value);
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
109
122
|
messages.push({
|
|
110
123
|
role: "tool",
|
|
111
124
|
tool_call_id: toolResponse.toolCallId,
|
|
112
|
-
content:
|
|
125
|
+
content: contentValue
|
|
113
126
|
});
|
|
114
127
|
}
|
|
115
128
|
break;
|
|
@@ -137,27 +150,33 @@ function getResponseMetadata({
|
|
|
137
150
|
}
|
|
138
151
|
|
|
139
152
|
// src/groq-chat-options.ts
|
|
140
|
-
var
|
|
141
|
-
var groqProviderOptions =
|
|
142
|
-
reasoningFormat:
|
|
153
|
+
var import_v4 = require("zod/v4");
|
|
154
|
+
var groqProviderOptions = import_v4.z.object({
|
|
155
|
+
reasoningFormat: import_v4.z.enum(["parsed", "raw", "hidden"]).optional(),
|
|
143
156
|
/**
|
|
144
157
|
* Whether to enable parallel function calling during tool use. Default to true.
|
|
145
158
|
*/
|
|
146
|
-
parallelToolCalls:
|
|
159
|
+
parallelToolCalls: import_v4.z.boolean().optional(),
|
|
147
160
|
/**
|
|
148
161
|
* A unique identifier representing your end-user, which can help OpenAI to
|
|
149
162
|
* monitor and detect abuse. Learn more.
|
|
150
163
|
*/
|
|
151
|
-
user:
|
|
164
|
+
user: import_v4.z.string().optional(),
|
|
165
|
+
/**
|
|
166
|
+
* Whether to use structured outputs.
|
|
167
|
+
*
|
|
168
|
+
* @default true
|
|
169
|
+
*/
|
|
170
|
+
structuredOutputs: import_v4.z.boolean().optional()
|
|
152
171
|
});
|
|
153
172
|
|
|
154
173
|
// src/groq-error.ts
|
|
155
|
-
var
|
|
174
|
+
var import_v42 = require("zod/v4");
|
|
156
175
|
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
157
|
-
var groqErrorDataSchema =
|
|
158
|
-
error:
|
|
159
|
-
message:
|
|
160
|
-
type:
|
|
176
|
+
var groqErrorDataSchema = import_v42.z.object({
|
|
177
|
+
error: import_v42.z.object({
|
|
178
|
+
message: import_v42.z.string(),
|
|
179
|
+
type: import_v42.z.string()
|
|
161
180
|
})
|
|
162
181
|
});
|
|
163
182
|
var groqFailedResponseHandler = (0, import_provider_utils.createJsonErrorResponseHandler)({
|
|
@@ -186,7 +205,7 @@ function prepareTools({
|
|
|
186
205
|
function: {
|
|
187
206
|
name: tool.name,
|
|
188
207
|
description: tool.description,
|
|
189
|
-
parameters: tool.
|
|
208
|
+
parameters: tool.inputSchema
|
|
190
209
|
}
|
|
191
210
|
});
|
|
192
211
|
}
|
|
@@ -239,21 +258,18 @@ function mapGroqFinishReason(finishReason) {
|
|
|
239
258
|
|
|
240
259
|
// src/groq-chat-language-model.ts
|
|
241
260
|
var GroqChatLanguageModel = class {
|
|
242
|
-
constructor(modelId,
|
|
261
|
+
constructor(modelId, config) {
|
|
243
262
|
this.specificationVersion = "v2";
|
|
244
|
-
this.
|
|
245
|
-
|
|
263
|
+
this.supportedUrls = {
|
|
264
|
+
"image/*": [/^https?:\/\/.*$/]
|
|
265
|
+
};
|
|
246
266
|
this.modelId = modelId;
|
|
247
|
-
this.settings = settings;
|
|
248
267
|
this.config = config;
|
|
249
268
|
}
|
|
250
269
|
get provider() {
|
|
251
270
|
return this.config.provider;
|
|
252
271
|
}
|
|
253
|
-
|
|
254
|
-
return !this.settings.downloadImages;
|
|
255
|
-
}
|
|
256
|
-
getArgs({
|
|
272
|
+
async getArgs({
|
|
257
273
|
prompt,
|
|
258
274
|
maxOutputTokens,
|
|
259
275
|
temperature,
|
|
@@ -269,25 +285,27 @@ var GroqChatLanguageModel = class {
|
|
|
269
285
|
toolChoice,
|
|
270
286
|
providerOptions
|
|
271
287
|
}) {
|
|
288
|
+
var _a, _b;
|
|
272
289
|
const warnings = [];
|
|
290
|
+
const groqOptions = await (0, import_provider_utils2.parseProviderOptions)({
|
|
291
|
+
provider: "groq",
|
|
292
|
+
providerOptions,
|
|
293
|
+
schema: groqProviderOptions
|
|
294
|
+
});
|
|
295
|
+
const structuredOutputs = (_a = groqOptions == null ? void 0 : groqOptions.structuredOutputs) != null ? _a : true;
|
|
273
296
|
if (topK != null) {
|
|
274
297
|
warnings.push({
|
|
275
298
|
type: "unsupported-setting",
|
|
276
299
|
setting: "topK"
|
|
277
300
|
});
|
|
278
301
|
}
|
|
279
|
-
if (responseFormat
|
|
302
|
+
if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !structuredOutputs) {
|
|
280
303
|
warnings.push({
|
|
281
304
|
type: "unsupported-setting",
|
|
282
305
|
setting: "responseFormat",
|
|
283
|
-
details: "JSON response format schema is
|
|
306
|
+
details: "JSON response format schema is only supported with structuredOutputs"
|
|
284
307
|
});
|
|
285
308
|
}
|
|
286
|
-
const groqOptions = (0, import_provider_utils2.parseProviderOptions)({
|
|
287
|
-
provider: "groq",
|
|
288
|
-
providerOptions,
|
|
289
|
-
schema: groqProviderOptions
|
|
290
|
-
});
|
|
291
309
|
const {
|
|
292
310
|
tools: groqTools,
|
|
293
311
|
toolChoice: groqToolChoice,
|
|
@@ -309,10 +327,14 @@ var GroqChatLanguageModel = class {
|
|
|
309
327
|
stop: stopSequences,
|
|
310
328
|
seed,
|
|
311
329
|
// response format:
|
|
312
|
-
response_format: (
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
330
|
+
response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? structuredOutputs && responseFormat.schema != null ? {
|
|
331
|
+
type: "json_schema",
|
|
332
|
+
json_schema: {
|
|
333
|
+
schema: responseFormat.schema,
|
|
334
|
+
name: (_b = responseFormat.name) != null ? _b : "response",
|
|
335
|
+
description: responseFormat.description
|
|
336
|
+
}
|
|
337
|
+
} : { type: "json_object" } : void 0,
|
|
316
338
|
// provider options:
|
|
317
339
|
reasoning_format: groqOptions == null ? void 0 : groqOptions.reasoningFormat,
|
|
318
340
|
// messages:
|
|
@@ -325,8 +347,11 @@ var GroqChatLanguageModel = class {
|
|
|
325
347
|
};
|
|
326
348
|
}
|
|
327
349
|
async doGenerate(options) {
|
|
328
|
-
var _a, _b, _c, _d, _e;
|
|
329
|
-
const { args, warnings } = this.getArgs({
|
|
350
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
351
|
+
const { args, warnings } = await this.getArgs({
|
|
352
|
+
...options,
|
|
353
|
+
stream: false
|
|
354
|
+
});
|
|
330
355
|
const body = JSON.stringify(args);
|
|
331
356
|
const {
|
|
332
357
|
responseHeaders,
|
|
@@ -347,29 +372,35 @@ var GroqChatLanguageModel = class {
|
|
|
347
372
|
fetch: this.config.fetch
|
|
348
373
|
});
|
|
349
374
|
const choice = response.choices[0];
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
375
|
+
const content = [];
|
|
376
|
+
const text = choice.message.content;
|
|
377
|
+
if (text != null && text.length > 0) {
|
|
378
|
+
content.push({ type: "text", text });
|
|
379
|
+
}
|
|
380
|
+
const reasoning = choice.message.reasoning;
|
|
381
|
+
if (reasoning != null && reasoning.length > 0) {
|
|
382
|
+
content.push({
|
|
383
|
+
type: "reasoning",
|
|
384
|
+
text: reasoning
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
if (choice.message.tool_calls != null) {
|
|
388
|
+
for (const toolCall of choice.message.tool_calls) {
|
|
389
|
+
content.push({
|
|
362
390
|
type: "tool-call",
|
|
363
|
-
|
|
364
|
-
toolCallId: (_a2 = toolCall.id) != null ? _a2 : (0, import_provider_utils2.generateId)(),
|
|
391
|
+
toolCallId: (_a = toolCall.id) != null ? _a : (0, import_provider_utils2.generateId)(),
|
|
365
392
|
toolName: toolCall.function.name,
|
|
366
|
-
|
|
367
|
-
};
|
|
368
|
-
}
|
|
393
|
+
input: toolCall.function.arguments
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return {
|
|
398
|
+
content,
|
|
369
399
|
finishReason: mapGroqFinishReason(choice.finish_reason),
|
|
370
400
|
usage: {
|
|
371
401
|
inputTokens: (_c = (_b = response.usage) == null ? void 0 : _b.prompt_tokens) != null ? _c : void 0,
|
|
372
|
-
outputTokens: (_e = (_d = response.usage) == null ? void 0 : _d.completion_tokens) != null ? _e : void 0
|
|
402
|
+
outputTokens: (_e = (_d = response.usage) == null ? void 0 : _d.completion_tokens) != null ? _e : void 0,
|
|
403
|
+
totalTokens: (_g = (_f = response.usage) == null ? void 0 : _f.total_tokens) != null ? _g : void 0
|
|
373
404
|
},
|
|
374
405
|
response: {
|
|
375
406
|
...getResponseMetadata(response),
|
|
@@ -381,7 +412,7 @@ var GroqChatLanguageModel = class {
|
|
|
381
412
|
};
|
|
382
413
|
}
|
|
383
414
|
async doStream(options) {
|
|
384
|
-
const { args, warnings } = this.getArgs({ ...options, stream: true });
|
|
415
|
+
const { args, warnings } = await this.getArgs({ ...options, stream: true });
|
|
385
416
|
const body = JSON.stringify({ ...args, stream: true });
|
|
386
417
|
const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
|
|
387
418
|
url: this.config.url({
|
|
@@ -402,15 +433,24 @@ var GroqChatLanguageModel = class {
|
|
|
402
433
|
let finishReason = "unknown";
|
|
403
434
|
const usage = {
|
|
404
435
|
inputTokens: void 0,
|
|
405
|
-
outputTokens: void 0
|
|
436
|
+
outputTokens: void 0,
|
|
437
|
+
totalTokens: void 0
|
|
406
438
|
};
|
|
407
439
|
let isFirstChunk = true;
|
|
440
|
+
let isActiveText = false;
|
|
441
|
+
let isActiveReasoning = false;
|
|
408
442
|
let providerMetadata;
|
|
409
443
|
return {
|
|
410
444
|
stream: response.pipeThrough(
|
|
411
445
|
new TransformStream({
|
|
446
|
+
start(controller) {
|
|
447
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
448
|
+
},
|
|
412
449
|
transform(chunk, controller) {
|
|
413
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
|
|
450
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
|
|
451
|
+
if (options.includeRawChunks) {
|
|
452
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
453
|
+
}
|
|
414
454
|
if (!chunk.success) {
|
|
415
455
|
finishReason = "error";
|
|
416
456
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
@@ -432,6 +472,7 @@ var GroqChatLanguageModel = class {
|
|
|
432
472
|
if (((_a = value.x_groq) == null ? void 0 : _a.usage) != null) {
|
|
433
473
|
usage.inputTokens = (_b = value.x_groq.usage.prompt_tokens) != null ? _b : void 0;
|
|
434
474
|
usage.outputTokens = (_c = value.x_groq.usage.completion_tokens) != null ? _c : void 0;
|
|
475
|
+
usage.totalTokens = (_d = value.x_groq.usage.total_tokens) != null ? _d : void 0;
|
|
435
476
|
}
|
|
436
477
|
const choice = value.choices[0];
|
|
437
478
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
@@ -442,16 +483,28 @@ var GroqChatLanguageModel = class {
|
|
|
442
483
|
}
|
|
443
484
|
const delta = choice.delta;
|
|
444
485
|
if (delta.reasoning != null && delta.reasoning.length > 0) {
|
|
486
|
+
if (!isActiveReasoning) {
|
|
487
|
+
controller.enqueue({
|
|
488
|
+
type: "reasoning-start",
|
|
489
|
+
id: "reasoning-0"
|
|
490
|
+
});
|
|
491
|
+
isActiveReasoning = true;
|
|
492
|
+
}
|
|
445
493
|
controller.enqueue({
|
|
446
|
-
type: "reasoning",
|
|
447
|
-
|
|
448
|
-
|
|
494
|
+
type: "reasoning-delta",
|
|
495
|
+
id: "reasoning-0",
|
|
496
|
+
delta: delta.reasoning
|
|
449
497
|
});
|
|
450
498
|
}
|
|
451
499
|
if (delta.content != null && delta.content.length > 0) {
|
|
500
|
+
if (!isActiveText) {
|
|
501
|
+
controller.enqueue({ type: "text-start", id: "txt-0" });
|
|
502
|
+
isActiveText = true;
|
|
503
|
+
}
|
|
452
504
|
controller.enqueue({
|
|
453
|
-
type: "text",
|
|
454
|
-
|
|
505
|
+
type: "text-delta",
|
|
506
|
+
id: "txt-0",
|
|
507
|
+
delta: delta.content
|
|
455
508
|
});
|
|
456
509
|
}
|
|
457
510
|
if (delta.tool_calls != null) {
|
|
@@ -470,39 +523,45 @@ var GroqChatLanguageModel = class {
|
|
|
470
523
|
message: `Expected 'id' to be a string.`
|
|
471
524
|
});
|
|
472
525
|
}
|
|
473
|
-
if (((
|
|
526
|
+
if (((_e = toolCallDelta.function) == null ? void 0 : _e.name) == null) {
|
|
474
527
|
throw new import_provider3.InvalidResponseDataError({
|
|
475
528
|
data: toolCallDelta,
|
|
476
529
|
message: `Expected 'function.name' to be a string.`
|
|
477
530
|
});
|
|
478
531
|
}
|
|
532
|
+
controller.enqueue({
|
|
533
|
+
type: "tool-input-start",
|
|
534
|
+
id: toolCallDelta.id,
|
|
535
|
+
toolName: toolCallDelta.function.name
|
|
536
|
+
});
|
|
479
537
|
toolCalls[index] = {
|
|
480
538
|
id: toolCallDelta.id,
|
|
481
539
|
type: "function",
|
|
482
540
|
function: {
|
|
483
541
|
name: toolCallDelta.function.name,
|
|
484
|
-
arguments: (
|
|
542
|
+
arguments: (_f = toolCallDelta.function.arguments) != null ? _f : ""
|
|
485
543
|
},
|
|
486
544
|
hasFinished: false
|
|
487
545
|
};
|
|
488
546
|
const toolCall2 = toolCalls[index];
|
|
489
|
-
if (((
|
|
547
|
+
if (((_g = toolCall2.function) == null ? void 0 : _g.name) != null && ((_h = toolCall2.function) == null ? void 0 : _h.arguments) != null) {
|
|
490
548
|
if (toolCall2.function.arguments.length > 0) {
|
|
491
549
|
controller.enqueue({
|
|
492
|
-
type: "tool-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
toolName: toolCall2.function.name,
|
|
496
|
-
argsTextDelta: toolCall2.function.arguments
|
|
550
|
+
type: "tool-input-delta",
|
|
551
|
+
id: toolCall2.id,
|
|
552
|
+
delta: toolCall2.function.arguments
|
|
497
553
|
});
|
|
498
554
|
}
|
|
499
555
|
if ((0, import_provider_utils2.isParsableJson)(toolCall2.function.arguments)) {
|
|
556
|
+
controller.enqueue({
|
|
557
|
+
type: "tool-input-end",
|
|
558
|
+
id: toolCall2.id
|
|
559
|
+
});
|
|
500
560
|
controller.enqueue({
|
|
501
561
|
type: "tool-call",
|
|
502
|
-
|
|
503
|
-
toolCallId: (_h = toolCall2.id) != null ? _h : (0, import_provider_utils2.generateId)(),
|
|
562
|
+
toolCallId: (_i = toolCall2.id) != null ? _i : (0, import_provider_utils2.generateId)(),
|
|
504
563
|
toolName: toolCall2.function.name,
|
|
505
|
-
|
|
564
|
+
input: toolCall2.function.arguments
|
|
506
565
|
});
|
|
507
566
|
toolCall2.hasFinished = true;
|
|
508
567
|
}
|
|
@@ -513,23 +572,24 @@ var GroqChatLanguageModel = class {
|
|
|
513
572
|
if (toolCall.hasFinished) {
|
|
514
573
|
continue;
|
|
515
574
|
}
|
|
516
|
-
if (((
|
|
517
|
-
toolCall.function.arguments += (
|
|
575
|
+
if (((_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null) {
|
|
576
|
+
toolCall.function.arguments += (_l = (_k = toolCallDelta.function) == null ? void 0 : _k.arguments) != null ? _l : "";
|
|
518
577
|
}
|
|
519
578
|
controller.enqueue({
|
|
520
|
-
type: "tool-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
toolName: toolCall.function.name,
|
|
524
|
-
argsTextDelta: (_l = toolCallDelta.function.arguments) != null ? _l : ""
|
|
579
|
+
type: "tool-input-delta",
|
|
580
|
+
id: toolCall.id,
|
|
581
|
+
delta: (_m = toolCallDelta.function.arguments) != null ? _m : ""
|
|
525
582
|
});
|
|
526
|
-
if (((
|
|
583
|
+
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)) {
|
|
584
|
+
controller.enqueue({
|
|
585
|
+
type: "tool-input-end",
|
|
586
|
+
id: toolCall.id
|
|
587
|
+
});
|
|
527
588
|
controller.enqueue({
|
|
528
589
|
type: "tool-call",
|
|
529
|
-
|
|
530
|
-
toolCallId: (_o = toolCall.id) != null ? _o : (0, import_provider_utils2.generateId)(),
|
|
590
|
+
toolCallId: (_p = toolCall.id) != null ? _p : (0, import_provider_utils2.generateId)(),
|
|
531
591
|
toolName: toolCall.function.name,
|
|
532
|
-
|
|
592
|
+
input: toolCall.function.arguments
|
|
533
593
|
});
|
|
534
594
|
toolCall.hasFinished = true;
|
|
535
595
|
}
|
|
@@ -537,6 +597,12 @@ var GroqChatLanguageModel = class {
|
|
|
537
597
|
}
|
|
538
598
|
},
|
|
539
599
|
flush(controller) {
|
|
600
|
+
if (isActiveReasoning) {
|
|
601
|
+
controller.enqueue({ type: "reasoning-end", id: "reasoning-0" });
|
|
602
|
+
}
|
|
603
|
+
if (isActiveText) {
|
|
604
|
+
controller.enqueue({ type: "text-end", id: "txt-0" });
|
|
605
|
+
}
|
|
540
606
|
controller.enqueue({
|
|
541
607
|
type: "finish",
|
|
542
608
|
finishReason,
|
|
@@ -546,105 +612,234 @@ var GroqChatLanguageModel = class {
|
|
|
546
612
|
}
|
|
547
613
|
})
|
|
548
614
|
),
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
request: { body }
|
|
615
|
+
request: { body },
|
|
616
|
+
response: { headers: responseHeaders }
|
|
552
617
|
};
|
|
553
618
|
}
|
|
554
619
|
};
|
|
555
|
-
var groqChatResponseSchema =
|
|
556
|
-
id:
|
|
557
|
-
created:
|
|
558
|
-
model:
|
|
559
|
-
choices:
|
|
560
|
-
|
|
561
|
-
message:
|
|
562
|
-
content:
|
|
563
|
-
reasoning:
|
|
564
|
-
tool_calls:
|
|
565
|
-
|
|
566
|
-
id:
|
|
567
|
-
type:
|
|
568
|
-
function:
|
|
569
|
-
name:
|
|
570
|
-
arguments:
|
|
620
|
+
var groqChatResponseSchema = import_v43.z.object({
|
|
621
|
+
id: import_v43.z.string().nullish(),
|
|
622
|
+
created: import_v43.z.number().nullish(),
|
|
623
|
+
model: import_v43.z.string().nullish(),
|
|
624
|
+
choices: import_v43.z.array(
|
|
625
|
+
import_v43.z.object({
|
|
626
|
+
message: import_v43.z.object({
|
|
627
|
+
content: import_v43.z.string().nullish(),
|
|
628
|
+
reasoning: import_v43.z.string().nullish(),
|
|
629
|
+
tool_calls: import_v43.z.array(
|
|
630
|
+
import_v43.z.object({
|
|
631
|
+
id: import_v43.z.string().nullish(),
|
|
632
|
+
type: import_v43.z.literal("function"),
|
|
633
|
+
function: import_v43.z.object({
|
|
634
|
+
name: import_v43.z.string(),
|
|
635
|
+
arguments: import_v43.z.string()
|
|
571
636
|
})
|
|
572
637
|
})
|
|
573
638
|
).nullish()
|
|
574
639
|
}),
|
|
575
|
-
index:
|
|
576
|
-
finish_reason:
|
|
640
|
+
index: import_v43.z.number(),
|
|
641
|
+
finish_reason: import_v43.z.string().nullish()
|
|
577
642
|
})
|
|
578
643
|
),
|
|
579
|
-
usage:
|
|
580
|
-
prompt_tokens:
|
|
581
|
-
completion_tokens:
|
|
644
|
+
usage: import_v43.z.object({
|
|
645
|
+
prompt_tokens: import_v43.z.number().nullish(),
|
|
646
|
+
completion_tokens: import_v43.z.number().nullish(),
|
|
647
|
+
total_tokens: import_v43.z.number().nullish()
|
|
582
648
|
}).nullish()
|
|
583
649
|
});
|
|
584
|
-
var groqChatChunkSchema =
|
|
585
|
-
|
|
586
|
-
id:
|
|
587
|
-
created:
|
|
588
|
-
model:
|
|
589
|
-
choices:
|
|
590
|
-
|
|
591
|
-
delta:
|
|
592
|
-
content:
|
|
593
|
-
reasoning:
|
|
594
|
-
tool_calls:
|
|
595
|
-
|
|
596
|
-
index:
|
|
597
|
-
id:
|
|
598
|
-
type:
|
|
599
|
-
function:
|
|
600
|
-
name:
|
|
601
|
-
arguments:
|
|
650
|
+
var groqChatChunkSchema = import_v43.z.union([
|
|
651
|
+
import_v43.z.object({
|
|
652
|
+
id: import_v43.z.string().nullish(),
|
|
653
|
+
created: import_v43.z.number().nullish(),
|
|
654
|
+
model: import_v43.z.string().nullish(),
|
|
655
|
+
choices: import_v43.z.array(
|
|
656
|
+
import_v43.z.object({
|
|
657
|
+
delta: import_v43.z.object({
|
|
658
|
+
content: import_v43.z.string().nullish(),
|
|
659
|
+
reasoning: import_v43.z.string().nullish(),
|
|
660
|
+
tool_calls: import_v43.z.array(
|
|
661
|
+
import_v43.z.object({
|
|
662
|
+
index: import_v43.z.number(),
|
|
663
|
+
id: import_v43.z.string().nullish(),
|
|
664
|
+
type: import_v43.z.literal("function").optional(),
|
|
665
|
+
function: import_v43.z.object({
|
|
666
|
+
name: import_v43.z.string().nullish(),
|
|
667
|
+
arguments: import_v43.z.string().nullish()
|
|
602
668
|
})
|
|
603
669
|
})
|
|
604
670
|
).nullish()
|
|
605
671
|
}).nullish(),
|
|
606
|
-
finish_reason:
|
|
607
|
-
index:
|
|
672
|
+
finish_reason: import_v43.z.string().nullable().optional(),
|
|
673
|
+
index: import_v43.z.number()
|
|
608
674
|
})
|
|
609
675
|
),
|
|
610
|
-
x_groq:
|
|
611
|
-
usage:
|
|
612
|
-
prompt_tokens:
|
|
613
|
-
completion_tokens:
|
|
676
|
+
x_groq: import_v43.z.object({
|
|
677
|
+
usage: import_v43.z.object({
|
|
678
|
+
prompt_tokens: import_v43.z.number().nullish(),
|
|
679
|
+
completion_tokens: import_v43.z.number().nullish(),
|
|
680
|
+
total_tokens: import_v43.z.number().nullish()
|
|
614
681
|
}).nullish()
|
|
615
682
|
}).nullish()
|
|
616
683
|
}),
|
|
617
684
|
groqErrorDataSchema
|
|
618
685
|
]);
|
|
619
686
|
|
|
687
|
+
// src/groq-transcription-model.ts
|
|
688
|
+
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
689
|
+
var import_v44 = require("zod/v4");
|
|
690
|
+
var groqProviderOptionsSchema = import_v44.z.object({
|
|
691
|
+
language: import_v44.z.string().nullish(),
|
|
692
|
+
prompt: import_v44.z.string().nullish(),
|
|
693
|
+
responseFormat: import_v44.z.string().nullish(),
|
|
694
|
+
temperature: import_v44.z.number().min(0).max(1).nullish(),
|
|
695
|
+
timestampGranularities: import_v44.z.array(import_v44.z.string()).nullish()
|
|
696
|
+
});
|
|
697
|
+
var GroqTranscriptionModel = class {
|
|
698
|
+
constructor(modelId, config) {
|
|
699
|
+
this.modelId = modelId;
|
|
700
|
+
this.config = config;
|
|
701
|
+
this.specificationVersion = "v2";
|
|
702
|
+
}
|
|
703
|
+
get provider() {
|
|
704
|
+
return this.config.provider;
|
|
705
|
+
}
|
|
706
|
+
async getArgs({
|
|
707
|
+
audio,
|
|
708
|
+
mediaType,
|
|
709
|
+
providerOptions
|
|
710
|
+
}) {
|
|
711
|
+
var _a, _b, _c, _d, _e;
|
|
712
|
+
const warnings = [];
|
|
713
|
+
const groqOptions = await (0, import_provider_utils3.parseProviderOptions)({
|
|
714
|
+
provider: "groq",
|
|
715
|
+
providerOptions,
|
|
716
|
+
schema: groqProviderOptionsSchema
|
|
717
|
+
});
|
|
718
|
+
const formData = new FormData();
|
|
719
|
+
const blob = audio instanceof Uint8Array ? new Blob([audio]) : new Blob([(0, import_provider_utils3.convertBase64ToUint8Array)(audio)]);
|
|
720
|
+
formData.append("model", this.modelId);
|
|
721
|
+
formData.append("file", new File([blob], "audio", { type: mediaType }));
|
|
722
|
+
if (groqOptions) {
|
|
723
|
+
const transcriptionModelOptions = {
|
|
724
|
+
language: (_a = groqOptions.language) != null ? _a : void 0,
|
|
725
|
+
prompt: (_b = groqOptions.prompt) != null ? _b : void 0,
|
|
726
|
+
response_format: (_c = groqOptions.responseFormat) != null ? _c : void 0,
|
|
727
|
+
temperature: (_d = groqOptions.temperature) != null ? _d : void 0,
|
|
728
|
+
timestamp_granularities: (_e = groqOptions.timestampGranularities) != null ? _e : void 0
|
|
729
|
+
};
|
|
730
|
+
for (const key in transcriptionModelOptions) {
|
|
731
|
+
const value = transcriptionModelOptions[key];
|
|
732
|
+
if (value !== void 0) {
|
|
733
|
+
formData.append(key, String(value));
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
return {
|
|
738
|
+
formData,
|
|
739
|
+
warnings
|
|
740
|
+
};
|
|
741
|
+
}
|
|
742
|
+
async doGenerate(options) {
|
|
743
|
+
var _a, _b, _c, _d, _e;
|
|
744
|
+
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
745
|
+
const { formData, warnings } = await this.getArgs(options);
|
|
746
|
+
const {
|
|
747
|
+
value: response,
|
|
748
|
+
responseHeaders,
|
|
749
|
+
rawValue: rawResponse
|
|
750
|
+
} = await (0, import_provider_utils3.postFormDataToApi)({
|
|
751
|
+
url: this.config.url({
|
|
752
|
+
path: "/audio/transcriptions",
|
|
753
|
+
modelId: this.modelId
|
|
754
|
+
}),
|
|
755
|
+
headers: (0, import_provider_utils3.combineHeaders)(this.config.headers(), options.headers),
|
|
756
|
+
formData,
|
|
757
|
+
failedResponseHandler: groqFailedResponseHandler,
|
|
758
|
+
successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
|
|
759
|
+
groqTranscriptionResponseSchema
|
|
760
|
+
),
|
|
761
|
+
abortSignal: options.abortSignal,
|
|
762
|
+
fetch: this.config.fetch
|
|
763
|
+
});
|
|
764
|
+
return {
|
|
765
|
+
text: response.text,
|
|
766
|
+
segments: (_e = (_d = response.segments) == null ? void 0 : _d.map((segment) => ({
|
|
767
|
+
text: segment.text,
|
|
768
|
+
startSecond: segment.start,
|
|
769
|
+
endSecond: segment.end
|
|
770
|
+
}))) != null ? _e : [],
|
|
771
|
+
language: response.language,
|
|
772
|
+
durationInSeconds: response.duration,
|
|
773
|
+
warnings,
|
|
774
|
+
response: {
|
|
775
|
+
timestamp: currentDate,
|
|
776
|
+
modelId: this.modelId,
|
|
777
|
+
headers: responseHeaders,
|
|
778
|
+
body: rawResponse
|
|
779
|
+
}
|
|
780
|
+
};
|
|
781
|
+
}
|
|
782
|
+
};
|
|
783
|
+
var groqTranscriptionResponseSchema = import_v44.z.object({
|
|
784
|
+
task: import_v44.z.string(),
|
|
785
|
+
language: import_v44.z.string(),
|
|
786
|
+
duration: import_v44.z.number(),
|
|
787
|
+
text: import_v44.z.string(),
|
|
788
|
+
segments: import_v44.z.array(
|
|
789
|
+
import_v44.z.object({
|
|
790
|
+
id: import_v44.z.number(),
|
|
791
|
+
seek: import_v44.z.number(),
|
|
792
|
+
start: import_v44.z.number(),
|
|
793
|
+
end: import_v44.z.number(),
|
|
794
|
+
text: import_v44.z.string(),
|
|
795
|
+
tokens: import_v44.z.array(import_v44.z.number()),
|
|
796
|
+
temperature: import_v44.z.number(),
|
|
797
|
+
avg_logprob: import_v44.z.number(),
|
|
798
|
+
compression_ratio: import_v44.z.number(),
|
|
799
|
+
no_speech_prob: import_v44.z.number()
|
|
800
|
+
})
|
|
801
|
+
),
|
|
802
|
+
x_groq: import_v44.z.object({
|
|
803
|
+
id: import_v44.z.string()
|
|
804
|
+
})
|
|
805
|
+
});
|
|
806
|
+
|
|
620
807
|
// src/groq-provider.ts
|
|
621
808
|
function createGroq(options = {}) {
|
|
622
809
|
var _a;
|
|
623
|
-
const baseURL = (_a = (0,
|
|
810
|
+
const baseURL = (_a = (0, import_provider_utils4.withoutTrailingSlash)(options.baseURL)) != null ? _a : "https://api.groq.com/openai/v1";
|
|
624
811
|
const getHeaders = () => ({
|
|
625
|
-
Authorization: `Bearer ${(0,
|
|
812
|
+
Authorization: `Bearer ${(0, import_provider_utils4.loadApiKey)({
|
|
626
813
|
apiKey: options.apiKey,
|
|
627
814
|
environmentVariableName: "GROQ_API_KEY",
|
|
628
815
|
description: "Groq"
|
|
629
816
|
})}`,
|
|
630
817
|
...options.headers
|
|
631
818
|
});
|
|
632
|
-
const createChatModel = (modelId
|
|
819
|
+
const createChatModel = (modelId) => new GroqChatLanguageModel(modelId, {
|
|
633
820
|
provider: "groq.chat",
|
|
634
821
|
url: ({ path }) => `${baseURL}${path}`,
|
|
635
822
|
headers: getHeaders,
|
|
636
823
|
fetch: options.fetch
|
|
637
824
|
});
|
|
638
|
-
const createLanguageModel = (modelId
|
|
825
|
+
const createLanguageModel = (modelId) => {
|
|
639
826
|
if (new.target) {
|
|
640
827
|
throw new Error(
|
|
641
828
|
"The Groq model function cannot be called with the new keyword."
|
|
642
829
|
);
|
|
643
830
|
}
|
|
644
|
-
return createChatModel(modelId
|
|
831
|
+
return createChatModel(modelId);
|
|
832
|
+
};
|
|
833
|
+
const createTranscriptionModel = (modelId) => {
|
|
834
|
+
return new GroqTranscriptionModel(modelId, {
|
|
835
|
+
provider: "groq.transcription",
|
|
836
|
+
url: ({ path }) => `${baseURL}${path}`,
|
|
837
|
+
headers: getHeaders,
|
|
838
|
+
fetch: options.fetch
|
|
839
|
+
});
|
|
645
840
|
};
|
|
646
|
-
const provider = function(modelId
|
|
647
|
-
return createLanguageModel(modelId
|
|
841
|
+
const provider = function(modelId) {
|
|
842
|
+
return createLanguageModel(modelId);
|
|
648
843
|
};
|
|
649
844
|
provider.languageModel = createLanguageModel;
|
|
650
845
|
provider.chat = createChatModel;
|
|
@@ -654,6 +849,7 @@ function createGroq(options = {}) {
|
|
|
654
849
|
provider.imageModel = (modelId) => {
|
|
655
850
|
throw new import_provider4.NoSuchModelError({ modelId, modelType: "imageModel" });
|
|
656
851
|
};
|
|
852
|
+
provider.transcription = createTranscriptionModel;
|
|
657
853
|
return provider;
|
|
658
854
|
}
|
|
659
855
|
var groq = createGroq();
|