You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: content/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx
+49-10
Original file line number
Diff line number
Diff line change
@@ -9,11 +9,9 @@ While large language models have incredible generation capabilities,
9
9
they struggle with discrete tasks (eg. mathematics) and interacting with the outside world (eg. getting the weather).
10
10
Tools can be thought of as programs you give to a model which can be run as and when the model deems applicable.
11
11
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
16
13
14
+
A tool is an object that can be called by the model to perform a specific task.
17
15
You can use tools with the `generateText` or `streamText` functions, by passing a tool(s) to the `tools` parameter.
18
16
19
17
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
22
20
- **`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.
23
21
- **`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.
24
22
25
-
<Note>
26
-
You can use the `tool` helper function to infer the types of the `execute`
27
-
parameters.
28
-
</Note>
29
-
30
23
The `tools` parameter of `generateText` and `streamText` is an object that has the tool names as keys and the tools as values:
31
24
32
25
```ts highlight="7-18"
@@ -53,6 +46,52 @@ const result = await generateText({
53
46
});
54
47
```
55
48
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.
57
56
The results of the tool executions are returned using tool result objects.
58
57
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'),
0 commit comments