Adding a Tool
Head back to your route handler and define your first tool: getWeather
. This tool will be used to get the current weather at a location. We will use the tool
helper from the AI SDK to define the tool.
First, you need to add a description to your tool. This is what the model will use to decide when to use it.
Now, we have to define the parameters that the tool needs in order to run. We're using Zod to define the schema for the parameters.
We can use the model's generative abilities to define parameters that can be inferred from the conversation. In this case, we will need the latitude, longitude, and city of the location we want to get the weather for. We expect the user will provide the city name, and the model can generate the latitude and longitude from that.
Finally we define an execute function. This is the code that will be run when the tool is called.
Head back to the terminal and ask for the weather in SF.
There's just a blank response... This is because the model generated a tool call rather than a message. Let's render tool calls and results in the UI.
Head back to your page.tsx and add the following code to render tool calls and results.
Save, jump back to the browser, and if the page hasn't refreshed, you should see the tool result in the UI.