Call Tools in Parallel
Learn how to call tools in parallel using the AI TOOLKIT in Node.js
Some language models support calling tools in parallel. This is particularly useful when multiple tools are independent of each other and can be executed in parallel during the same generation step. import { generateText, tool } from 'ai-toolkit'; import { z } from 'zod'; __PROVIDER_IMPORT__; const result = await generateText({ model: __MODEL__, tools: { weather: tool({ description: 'Get the weather in 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 }) => { // This function would normally make an API request to get the weather. const weather = { value: 25, description: 'Sunny' }; return It is currently ${weather.value}°${unit} and ${weather.description} in ${city}!; }, }), }, prompt: 'What is the weather in Paris and New York?', }); // The model will call the weather tool twice in parallel console.log(result.toolCalls); // [ // { toolName: 'weather', input: { city: 'Paris', unit: 'C' } }, // { toolName: 'weather', input: { city: 'New York', unit: 'C' } } // ] console.log(result.toolResults); // [ // { toolName: 'weather', input: { city: 'Paris', unit: 'C' }, output: 'It is currently 25°C and Sunny in Paris!' }, // { toolName: 'weather', input: { city: 'New York', unit: 'C' }, output: 'It is currently 25°C and Sunny in New York!' } // ]
- 1import { generateText, tool } from 'ai-toolkit';
- 2import { z } from 'zod';
- 3__PROVIDER_IMPORT__;
- 4const result = await generateText({
- 5model: __MODEL__,
- 6tools: {
- 7weather: tool({
- 8description: 'Get the weather in a location',
- 9inputSchema: z.object({
- 10city: z.string().describe('The city to get the weather for'),
- 11unit: z
- 12.enum(['C', 'F'])
- 13.describe('The unit to display the temperature in'),
- 14}),
- 15execute: async ({ city, unit }) => {
- 16// This function would normally make an API request to get the weather.
- 17const weather = { value: 25, description: 'Sunny' };
- 18return `It is currently ${weather.value}°${unit} and ${weather.description} in ${city}!`;
- 19},
- 20}),
- 21},
- 22prompt: 'What is the weather in Paris and New York?',
- 23});
- 24// The model will call the weather tool twice in parallel
- 25console.log(result.toolCalls);
- 26// [
- 27// { toolName: 'weather', input: { city: 'Paris', unit: 'C' } },
- 28// { toolName: 'weather', input: { city: 'New York', unit: 'C' } }
- 29// ]
- 30console.log(result.toolResults);
- 31// [
- 32// { toolName: 'weather', input: { city: 'Paris', unit: 'C' }, output: 'It is currently 25°C and Sunny in Paris!' },
- 33// { toolName: 'weather', input: { city: 'New York', unit: 'C' }, output: 'It is currently 25°C and Sunny in New York!' }
- 34// ]
Run it locally
$ npm install ai