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