Call Tools in Multiple Steps
Learn how to call tools in multiple steps using the AI TOOLKIT and Next.js
Some language models are great at calling tools in multiple steps to achieve a more complex task. This is particularly useful when the tools are dependent on each other and need to be executed in sequence during the same generation step.
Run it locally
$ npm install aiClient
Let's create a React component that imports the useChat hook from the @ai-toolkit/react module. The useChat hook will call the /api/chat endpoint when the user sends a message. The endpoint will generate the assistant's response based on the conversation history and stream it to the client. If the assistant responds with a tool call, the hook will automatically display them as well.
'use client';
import { useChat } from '@ai-toolkit/react';
import { DefaultChatTransport } from 'ai-toolkit';
import { useState } from 'react';
import type { ChatMessage } from './api/chat/route';
export default function Page() {
const [input, setInput] = useState('');
const { messages, sendMessage } = useChat<ChatMessage>({
transport: new DefaultChatTransport({
api: '/api/chat',
}),
});
return (
<div>
<input
className="border"
value={input}
onChange={event => {
setInput(event.target.value);
}}
onKeyDown={async event => {
if (event.key === 'Enter') {
sendMessage({
text: input,
});
setInput('');
}
}}
/>
{messages.map((message, index) => (
<div key={index}>
{message.parts.map((part, i) => {
switch (part.type) {
case 'text':
return <div key={${message.id}-text}>{part.text}</div>;
case 'tool-getLocation':
case 'tool-getWeather':
return (
<div key={${message.id}-weather-${i}}>
{JSON.stringify(part, null, 2)}
</div>
);
}
})}
</div>
))}
</div>
);
}
- 1'use client';
- 2import { useChat } from '@ai-toolkit/react';
- 3import { DefaultChatTransport } from 'ai-toolkit';
- 4import { useState } from 'react';
- 5import type { ChatMessage } from './api/chat/route';
- 6export default function Page() {
- 7 const [input, setInput] = useState('');
- 8 const { messages, sendMessage } = useChat<ChatMessage>({
- 9 transport: new DefaultChatTransport({
- 10 api: '/api/chat',
- 11 }),
- 12 });
- 13 return (
- 14 <div>
- 15 <input
- 16 className="border"
- 17 value={input}
- 18 onChange={event => {
- 19 setInput(event.target.value);
- 20 }}
- 21 onKeyDown={async event => {
- 22 if (event.key === 'Enter') {
- 23 sendMessage({
- 24 text: input,
- 25 });
- 26 setInput('');
- 27 }
- 28 }}
- 29 />
- 30 {messages.map((message, index) => (
- 31 <div key={index}>
- 32 {message.parts.map((part, i) => {
- 33 switch (part.type) {
- 34 case 'text':
- 35 return <div key={`${message.id}-text`}>{part.text}</div>;
- 36 case 'tool-getLocation':
- 37 case 'tool-getWeather':
- 38 return (
- 39 <div key={`${message.id}-weather-${i}`}>
- 40 {JSON.stringify(part, null, 2)}
- 41 </div>
- 42 );
- 43 }
- 44 })}
- 45 </div>
- 46 ))}
- 47 </div>
- 48 );
- 49}
Server
You will create a new route at /api/chat that will use the streamText function from the ai module to generate the assistant's response based on the conversation history.
You will use the `tools` parameter to specify two tools called getLocation and getWeather that will first get the user's location and then use it to get the weather.
You will add the two functions mentioned earlier and use zod to specify the schema for its parameters.
To call tools in multiple steps, you can use the stopWhen option to specify the stopping conditions for when the model generates a tool call. In this example, you will set it to stepCountIs(5) to allow for multiple consecutive tool calls (steps).
import {
type InferUITools,
type ToolSet,
type UIDataTypes,
type UIMessage,
convertToModelMessages,
stepCountIs,
streamText,
tool,
} from 'ai-toolkit';
import { z } from 'zod';
const tools = {
getLocation: tool({
description: 'Get the location of the user',
inputSchema: z.object({}),
execute: async () => {
const location = { lat: 37.7749, lon: -122.4194 };
return Your location is at latitude ${location.lat} and longitude ${location.lon};
},
}),
getWeather: tool({
description: 'Get the weather for a location',
inputSchema: z.object({
city: z.string().describe('The city to get the weather for'),
unit: z
.enum(['C', 'F'])
.describe('The unit to display the temperature in'),
}),
execute: async ({ city, unit }) => {
const weather = {
value: 24,
description: 'Sunny',
};
return It is currently ${weather.value}°${unit} and ${weather.description} in ${city}!;
},
}),
} satisfies ToolSet;
export type ChatTools = InferUITools<typeof tools>;
export type ChatMessage = UIMessage<never, UIDataTypes, ChatTools>;
export async function POST(req: Request) {
const { messages }: { messages: ChatMessage[] } = await req.json();
const result = streamText({
model: 'openai/gpt-4o',
system: 'You are a helpful assistant.',
messages: await convertToModelMessages(messages),
stopWhen: stepCountIs(5),
tools,
});
return result.toUIMessageStreamResponse();
}
- 1import {
- 2 type InferUITools,
- 3 type ToolSet,
- 4 type UIDataTypes,
- 5 type UIMessage,
- 6 convertToModelMessages,
- 7 stepCountIs,
- 8 streamText,
- 9 tool,
- 10} from 'ai-toolkit';
- 11import { z } from 'zod';
- 12const tools = {
- 13 getLocation: tool({
- 14 description: 'Get the location of the user',
- 15 inputSchema: z.object({}),
- 16 execute: async () => {
- 17 const location = { lat: 37.7749, lon: -122.4194 };
- 18 return `Your location is at latitude ${location.lat} and longitude ${location.lon}`;
- 19 },
- 20 }),
- 21 getWeather: tool({
- 22 description: 'Get the weather for a location',
- 23 inputSchema: z.object({
- 24 city: z.string().describe('The city to get the weather for'),
- 25 unit: z
- 26 .enum(['C', 'F'])
- 27 .describe('The unit to display the temperature in'),
- 28 }),
- 29 execute: async ({ city, unit }) => {
- 30 const weather = {
- 31 value: 24,
- 32 description: 'Sunny',
- 33 };
- 34 return `It is currently ${weather.value}°${unit} and ${weather.description} in ${city}!`;
- 35 },
- 36 }),
- 37} satisfies ToolSet;
- 38export type ChatTools = InferUITools<typeof tools>;
- 39export type ChatMessage = UIMessage<never, UIDataTypes, ChatTools>;
- 40export async function POST(req: Request) {
- 41 const { messages }: { messages: ChatMessage[] } = await req.json();
- 42 const result = streamText({
- 43 model: 'openai/gpt-4o',
- 44 system: 'You are a helpful assistant.',
- 45 messages: await convertToModelMessages(messages),
- 46 stopWhen: stepCountIs(5),
- 47 tools,
- 48 });
- 49 return result.toUIMessageStreamResponse();
- 50}