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
description: Learn how to use tool calling with the useChat hook.
2
+
title: Chatbot with Tools
3
+
description: Learn how to use tools with the useChat hook.
4
4
---
5
5
6
-
# Chatbot with Tool Calling
6
+
# Chatbot with Tools
7
7
8
8
<Notetype="warning">
9
-
The tool calling functionality described here is experimental. It is currently
10
-
only available for React.
9
+
The tool calling functionality described here currently only available for
10
+
React.
11
11
</Note>
12
12
13
13
With `useChat` and `streamText`, you can use tools in your chatbot application.
14
-
The Vercel AI SDK supports both client and server side tool execution.
14
+
The Vercel AI SDK supports three types of tools in this context:
15
+
16
+
1. Automatically executed server-side tools
17
+
2. Automatically executed client-side tools
18
+
3. Tools that require user interaction, such as confirmation dialogs
15
19
16
20
The flow is as follows:
17
21
22
+
1. The user enters a message in the chat UI.
23
+
1. The message is sent to the API route.
24
+
1. The messages from the client are converted to AI SDK Core messages using `convertToCoreMessages`.
18
25
1. In your server side route, the language model generates tool calls during the `streamText` call.
19
-
The messages from the client are converted to AI SDK Core messages using `convertToCoreMessages`.
20
-
2. All tool calls are forwarded to the client.
21
-
3. Server-side tools are executed using their `execute` method and their results are sent back to the client.
22
-
4. The client-side tools are executed on the client.
23
-
The results of client side tool calls can be appended to last assigned message using `experimental_addToolResult`.
24
-
5. When all tool calls are resolved, the client sends the updated messages with the tool results back to the server, triggering another `streamText` call.
26
+
1. All tool calls are forwarded to the client.
27
+
1. Server-side tools are executed using their `execute` method and their results are forwarded to the client.
28
+
1. Client-side tools that should be automatically executed are handled with the `onToolCall` callback.
29
+
You can return the tool result from the callback.
30
+
1. Client-side tool that require user interactions can be displayed in the UI.
31
+
The tool calls and results are available in the `toolInvocations` property of the last assistant message.
32
+
1. When the user interaction is done, `experimental_addToolResult` can be used to add the tool result to the chat.
33
+
1. When there are tool calls in the last assistant message and all tool results are available, the client sends the updated messages back to the server.
34
+
This triggers another iteration of this flow.
25
35
26
36
The tool call and tool executions are integrated into the assistant message as `toolInvocations`.
27
37
A tool invocation is at first a tool call, and then it becomes a tool result when the tool is executed.
@@ -34,16 +44,15 @@ The tool result contains all information about the tool call as well as the resu
34
44
for backward compatibility.
35
45
</Note>
36
46
37
-
## Example: Client-Side Tool Execution
47
+
## Example
38
48
39
-
In this example, we'll define two tools.
40
-
The client-side tool is a confirmation dialog that asks the user to confirm the execution of the server-side tool.
41
-
The server-side tool is a simple fake tool that restarts an engine.
49
+
In this example, we'll use three tools:
42
50
43
-
### Server-side route
51
+
-`getWeatherInformation`: An automatically executed server-side tool that returns the weather in a given city.
52
+
-`askForConfirmation`: A user-interaction client-side tool that asks the user for confirmation.
53
+
-`getLocation`: An automatically executed client-side tool that returns a random city.
44
54
45
-
Please note that only the `restartEngine` tool has an `execute` method and is executed on the server side.
46
-
The `askForConfirmation` tool is executed on the client side.
55
+
### API route
47
56
48
57
```tsx filename='app/api/chat/route.ts'
49
58
import { openai } from'@ai-sdk/openai';
@@ -60,19 +69,30 @@ export async function POST(req: Request) {
60
69
model: openai('gpt-4-turbo'),
61
70
messages: convertToCoreMessages(messages),
62
71
tools: {
63
-
restartEngine: {
64
-
description:
65
-
'Restarts the engine. '+
66
-
'Always ask for confirmation before using this tool.',
67
-
parameters: z.object({}),
68
-
execute: async () =>'Engine restarted.',
72
+
// server-side tool with execute function:
73
+
getWeatherInformation: {
74
+
description: 'show the weather in a given city to the user',
0 commit comments