Skip to content

Commit f39c0dd

Browse files
authoredMay 28, 2024··
feat (core,providers,rsc): add toolChoice setting (#1718)
1 parent cc2197f commit f39c0dd

32 files changed

+2156
-753
lines changed
 

‎.changeset/happy-hats-joke.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@ai-sdk/google-vertex': patch
3+
'@ai-sdk/anthropic': patch
4+
'@ai-sdk/mistral': patch
5+
'@ai-sdk/google': patch
6+
'@ai-sdk/openai': patch
7+
---
8+
9+
feat (provider): implement toolChoice support

‎.changeset/long-dodos-promise.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/provider': patch
3+
---
4+
5+
feat (provider): add toolChoice to language model specification

‎.changeset/short-phones-lie.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': patch
3+
---
4+
5+
feat (core, rsc): add toolChoice setting

‎content/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx

+49-10
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ While large language models have incredible generation capabilities,
99
they struggle with discrete tasks (eg. mathematics) and interacting with the outside world (eg. getting the weather).
1010
Tools can be thought of as programs you give to a model which can be run as and when the model deems applicable.
1111

12-
<Note>
13-
When a model uses a tool, it is called a "tool call" and the output of the
14-
tool is called a "tool result".
15-
</Note>
12+
## Tools
1613

14+
A tool is an object that can be called by the model to perform a specific task.
1715
You can use tools with the `generateText` or `streamText` functions, by passing a tool(s) to the `tools` parameter.
1816

1917
There are three elements of a tool, a description, parameters, and an optional execute function.
@@ -22,11 +20,6 @@ There are three elements of a tool, a description, parameters, and an optional e
2220
- **`parameters`**: A [Zod](https://zod.dev/) schema that defines the parameters. It is converted to a JSON schema that is consumed by the LLM, and also used to validate the LLM tool calls.
2321
- **`execute`**: An optional async function that is called with the arguments from the tool call and produces a value of type `RESULT` (generic type). It is optional because you might want to forward tool calls to the client or to a queue instead of executing them in the same process.
2422

25-
<Note>
26-
You can use the `tool` helper function to infer the types of the `execute`
27-
parameters.
28-
</Note>
29-
3023
The `tools` parameter of `generateText` and `streamText` is an object that has the tool names as keys and the tools as values:
3124

3225
```ts highlight="7-18"
@@ -53,6 +46,52 @@ const result = await generateText({
5346
});
5447
```
5548

56-
If the LLM decides to use a tool, it will generate a tool call. Tools with an `execute` function are run automatically when these calls are generated.
49+
<Note>
50+
You can use the `tool` helper function to infer the types of the `execute`
51+
parameters.
52+
</Note>
53+
54+
If the LLM decides to use a tool, it will generate a tool call.
55+
Tools with an `execute` function are run automatically when these calls are generated.
5756
The results of the tool executions are returned using tool result objects.
5857
Each tool result object has a `toolCallId`, a `toolName`, a typed `args` object, and a typed `result`.
58+
59+
<Note>
60+
When a model uses a tool, it is called a "tool call" and the output of the
61+
tool is called a "tool result".
62+
</Note>
63+
64+
## Tool Choice
65+
66+
You can use the `toolChoice` setting to influence when a tool is selected.
67+
It supports the following settings:
68+
69+
- `auto` (default): the model can choose whether and which tools to call.
70+
- `required`: the model must call a tool. It can choose which tool to call.
71+
- `none`: the model must not call tools
72+
- `{ type: 'tool', tooName: string (typed) }`: the model must call the specified tool
73+
74+
```ts highlight="19"
75+
import { z } from 'zod';
76+
import { generateText, tool } from 'ai';
77+
import { openai } from '@ai-sdk/openai';
78+
79+
const result = await generateText({
80+
model: openai('gpt-4-turbo'),
81+
tools: {
82+
weather: tool({
83+
description: 'Get the weather in a location',
84+
parameters: z.object({
85+
location: z.string().describe('The location to get the weather for'),
86+
}),
87+
execute: async ({ location }) => ({
88+
location,
89+
temperature: 72 + Math.floor(Math.random() * 21) - 10,
90+
}),
91+
}),
92+
},
93+
toolChoice: 'required', // force the model to call a tool
94+
prompt:
95+
'What is the weather in San Francisco and what attractions should I visit?',
96+
});
97+
```

0 commit comments

Comments
 (0)
Please sign in to comment.