ai 6.0.237 → 6.0.239
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 +19 -0
- package/dist/index.d.mts +16 -6
- package/dist/index.d.ts +16 -6
- package/dist/index.js +144 -117
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +144 -117
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +1 -1
- package/dist/internal/index.mjs +1 -1
- package/docs/03-agents/02-building-agents.mdx +4 -0
- package/docs/03-agents/04-loop-control.mdx +40 -0
- package/docs/03-ai-sdk-core/10-generating-structured-data.mdx +24 -9
- package/docs/03-ai-sdk-core/60-telemetry.mdx +4 -4
- package/docs/07-reference/01-ai-sdk-core/01-generate-text.mdx +64 -6
- package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +57 -1
- package/docs/07-reference/01-ai-sdk-core/16-tool-loop-agent.mdx +1 -1
- package/docs/07-reference/01-ai-sdk-core/23-create-mcp-client.mdx +7 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-output-generated-error.mdx +5 -0
- package/docs/08-migration-guides/26-migration-guide-5-0.mdx +40 -3
- package/package.json +3 -3
- package/src/generate-text/generate-text-result.ts +6 -2
- package/src/generate-text/generate-text.ts +17 -9
- package/src/generate-text/prepare-step-call-settings.ts +30 -0
- package/src/generate-text/prepare-step.ts +20 -3
- package/src/generate-text/step-result.ts +2 -1
- package/src/generate-text/stream-text.ts +26 -10
package/dist/internal/index.js
CHANGED
|
@@ -164,7 +164,7 @@ function detectMediaType({
|
|
|
164
164
|
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
165
165
|
|
|
166
166
|
// src/version.ts
|
|
167
|
-
var VERSION = true ? "6.0.
|
|
167
|
+
var VERSION = true ? "6.0.239" : "0.0.0-test";
|
|
168
168
|
|
|
169
169
|
// src/util/download/download.ts
|
|
170
170
|
var download = async ({
|
package/dist/internal/index.mjs
CHANGED
|
@@ -117,6 +117,10 @@ const agent = new ToolLoopAgent({
|
|
|
117
117
|
|
|
118
118
|
Learn more about [loop control and stop conditions](/docs/agents/loop-control).
|
|
119
119
|
|
|
120
|
+
Model call settings returned from `prepareStep`, such as `temperature`, apply
|
|
121
|
+
only to the current step. Later steps use the agent's top-level setting unless
|
|
122
|
+
they return another override.
|
|
123
|
+
|
|
120
124
|
### Tool Choice
|
|
121
125
|
|
|
122
126
|
Control how the agent uses tools:
|
|
@@ -178,6 +178,46 @@ const result = await agent.generate({
|
|
|
178
178
|
});
|
|
179
179
|
```
|
|
180
180
|
|
|
181
|
+
### Model Call Settings
|
|
182
|
+
|
|
183
|
+
Override provider-agnostic model call settings for an individual step. This can
|
|
184
|
+
be useful when tool-calling steps need more deterministic sampling than the
|
|
185
|
+
final response:
|
|
186
|
+
|
|
187
|
+
```ts
|
|
188
|
+
import { ToolLoopAgent } from 'ai';
|
|
189
|
+
__PROVIDER_IMPORT__;
|
|
190
|
+
|
|
191
|
+
const agent = new ToolLoopAgent({
|
|
192
|
+
model: __MODEL__,
|
|
193
|
+
temperature: 0.7,
|
|
194
|
+
tools: {
|
|
195
|
+
// your tools
|
|
196
|
+
},
|
|
197
|
+
prepareStep: async ({ stepNumber }) => {
|
|
198
|
+
if (stepNumber === 0) {
|
|
199
|
+
return {
|
|
200
|
+
temperature: 0,
|
|
201
|
+
maxOutputTokens: 300,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return {};
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
const result = await agent.generate({
|
|
210
|
+
prompt: '...',
|
|
211
|
+
});
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
`prepareStep` can override `maxOutputTokens`, `temperature`, `topP`, `topK`,
|
|
215
|
+
`presencePenalty`, `frequencyPenalty`, `stopSequences`, and `seed`. These
|
|
216
|
+
overrides apply only to the current step. When a setting is omitted or
|
|
217
|
+
`undefined`, the top-level value is used for that step. Defined falsy values
|
|
218
|
+
such as `temperature: 0`, `seed: 0`, and an empty `stopSequences` array are
|
|
219
|
+
preserved.
|
|
220
|
+
|
|
181
221
|
### Context Management
|
|
182
222
|
|
|
183
223
|
Manage growing conversation history in long-running loops:
|
|
@@ -415,16 +415,22 @@ console.log(result.reasoningText);
|
|
|
415
415
|
|
|
416
416
|
## Error Handling
|
|
417
417
|
|
|
418
|
-
|
|
418
|
+
`generateText` can report structured output failures in two ways:
|
|
419
419
|
|
|
420
|
-
|
|
421
|
-
|
|
420
|
+
- If the model response cannot be parsed or validated against the schema,
|
|
421
|
+
`generateText` rejects with an
|
|
422
|
+
[`AI_NoObjectGeneratedError`](/docs/reference/ai-sdk-errors/ai-no-object-generated-error).
|
|
423
|
+
- If `generateText` returns a result without an output, accessing `result.output`
|
|
424
|
+
throws an
|
|
425
|
+
[`AI_NoOutputGeneratedError`](/docs/reference/ai-sdk-errors/ai-no-output-generated-error).
|
|
426
|
+
This can happen when the final step does not finish with a `stop` reason, for
|
|
427
|
+
example when it finishes with `tool-calls`.
|
|
422
428
|
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
- The model generated a response that could not be validated against the schema.
|
|
429
|
+
The `output` property is a getter, so destructuring it also triggers this
|
|
430
|
+
access.
|
|
426
431
|
|
|
427
|
-
|
|
432
|
+
`NoObjectGeneratedError` preserves the following information to help you log
|
|
433
|
+
the issue:
|
|
428
434
|
|
|
429
435
|
- `text`: The text that was generated by the model. This can be the raw text or the tool call text, depending on the object generation mode.
|
|
430
436
|
- `response`: Metadata about the language model response, including response id, timestamp, and model.
|
|
@@ -432,14 +438,21 @@ The error preserves the following information to help you log the issue:
|
|
|
432
438
|
- `cause`: The cause of the error (e.g. a JSON parsing error). You can use this for more detailed error handling.
|
|
433
439
|
|
|
434
440
|
```ts
|
|
435
|
-
import {
|
|
441
|
+
import {
|
|
442
|
+
generateText,
|
|
443
|
+
NoObjectGeneratedError,
|
|
444
|
+
NoOutputGeneratedError,
|
|
445
|
+
Output,
|
|
446
|
+
} from 'ai';
|
|
436
447
|
|
|
437
448
|
try {
|
|
438
|
-
await generateText({
|
|
449
|
+
const result = await generateText({
|
|
439
450
|
model,
|
|
440
451
|
output: Output.object({ schema }),
|
|
441
452
|
prompt,
|
|
442
453
|
});
|
|
454
|
+
|
|
455
|
+
console.log(result.output);
|
|
443
456
|
} catch (error) {
|
|
444
457
|
if (NoObjectGeneratedError.isInstance(error)) {
|
|
445
458
|
console.log('NoObjectGeneratedError');
|
|
@@ -447,6 +460,8 @@ try {
|
|
|
447
460
|
console.log('Text:', error.text);
|
|
448
461
|
console.log('Response:', error.response);
|
|
449
462
|
console.log('Usage:', error.usage);
|
|
463
|
+
} else if (NoOutputGeneratedError.isInstance(error)) {
|
|
464
|
+
console.log('NoOutputGeneratedError');
|
|
450
465
|
}
|
|
451
466
|
}
|
|
452
467
|
```
|
|
@@ -197,7 +197,7 @@ The event types for each method are the same as the corresponding [event callbac
|
|
|
197
197
|
|
|
198
198
|
`generateText` records 3 types of spans:
|
|
199
199
|
|
|
200
|
-
- `ai.generateText` (span): the full length of the generateText call. It
|
|
200
|
+
- `ai.generateText` (span): the full length of the generateText call. It is the direct parent of 1 or more `ai.generateText.doGenerate` spans and any `ai.toolCall` spans recorded for the call.
|
|
201
201
|
It contains the [basic LLM span information](#basic-llm-span-information) and the following attributes:
|
|
202
202
|
|
|
203
203
|
- `operation.name`: `ai.generateText` and the functionId that was set through `telemetry.functionId`
|
|
@@ -208,7 +208,7 @@ The event types for each method are the same as the corresponding [event callbac
|
|
|
208
208
|
- `ai.response.finishReason`: the reason why the generation finished
|
|
209
209
|
- `ai.settings.maxOutputTokens`: the maximum number of output tokens that were set
|
|
210
210
|
|
|
211
|
-
- `ai.generateText.doGenerate` (span): a provider doGenerate call.
|
|
211
|
+
- `ai.generateText.doGenerate` (span): a provider doGenerate call.
|
|
212
212
|
It contains the [call LLM span information](#call-llm-span-information) and the following attributes:
|
|
213
213
|
|
|
214
214
|
- `operation.name`: `ai.generateText.doGenerate` and the functionId that was set through `telemetry.functionId`
|
|
@@ -229,7 +229,7 @@ The event types for each method are the same as the corresponding [event callbac
|
|
|
229
229
|
|
|
230
230
|
`streamText` records 3 types of spans and 2 types of events:
|
|
231
231
|
|
|
232
|
-
- `ai.streamText` (span): the full length of the streamText call. It
|
|
232
|
+
- `ai.streamText` (span): the full length of the streamText call. It is the direct parent of 1 or more `ai.streamText.doStream` spans and any `ai.toolCall` spans recorded for the call.
|
|
233
233
|
It contains the [basic LLM span information](#basic-llm-span-information) and the following attributes:
|
|
234
234
|
|
|
235
235
|
- `operation.name`: `ai.streamText` and the functionId that was set through `telemetry.functionId`
|
|
@@ -241,7 +241,7 @@ The event types for each method are the same as the corresponding [event callbac
|
|
|
241
241
|
- `ai.settings.maxOutputTokens`: the maximum number of output tokens that were set
|
|
242
242
|
|
|
243
243
|
- `ai.streamText.doStream` (span): a provider doStream call.
|
|
244
|
-
This span
|
|
244
|
+
This span can contain `ai.stream.firstChunk` and `ai.stream.finish` events.
|
|
245
245
|
It contains the [call LLM span information](#call-llm-span-information) and the following attributes:
|
|
246
246
|
|
|
247
247
|
- `operation.name`: `ai.streamText.doStream` and the functionId that was set through `telemetry.functionId`
|
|
@@ -530,7 +530,7 @@ To see `generateText` in action, check out [these examples](#examples).
|
|
|
530
530
|
type: '(options: PrepareStepOptions) => PrepareStepResult<TOOLS> | Promise<PrepareStepResult<TOOLS>>',
|
|
531
531
|
isOptional: true,
|
|
532
532
|
description:
|
|
533
|
-
'Optional function that you can use to provide different settings for a step. You can modify the model, tool choices, active tools, system prompt, and input messages for each step.',
|
|
533
|
+
'Optional function that you can use to provide different settings for a step. You can modify the model, model call settings, tool choices, active tools, system prompt, and input messages for each step.',
|
|
534
534
|
properties: [
|
|
535
535
|
{
|
|
536
536
|
type: 'PrepareStepFunction<TOOLS>',
|
|
@@ -590,6 +590,62 @@ To see `generateText` in action, check out [these examples](#examples).
|
|
|
590
590
|
description:
|
|
591
591
|
'Optionally override which LanguageModel instance is used for this step.',
|
|
592
592
|
},
|
|
593
|
+
{
|
|
594
|
+
name: 'maxOutputTokens',
|
|
595
|
+
type: 'number',
|
|
596
|
+
isOptional: true,
|
|
597
|
+
description:
|
|
598
|
+
'Maximum number of tokens to generate for this step. Uses the top-level value when omitted or undefined.',
|
|
599
|
+
},
|
|
600
|
+
{
|
|
601
|
+
name: 'temperature',
|
|
602
|
+
type: 'number',
|
|
603
|
+
isOptional: true,
|
|
604
|
+
description:
|
|
605
|
+
'Temperature for this step. Uses the top-level value when omitted or undefined.',
|
|
606
|
+
},
|
|
607
|
+
{
|
|
608
|
+
name: 'topP',
|
|
609
|
+
type: 'number',
|
|
610
|
+
isOptional: true,
|
|
611
|
+
description:
|
|
612
|
+
'Nucleus sampling value for this step. Uses the top-level value when omitted or undefined.',
|
|
613
|
+
},
|
|
614
|
+
{
|
|
615
|
+
name: 'topK',
|
|
616
|
+
type: 'number',
|
|
617
|
+
isOptional: true,
|
|
618
|
+
description:
|
|
619
|
+
'Top-K sampling value for this step. Uses the top-level value when omitted or undefined.',
|
|
620
|
+
},
|
|
621
|
+
{
|
|
622
|
+
name: 'presencePenalty',
|
|
623
|
+
type: 'number',
|
|
624
|
+
isOptional: true,
|
|
625
|
+
description:
|
|
626
|
+
'Presence penalty for this step. Uses the top-level value when omitted or undefined.',
|
|
627
|
+
},
|
|
628
|
+
{
|
|
629
|
+
name: 'frequencyPenalty',
|
|
630
|
+
type: 'number',
|
|
631
|
+
isOptional: true,
|
|
632
|
+
description:
|
|
633
|
+
'Frequency penalty for this step. Uses the top-level value when omitted or undefined.',
|
|
634
|
+
},
|
|
635
|
+
{
|
|
636
|
+
name: 'stopSequences',
|
|
637
|
+
type: 'string[]',
|
|
638
|
+
isOptional: true,
|
|
639
|
+
description:
|
|
640
|
+
'Stop sequences for this step. Uses the top-level value when omitted or undefined.',
|
|
641
|
+
},
|
|
642
|
+
{
|
|
643
|
+
name: 'seed',
|
|
644
|
+
type: 'number',
|
|
645
|
+
isOptional: true,
|
|
646
|
+
description:
|
|
647
|
+
'Random sampling seed for this step. Uses the top-level value when omitted or undefined.',
|
|
648
|
+
},
|
|
593
649
|
{
|
|
594
650
|
name: 'toolChoice',
|
|
595
651
|
type: 'ToolChoice<TOOLS>',
|
|
@@ -1977,7 +2033,8 @@ To see `generateText` in action, check out [these examples](#examples).
|
|
|
1977
2033
|
{
|
|
1978
2034
|
name: 'text',
|
|
1979
2035
|
type: 'string',
|
|
1980
|
-
description:
|
|
2036
|
+
description:
|
|
2037
|
+
'The concatenation of all text parts generated in the final step. It is an empty string if the final step contains no text parts. Inspect `content` for a text part to distinguish that case.',
|
|
1981
2038
|
},
|
|
1982
2039
|
{
|
|
1983
2040
|
name: 'reasoning',
|
|
@@ -2339,9 +2396,9 @@ To see `generateText` in action, check out [these examples](#examples).
|
|
|
2339
2396
|
},
|
|
2340
2397
|
{
|
|
2341
2398
|
name: 'output',
|
|
2342
|
-
type: '
|
|
2343
|
-
|
|
2344
|
-
|
|
2399
|
+
type: 'InferCompleteOutput<OUTPUT>',
|
|
2400
|
+
description:
|
|
2401
|
+
'The generated output according to the `output` specification. Accessing this property throws `NoOutputGeneratedError` when no output is available, for example when the final step does not finish with a `stop` reason.',
|
|
2345
2402
|
},
|
|
2346
2403
|
{
|
|
2347
2404
|
name: 'steps',
|
|
@@ -2388,7 +2445,8 @@ To see `generateText` in action, check out [these examples](#examples).
|
|
|
2388
2445
|
{
|
|
2389
2446
|
name: 'text',
|
|
2390
2447
|
type: 'string',
|
|
2391
|
-
description:
|
|
2448
|
+
description:
|
|
2449
|
+
'The concatenation of all text parts generated in this step. It is an empty string if the step contains no text parts.',
|
|
2392
2450
|
},
|
|
2393
2451
|
{
|
|
2394
2452
|
name: 'reasoning',
|
|
@@ -574,7 +574,7 @@ To see `streamText` in action, check out [these examples](#examples).
|
|
|
574
574
|
type: '(options: PrepareStepOptions) => PrepareStepResult<TOOLS> | Promise<PrepareStepResult<TOOLS>>',
|
|
575
575
|
isOptional: true,
|
|
576
576
|
description:
|
|
577
|
-
'Optional function that you can use to provide different settings for a step. You can modify the model, tool choices, active tools, system prompt, and input messages for each step.',
|
|
577
|
+
'Optional function that you can use to provide different settings for a step. You can modify the model, model call settings, tool choices, active tools, system prompt, and input messages for each step.',
|
|
578
578
|
properties: [
|
|
579
579
|
{
|
|
580
580
|
type: 'PrepareStepFunction<TOOLS>',
|
|
@@ -634,6 +634,62 @@ To see `streamText` in action, check out [these examples](#examples).
|
|
|
634
634
|
description:
|
|
635
635
|
'Optionally override which LanguageModel instance is used for this step.',
|
|
636
636
|
},
|
|
637
|
+
{
|
|
638
|
+
name: 'maxOutputTokens',
|
|
639
|
+
type: 'number',
|
|
640
|
+
isOptional: true,
|
|
641
|
+
description:
|
|
642
|
+
'Maximum number of tokens to generate for this step. Uses the top-level value when omitted or undefined.',
|
|
643
|
+
},
|
|
644
|
+
{
|
|
645
|
+
name: 'temperature',
|
|
646
|
+
type: 'number',
|
|
647
|
+
isOptional: true,
|
|
648
|
+
description:
|
|
649
|
+
'Temperature for this step. Uses the top-level value when omitted or undefined.',
|
|
650
|
+
},
|
|
651
|
+
{
|
|
652
|
+
name: 'topP',
|
|
653
|
+
type: 'number',
|
|
654
|
+
isOptional: true,
|
|
655
|
+
description:
|
|
656
|
+
'Nucleus sampling value for this step. Uses the top-level value when omitted or undefined.',
|
|
657
|
+
},
|
|
658
|
+
{
|
|
659
|
+
name: 'topK',
|
|
660
|
+
type: 'number',
|
|
661
|
+
isOptional: true,
|
|
662
|
+
description:
|
|
663
|
+
'Top-K sampling value for this step. Uses the top-level value when omitted or undefined.',
|
|
664
|
+
},
|
|
665
|
+
{
|
|
666
|
+
name: 'presencePenalty',
|
|
667
|
+
type: 'number',
|
|
668
|
+
isOptional: true,
|
|
669
|
+
description:
|
|
670
|
+
'Presence penalty for this step. Uses the top-level value when omitted or undefined.',
|
|
671
|
+
},
|
|
672
|
+
{
|
|
673
|
+
name: 'frequencyPenalty',
|
|
674
|
+
type: 'number',
|
|
675
|
+
isOptional: true,
|
|
676
|
+
description:
|
|
677
|
+
'Frequency penalty for this step. Uses the top-level value when omitted or undefined.',
|
|
678
|
+
},
|
|
679
|
+
{
|
|
680
|
+
name: 'stopSequences',
|
|
681
|
+
type: 'string[]',
|
|
682
|
+
isOptional: true,
|
|
683
|
+
description:
|
|
684
|
+
'Stop sequences for this step. Uses the top-level value when omitted or undefined.',
|
|
685
|
+
},
|
|
686
|
+
{
|
|
687
|
+
name: 'seed',
|
|
688
|
+
type: 'number',
|
|
689
|
+
isOptional: true,
|
|
690
|
+
description:
|
|
691
|
+
'Random sampling seed for this step. Uses the top-level value when omitted or undefined.',
|
|
692
|
+
},
|
|
637
693
|
{
|
|
638
694
|
name: 'toolChoice',
|
|
639
695
|
type: 'ToolChoice<TOOLS>',
|
|
@@ -102,7 +102,7 @@ To see `ToolLoopAgent` in action, check out [these examples](#examples).
|
|
|
102
102
|
type: 'PrepareStepFunction',
|
|
103
103
|
isOptional: true,
|
|
104
104
|
description:
|
|
105
|
-
'Optional function to mutate step settings or inject state for each agent step.',
|
|
105
|
+
'Optional function to mutate step settings or inject state for each agent step, including per-step model call settings such as temperature, maxOutputTokens, sampling controls, penalties, stop sequences, and seed. Model call setting overrides apply only to the current step.',
|
|
106
106
|
},
|
|
107
107
|
{
|
|
108
108
|
name: 'experimental_repairToolCall',
|
|
@@ -120,6 +120,13 @@ It currently does not support accepting notifications from an MCP server, and cu
|
|
|
120
120
|
},
|
|
121
121
|
],
|
|
122
122
|
},
|
|
123
|
+
{
|
|
124
|
+
name: 'initializationOptions',
|
|
125
|
+
type: 'RequestOptions',
|
|
126
|
+
isOptional: true,
|
|
127
|
+
description:
|
|
128
|
+
'Optional signal and timeout settings that bound transport startup and the initialize request. A timeout or abort closes the transport and rejects createMCPClient.',
|
|
129
|
+
},
|
|
123
130
|
{
|
|
124
131
|
name: 'clientName',
|
|
125
132
|
type: 'string',
|
|
@@ -7,6 +7,11 @@ description: Learn how to fix AI_NoOutputGeneratedError
|
|
|
7
7
|
|
|
8
8
|
This error is thrown when no LLM output was generated, e.g. because of errors.
|
|
9
9
|
|
|
10
|
+
For `generateText`, accessing `result.output` throws this error when the result
|
|
11
|
+
does not contain an output. This can happen when the final step does not finish
|
|
12
|
+
with a `stop` reason, for example when it finishes with `tool-calls`. The
|
|
13
|
+
`output` property is a getter, so destructuring it also triggers this access.
|
|
14
|
+
|
|
10
15
|
## Properties
|
|
11
16
|
|
|
12
17
|
- `message`: The error message (optional, defaults to `'No output generated.'`)
|
|
@@ -2069,11 +2069,48 @@ import { useAssistant } from '@ai-sdk/react';
|
|
|
2069
2069
|
```
|
|
2070
2070
|
|
|
2071
2071
|
```tsx filename="AI SDK 5.0"
|
|
2072
|
-
|
|
2073
|
-
|
|
2072
|
+
import { useChat } from '@ai-sdk/react';
|
|
2073
|
+
import { DefaultChatTransport } from 'ai';
|
|
2074
|
+
|
|
2075
|
+
function Chat() {
|
|
2076
|
+
const { messages, sendMessage } = useChat({
|
|
2077
|
+
transport: new DefaultChatTransport({
|
|
2078
|
+
api: '/api/chat',
|
|
2079
|
+
}),
|
|
2080
|
+
});
|
|
2081
|
+
|
|
2082
|
+
// ...
|
|
2083
|
+
}
|
|
2074
2084
|
```
|
|
2075
2085
|
|
|
2076
|
-
|
|
2086
|
+
The `useAssistant` hook was specific to the OpenAI Assistants API. OpenAI has
|
|
2087
|
+
deprecated that API in favor of the Responses API. Configure `useChat` for your
|
|
2088
|
+
route as shown above, then return a UI message stream from the route:
|
|
2089
|
+
|
|
2090
|
+
```tsx filename="app/api/chat/route.ts"
|
|
2091
|
+
import { openai } from '@ai-sdk/openai';
|
|
2092
|
+
import { convertToModelMessages, streamText, type UIMessage } from 'ai';
|
|
2093
|
+
|
|
2094
|
+
export async function POST(req: Request) {
|
|
2095
|
+
const { messages }: { messages: UIMessage[] } = await req.json();
|
|
2096
|
+
|
|
2097
|
+
const result = streamText({
|
|
2098
|
+
model: openai.responses('gpt-4o-mini'),
|
|
2099
|
+
prompt: convertToModelMessages(messages),
|
|
2100
|
+
});
|
|
2101
|
+
|
|
2102
|
+
return result.toUIMessageStreamResponse();
|
|
2103
|
+
}
|
|
2104
|
+
```
|
|
2105
|
+
|
|
2106
|
+
For persistent conversation state and built-in tools, see the
|
|
2107
|
+
[OpenAI Responses API guide](/cookbook/guides/openai-responses).
|
|
2108
|
+
|
|
2109
|
+
If you need to connect `useChat` to another backend, see the
|
|
2110
|
+
[transport documentation](/docs/ai-sdk-ui/transport) and
|
|
2111
|
+
[stream protocol](/docs/ai-sdk-ui/stream-protocol). For migrating existing
|
|
2112
|
+
OpenAI Assistants data and API calls, see OpenAI's
|
|
2113
|
+
[Assistants migration guide](https://platform.openai.com/docs/assistants/migration).
|
|
2077
2114
|
|
|
2078
2115
|
#### Attachments → File Parts
|
|
2079
2116
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.239",
|
|
4
4
|
"description": "AI SDK by Vercel - build apps like ChatGPT, Claude, Gemini, and more with a single interface for any model using the Vercel AI Gateway or go direct to OpenAI, Anthropic, Google, or any other model provider.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -45,9 +45,9 @@
|
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"@opentelemetry/api": "^1.9.0",
|
|
48
|
-
"@ai-sdk/gateway": "3.0.
|
|
48
|
+
"@ai-sdk/gateway": "3.0.161",
|
|
49
49
|
"@ai-sdk/provider": "3.0.14",
|
|
50
|
-
"@ai-sdk/provider-utils": "4.0.
|
|
50
|
+
"@ai-sdk/provider-utils": "4.0.41"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@edge-runtime/vm": "^5.0.0",
|
|
@@ -36,7 +36,9 @@ export interface GenerateTextResult<
|
|
|
36
36
|
readonly content: Array<ContentPart<TOOLS>>;
|
|
37
37
|
|
|
38
38
|
/**
|
|
39
|
-
* The text
|
|
39
|
+
* The concatenation of all text parts generated in the final step.
|
|
40
|
+
* It is an empty string if the final step contains no text parts.
|
|
41
|
+
* Inspect `content` for a text part to distinguish that case.
|
|
40
42
|
*/
|
|
41
43
|
readonly text: string;
|
|
42
44
|
|
|
@@ -165,8 +167,10 @@ export interface GenerateTextResult<
|
|
|
165
167
|
readonly experimental_output: InferCompleteOutput<OUTPUT>;
|
|
166
168
|
|
|
167
169
|
/**
|
|
168
|
-
* The generated
|
|
170
|
+
* The generated output according to the `output` specification.
|
|
169
171
|
*
|
|
172
|
+
* @throws {NoOutputGeneratedError} When no output is available, for example
|
|
173
|
+
* when the final step does not finish with a `stop` reason.
|
|
170
174
|
*/
|
|
171
175
|
readonly output: InferCompleteOutput<OUTPUT>;
|
|
172
176
|
}
|
|
@@ -77,6 +77,7 @@ import { text, type Output } from './output';
|
|
|
77
77
|
import type { InferCompleteOutput } from './output-utils';
|
|
78
78
|
import { parseToolCall } from './parse-tool-call';
|
|
79
79
|
import type { PrepareStepFunction } from './prepare-step';
|
|
80
|
+
import { prepareStepCallSettings } from './prepare-step-call-settings';
|
|
80
81
|
import type { ResponseMessage } from './response-message';
|
|
81
82
|
import { DefaultStepResult, type StepResult } from './step-result';
|
|
82
83
|
import {
|
|
@@ -752,6 +753,11 @@ export async function generateText<
|
|
|
752
753
|
prepareStepResult?.providerOptions,
|
|
753
754
|
);
|
|
754
755
|
|
|
756
|
+
const stepCallSettings = prepareStepCallSettings({
|
|
757
|
+
callSettings,
|
|
758
|
+
stepSettings: prepareStepResult,
|
|
759
|
+
});
|
|
760
|
+
|
|
755
761
|
await notify({
|
|
756
762
|
event: {
|
|
757
763
|
stepNumber: steps.length,
|
|
@@ -816,20 +822,22 @@ export async function generateText<
|
|
|
816
822
|
'gen_ai.system': stepModel.provider,
|
|
817
823
|
'gen_ai.request.model': stepModel.modelId,
|
|
818
824
|
'gen_ai.request.frequency_penalty':
|
|
819
|
-
|
|
820
|
-
'gen_ai.request.max_tokens':
|
|
821
|
-
|
|
822
|
-
'gen_ai.request.
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
'gen_ai.request.
|
|
825
|
+
stepCallSettings.frequencyPenalty,
|
|
826
|
+
'gen_ai.request.max_tokens':
|
|
827
|
+
stepCallSettings.maxOutputTokens,
|
|
828
|
+
'gen_ai.request.presence_penalty':
|
|
829
|
+
stepCallSettings.presencePenalty,
|
|
830
|
+
'gen_ai.request.stop_sequences':
|
|
831
|
+
stepCallSettings.stopSequences,
|
|
832
|
+
'gen_ai.request.temperature': stepCallSettings.temperature,
|
|
833
|
+
'gen_ai.request.top_k': stepCallSettings.topK,
|
|
834
|
+
'gen_ai.request.top_p': stepCallSettings.topP,
|
|
827
835
|
},
|
|
828
836
|
}),
|
|
829
837
|
tracer,
|
|
830
838
|
fn: async span => {
|
|
831
839
|
const result = await stepModel.doGenerate({
|
|
832
|
-
...
|
|
840
|
+
...stepCallSettings,
|
|
833
841
|
tools: stepTools,
|
|
834
842
|
toolChoice: stepToolChoice,
|
|
835
843
|
responseFormat: await output?.responseFormat,
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { prepareCallSettings } from '../prompt/prepare-call-settings';
|
|
2
|
+
import type { PrepareStepCallSettings } from './prepare-step';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Resolves model call settings for a single step.
|
|
6
|
+
*
|
|
7
|
+
* Undefined step settings intentionally fall back to the outer call settings,
|
|
8
|
+
* while defined falsy values such as `temperature: 0` and `seed: 0` are kept.
|
|
9
|
+
*/
|
|
10
|
+
export function prepareStepCallSettings({
|
|
11
|
+
callSettings,
|
|
12
|
+
stepSettings,
|
|
13
|
+
}: {
|
|
14
|
+
callSettings: PrepareStepCallSettings;
|
|
15
|
+
stepSettings: PrepareStepCallSettings | undefined;
|
|
16
|
+
}): PrepareStepCallSettings {
|
|
17
|
+
return prepareCallSettings({
|
|
18
|
+
maxOutputTokens:
|
|
19
|
+
stepSettings?.maxOutputTokens ?? callSettings.maxOutputTokens,
|
|
20
|
+
temperature: stepSettings?.temperature ?? callSettings.temperature,
|
|
21
|
+
topP: stepSettings?.topP ?? callSettings.topP,
|
|
22
|
+
topK: stepSettings?.topK ?? callSettings.topK,
|
|
23
|
+
presencePenalty:
|
|
24
|
+
stepSettings?.presencePenalty ?? callSettings.presencePenalty,
|
|
25
|
+
frequencyPenalty:
|
|
26
|
+
stepSettings?.frequencyPenalty ?? callSettings.frequencyPenalty,
|
|
27
|
+
stopSequences: stepSettings?.stopSequences ?? callSettings.stopSequences,
|
|
28
|
+
seed: stepSettings?.seed ?? callSettings.seed,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
@@ -4,9 +4,22 @@ import type {
|
|
|
4
4
|
SystemModelMessage,
|
|
5
5
|
Tool,
|
|
6
6
|
} from '@ai-sdk/provider-utils';
|
|
7
|
+
import type { CallSettings } from '../prompt/call-settings';
|
|
7
8
|
import type { LanguageModel, ToolChoice } from '../types/language-model';
|
|
8
9
|
import type { StepResult } from './step-result';
|
|
9
10
|
|
|
11
|
+
export type PrepareStepCallSettings = Pick<
|
|
12
|
+
CallSettings,
|
|
13
|
+
| 'maxOutputTokens'
|
|
14
|
+
| 'temperature'
|
|
15
|
+
| 'topP'
|
|
16
|
+
| 'topK'
|
|
17
|
+
| 'presencePenalty'
|
|
18
|
+
| 'frequencyPenalty'
|
|
19
|
+
| 'stopSequences'
|
|
20
|
+
| 'seed'
|
|
21
|
+
>;
|
|
22
|
+
|
|
10
23
|
/**
|
|
11
24
|
* Function that you can use to provide different settings for a step.
|
|
12
25
|
*
|
|
@@ -51,12 +64,16 @@ export type PrepareStepFunction<
|
|
|
51
64
|
|
|
52
65
|
/**
|
|
53
66
|
* The result type returned by a {@link PrepareStepFunction},
|
|
54
|
-
* allowing per-step overrides of model, tools, or
|
|
67
|
+
* allowing per-step overrides of model call settings, model, tools, or
|
|
68
|
+
* messages.
|
|
69
|
+
*
|
|
70
|
+
* Model call setting overrides apply only to the current step. Undefined
|
|
71
|
+
* settings fall back to the outer call settings.
|
|
55
72
|
*/
|
|
56
73
|
export type PrepareStepResult<
|
|
57
74
|
TOOLS extends Record<string, Tool> = Record<string, Tool>,
|
|
58
75
|
> =
|
|
59
|
-
| {
|
|
76
|
+
| ({
|
|
60
77
|
/**
|
|
61
78
|
* Optionally override which LanguageModel instance is used for this step.
|
|
62
79
|
*/
|
|
@@ -99,5 +116,5 @@ export type PrepareStepResult<
|
|
|
99
116
|
* container IDs for Anthropic's code execution.
|
|
100
117
|
*/
|
|
101
118
|
providerOptions?: ProviderOptions;
|
|
102
|
-
}
|
|
119
|
+
} & PrepareStepCallSettings)
|
|
103
120
|
| undefined;
|
|
@@ -65,7 +65,8 @@ export type StepResult<TOOLS extends ToolSet> = {
|
|
|
65
65
|
readonly content: Array<ContentPart<TOOLS>>;
|
|
66
66
|
|
|
67
67
|
/**
|
|
68
|
-
* The generated
|
|
68
|
+
* The concatenation of all text parts generated in this step.
|
|
69
|
+
* It is an empty string if the step contains no text parts.
|
|
69
70
|
*/
|
|
70
71
|
readonly text: string;
|
|
71
72
|
|