Skip to content

Commit c03cafe

Browse files
authoredJun 3, 2024··
chore (core, ui): rename maxAutomaticRoundtrips to maxToolRoundtrips (#1799)
1 parent 2047f10 commit c03cafe

File tree

13 files changed

+51
-33
lines changed

13 files changed

+51
-33
lines changed
 

‎.changeset/mean-shrimps-lie.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': patch
3+
---
4+
5+
chore (core, ui): rename maxAutomaticRoundtrips to maxToolRoundtrips

‎content/docs/05-ai-sdk-ui/03-chatbot-with-tool-calling.mdx

+5-5
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ The tool result contains all information about the tool call as well as the resu
3939

4040
<Note>
4141
In order to automatically send another request to the server when all tool
42-
calls are server-side, you need to set `maxAutomaticRoundtrips` to a value
43-
greater than 0 in the `useChat` options. It is disabled by default for
44-
backward compatibility.
42+
calls are server-side, you need to set `maxToolRoundtrips` to a value greater
43+
than 0 in the `useChat` options. It is disabled by default for backward
44+
compatibility.
4545
</Note>
4646

4747
## Example
@@ -115,7 +115,7 @@ There are three things worth mentioning:
115115
It asks the user for confirmation and displays the result once the user confirms or denies the execution.
116116
The result is added to the chat using `addToolResult`.
117117

118-
1. The `maxAutomaticRoundtrips` option is set to 5.
118+
1. The `maxToolRoundtrips` option is set to 5.
119119
This enables several tool use iterations between the client and the server.
120120

121121
```tsx filename='app/page.tsx'
@@ -127,7 +127,7 @@ import { Message, useChat } from 'ai/react';
127127
export default function Chat() {
128128
const { messages, input, handleInputChange, handleSubmit, addToolResult } =
129129
useChat({
130-
maxAutomaticRoundtrips: 5,
130+
maxToolRoundtrips: 5,
131131

132132
// run client-side tools that are automatically executed:
133133
async onToolCall({ toolCall }) {

‎content/docs/07-reference/ai-sdk-core/01-generate-text.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ const result = await generateText({
320320
'An optional abort signal that can be used to cancel the call.',
321321
},
322322
{
323-
name: 'maxAutomaticRoundtrips',
323+
name: 'maxToolRoundtrips',
324324
type: 'number',
325325
isOptional: true,
326326
description:

‎content/docs/07-reference/ai-sdk-ui/01-use-chat.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Allows you to easily create a conversational user interface for your chatbot app
105105
"An optional boolean that determines whether to send extra fields you've added to `messages`. Defaults to `false` and only the `content` and `role` fields will be sent to the API endpoint.",
106106
},
107107
{
108-
name: 'maxAutomaticRoundtrips',
108+
name: 'maxToolRoundtrips',
109109
type: 'number',
110110
description:
111111
'React only. Maximal number of automatic roundtrips for tool calls. An automatic tool call roundtrip is a call to the server with the tool call results when all tool calls in the last assistant message have results. A maximum number is required to prevent infinite loops in the case of misconfigured tools. By default, it is set to 0, which will disable the feature.',

‎content/examples/02-next-pages/tools/render-interface-during-tool-call.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export default function Chat() {
4848
const { messages, input, handleInputChange, handleSubmit, addToolResult } =
4949
useChat({
5050
api: '/api/use-chat-tools-ui',
51-
experimental_maxAutomaticRoundtrips: 5,
51+
maxToolRoundtrips: 5,
5252

5353
// run client-side tools that are automatically executed:
5454
async onToolCall({ toolCall }) {

‎content/examples/03-node/03-tools/05-call-tools-with-automatic-roundtrips.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ description: Learn how to call tools with automatic roundtrips in a Node.js appl
88
Models call tools to gather information or perform actions that are not directly available to the model.
99
When tool results are available, the model can use them to generate another response.
1010

11-
You can enable automatic roundtrips in `generateText` by setting the `maxAutomaticRoundtrips` option to
11+
You can enable automatic roundtrips in `generateText` by setting the `maxToolRoundtrips` option to
1212
a number greater than 0.
1313
This option specifies the maximum number of automatic roundtrips that can be made to prevent infinite loops.
1414

@@ -19,7 +19,7 @@ import { z } from 'zod'
1919

2020
const { text } = await generateText({
2121
model: openai('gpt-4-turbo'),
22-
maxAutomaticRoundtrips: 5
22+
maxToolRoundtrips: 5
2323
tools: {
2424
weather: tool({
2525
description: 'Get the weather in a location',

‎examples/ai-core/src/complex/math-agent/agent.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ async function main() {
4141
}),
4242
},
4343
toolChoice: 'required',
44-
maxAutomaticRoundtrips: 10,
44+
maxToolRoundtrips: 10,
4545
});
4646
}
4747

‎examples/ai-core/src/generate-text/google-vertex-tool-call.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async function main() {
2424
},
2525
}),
2626
},
27-
maxAutomaticRoundtrips: 5,
27+
maxToolRoundtrips: 5,
2828
});
2929

3030
console.log(text);

‎examples/next-openai-pages/pages/use-chat-tools-ui.tsx

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default function Chat() {
55
const { messages, input, handleInputChange, handleSubmit, addToolResult } =
66
useChat({
77
api: '/api/use-chat-tools-ui',
8-
experimental_maxAutomaticRoundtrips: 5,
8+
maxAutomaticRoundtrips: 5,
99

1010
// run client-side tools that are automatically executed:
1111
async onToolCall({ toolCall }) {
@@ -22,9 +22,9 @@ export default function Chat() {
2222
});
2323

2424
return (
25-
<div className="flex flex-col w-full max-w-md py-24 mx-auto stretch gap-4">
25+
<div className="flex flex-col w-full max-w-md gap-4 py-24 mx-auto stretch">
2626
{messages?.map((m: Message) => (
27-
<div key={m.id} className="whitespace-pre-wrap flex flex-col gap-1">
27+
<div key={m.id} className="flex flex-col gap-1 whitespace-pre-wrap">
2828
<strong>{`${m.role}: `}</strong>
2929
{m.content}
3030
{m.toolInvocations?.map((toolInvocation: ToolInvocation) => {
@@ -35,7 +35,7 @@ export default function Chat() {
3535
return (
3636
<div
3737
key={toolCallId}
38-
className="text-gray-500 flex flex-col gap-2"
38+
className="flex flex-col gap-2 text-gray-500"
3939
>
4040
{toolInvocation.args.message}
4141
<div className="flex gap-2">
@@ -79,15 +79,15 @@ export default function Chat() {
7979
key={toolCallId}
8080
className="flex flex-col gap-2 p-4 bg-blue-400 rounded-lg"
8181
>
82-
<div className="flex flex-row justify-between items-center">
83-
<div className="text-4xl text-blue-50 font-medium">
82+
<div className="flex flex-row items-center justify-between">
83+
<div className="text-4xl font-medium text-blue-50">
8484
{toolInvocation.result.value}°
8585
{toolInvocation.result.unit === 'celsius' ? 'C' : 'F'}
8686
</div>
8787

88-
<div className="h-9 w-9 bg-amber-400 rounded-full flex-shrink-0" />
88+
<div className="flex-shrink-0 rounded-full h-9 w-9 bg-amber-400" />
8989
</div>
90-
<div className="flex flex-row gap-2 text-blue-50 justify-between">
90+
<div className="flex flex-row justify-between gap-2 text-blue-50">
9191
{toolInvocation.result.weeklyForecast.map(
9292
(forecast: any) => (
9393
<div
@@ -104,7 +104,7 @@ export default function Chat() {
104104
) : toolInvocation.toolName === 'getLocation' ? (
105105
<div
106106
key={toolCallId}
107-
className="text-gray-500 bg-gray-100 rounded-lg p-4"
107+
className="p-4 text-gray-500 bg-gray-100 rounded-lg"
108108
>
109109
User is in {toolInvocation.result}.
110110
</div>

‎examples/next-openai/app/use-chat-tools/page.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default function Chat() {
77
const { messages, input, handleInputChange, handleSubmit, addToolResult } =
88
useChat({
99
api: '/api/use-chat-tools',
10-
maxAutomaticRoundtrips: 5,
10+
maxToolRoundtrips: 5,
1111

1212
// run client-side tools that are automatically executed:
1313
async onToolCall({ toolCall }) {

‎packages/core/core/generate-text/generate-text.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ describe('result.responseMessages', () => {
315315
},
316316
},
317317
prompt: 'test-input',
318-
maxAutomaticRoundtrips: 2,
318+
maxToolRoundtrips: 2,
319319
});
320320

321321
assert.deepStrictEqual(result.responseMessages, [
@@ -350,7 +350,7 @@ describe('result.responseMessages', () => {
350350
});
351351
});
352352

353-
describe('maxAutomaticRoundtrips', () => {
353+
describe('maxToolRoundtrips', () => {
354354
it('should return text, tool calls and tool results from last roundtrip', async () => {
355355
let responseCount = 0;
356356
const result = await generateText({
@@ -468,7 +468,7 @@ describe('maxAutomaticRoundtrips', () => {
468468
},
469469
},
470470
prompt: 'test-input',
471-
maxAutomaticRoundtrips: 2,
471+
maxToolRoundtrips: 2,
472472
});
473473

474474
assert.deepStrictEqual(result.text, 'Hello, world!');

‎packages/core/core/generate-text/generate-text.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ If set and supported by the model, calls will generate deterministic results.
5454
@param maxRetries - Maximum number of retries. Set to 0 to disable retries. Default: 2.
5555
@param abortSignal - An optional abort signal that can be used to cancel the call.
5656
57-
@param maxAutomaticRoundtrips - Maximal number of automatic roundtrips for tool calls.
57+
@param maxToolRoundtrips - Maximal number of automatic roundtrips for tool calls.
5858
5959
@returns
6060
A result object that contains the generated text, the results of the tool calls, and additional information.
@@ -69,6 +69,7 @@ export async function generateText<TOOLS extends Record<string, CoreTool>>({
6969
maxRetries,
7070
abortSignal,
7171
maxAutomaticRoundtrips = 0,
72+
maxToolRoundtrips = maxAutomaticRoundtrips,
7273
...settings
7374
}: CallSettings &
7475
Prompt & {
@@ -87,6 +88,11 @@ The tool choice strategy. Default: 'auto'.
8788
*/
8889
toolChoice?: CoreToolChoice<TOOLS>;
8990

91+
/**
92+
@deprecated Use `maxToolRoundtrips` instead.
93+
*/
94+
maxAutomaticRoundtrips?: number;
95+
9096
/**
9197
Maximal number of automatic roundtrips for tool calls.
9298
@@ -99,7 +105,7 @@ case of misconfigured tools.
99105
100106
By default, it's set to 0, which will disable the feature.
101107
*/
102-
maxAutomaticRoundtrips?: number;
108+
maxToolRoundtrips?: number;
103109
}): Promise<GenerateTextResult<TOOLS>> {
104110
const retry = retryWithExponentialBackoff({ maxRetries });
105111
const validatedPrompt = getValidatedPrompt({ system, prompt, messages });
@@ -156,7 +162,7 @@ By default, it's set to 0, which will disable the feature.
156162
// all current tool calls have results:
157163
currentToolResults.length === currentToolCalls.length &&
158164
// the number of roundtrips is less than the maximum:
159-
roundtrips++ < maxAutomaticRoundtrips
165+
roundtrips++ < maxToolRoundtrips
160166
);
161167

162168
return new GenerateTextResult({

‎packages/core/react/use-chat.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ export function useChat({
224224
onToolCall,
225225
experimental_maxAutomaticRoundtrips = 0,
226226
maxAutomaticRoundtrips = experimental_maxAutomaticRoundtrips,
227+
maxToolRoundtrips = maxAutomaticRoundtrips,
227228
streamMode,
228229
onResponse,
229230
onFinish,
@@ -236,9 +237,15 @@ export function useChat({
236237
api?: string | StreamingReactResponseAction;
237238
key?: string;
238239
/**
239-
@deprecated Use `maxAutomaticRoundtrips` instead.
240+
@deprecated Use `maxToolRoundtrips` instead.
240241
*/
241242
experimental_maxAutomaticRoundtrips?: number;
243+
244+
/**
245+
@deprecated Use `maxToolRoundtrips` instead.
246+
*/
247+
maxAutomaticRoundtrips?: number;
248+
242249
/**
243250
Maximal number of automatic roundtrips for tool calls.
244251
@@ -251,7 +258,7 @@ case of misconfigured tools.
251258
252259
By default, it's set to 0, which will disable the feature.
253260
*/
254-
maxAutomaticRoundtrips?: number;
261+
maxToolRoundtrips?: number;
255262
} = {}): UseChatHelpers & {
256263
/**
257264
* @deprecated Use `addToolResult` instead.
@@ -384,11 +391,11 @@ By default, it's set to 0, which will disable the feature.
384391
// ensure there is a last message:
385392
lastMessage != null &&
386393
// check if the feature is enabled:
387-
maxAutomaticRoundtrips > 0 &&
394+
maxToolRoundtrips > 0 &&
388395
// check that roundtrip is possible:
389396
isAssistantMessageWithCompletedToolCalls(lastMessage) &&
390397
// limit the number of automatic roundtrips:
391-
countTrailingAssistantMessages(messages) <= maxAutomaticRoundtrips
398+
countTrailingAssistantMessages(messages) <= maxToolRoundtrips
392399
) {
393400
await triggerRequest({ messages });
394401
}
@@ -409,7 +416,7 @@ By default, it's set to 0, which will disable the feature.
409416
experimental_onFunctionCall,
410417
experimental_onToolCall,
411418
onToolCall,
412-
maxAutomaticRoundtrips,
419+
maxToolRoundtrips,
413420
messagesRef,
414421
abortControllerRef,
415422
generateId,

0 commit comments

Comments
 (0)
Please sign in to comment.