Tools Calling

Learn how to create and configure Tools Calling with Vapi.

This guide focuses on configuring tools within your Vapi assistant using the provided sample payload as a reference. We’ll explore how to adapt the existing structure to fit your specific needs and desired functionalities.

Understanding the Tool Structure

The sample payload demonstrates the configuration for a “function” type tool. Let’s break down its key components:

1{
2 "type": "function",
3 "messages": [ ... ],
4 "function": { ... },
5 "async": false,
6 "server": { ... }
7}
  1. type: This remains as “function” if you’re setting up an API call.
  2. messages: This array holds the messages the AI will communicate to the user at different stages of the tool’s execution.
  3. function: Defines the function details:
    • name: The unique identifier for your function.
    • parameters: (Optional) An object describing the parameters expected by the function.
    • description: (Optional) A description of the function’s purpose.
  4. async: Set to “true” if the function call should be asynchronous, allowing the AI to continue without waiting for the response. Use “false” for synchronous calls.
  5. server: Provides the URL of the server where the function is hosted.

Adapting the Payload for Your Needs

  1. Modify Function Details:
    • Change the “name” to reflect your specific function.
    • Adjust the “parameters” object according to the data your function requires.
    • Update the “description” to accurately explain the function’s purpose.
  2. Customize Messages:
    • Edit the content of each message within the “messages” array to align with your desired communication style and information.
    • Add or remove messages as needed. You can include messages like “request-start”, “request-response-delayed”, “request-complete”, and “request-failed” to inform the user about the tool’s progress.
  3. Set Server URL:
    • Replace the existing URL in the “server” object with the endpoint of your server hosting the function.
  4. Choose Asynchronous Behavior (Optional):
    • Change “async” to “true” if you want the AI to continue without waiting for the function’s response.

Example Modification

Let’s say you want to create a tool that fetches the weather for a given location. You would modify the payload as follows:

1{
2 "type": "function",
3 "messages": [
4 {
5 "type": "request-start",
6 "content": "Checking the weather forecast. Please wait..."
7 },
8 {
9 "type": "request-complete",
10 "content": "The weather in location is"
11 },
12 {
13 "type": "request-failed",
14 "content": "I couldn't get the weather information right now."
15 },
16 {
17 "type": "request-response-delayed",
18 "content": "It appears there is some delay in communication with the weather API.",
19 "timingMilliseconds": 2000
20 }
21 ],
22 "function": {
23 "name": "get_weather",
24 "parameters": {
25 "type": "object",
26 "properties": {
27 "location": {
28 "type": "string"
29 }
30 }
31 },
32 "description": "Retrieves the current weather for a specified location."
33 },
34 "async": false,
35 "server": {
36 "url": "https://your-weather-api.com/weather"
37 }
38}

Adding More Tools

Simply create additional tool objects within the “tools” array, following the same structure and modifying the details as needed. Each tool can have its own unique configuration and messages.

Server Response Format: Providing Results and Context

When your Vapi assistant calls a tool (via the server URL you configured), your server will receive an HTTP request containing information about the tool call. Upon processing the request and executing the desired function, your server needs to send back a response in the following JSON format:

1{
2 "results": [
3 {
4 "toolCallId": "X",
5 "result": "Y"
6 }
7 ]
8}

Breaking down the components:

  • toolCallId (X): This is a unique identifier included in the initial request from Vapi. It allows the assistant to match the response with the corresponding tool call, ensuring accurate processing and context preservation.
  • result (Y): This field holds the actual output or result of your tool’s execution. The format and content of “result” will vary depending on the specific function of your tool. It could be a string, a number, an object, an array, or any other data structure that is relevant to the tool’s purpose.

Example:

Let’s revisit the weather tool example from before. If the tool successfully retrieves the weather for a given location, the server response might look like this:

1{
2 "results": [
3 {
4 "toolCallId": "call_VaJOd8ZeZgWCEHDYomyCPfwN",
5 "result": "San Francisco's weather today is 62°C, partly cloudy."
6 }
7 ]
8}

Some Key Points:

  • Pay attention to the required parameters and response format of your functions.
  • Ensure your server is accessible and can handle the incoming requests from Vapi.
  • Make sure to add “Tools Calls” in both the Server and Client messages and remove the function calling from it.

By following these guidelines and adapting the sample payload, you can easily configure a variety of tools to expand your Vapi assistant’s capabilities and provide a richer, more interactive user experience.

Video Tutorial: