How to Route Functions in Your Agent Public Preview

DigitalOcean GenAI Platform lets you build GPU-powered AI agents with fully-managed deployment. Agents can use pre-built or custom foundation models, incorporate function and agent routes, and implement RAG pipelines with knowledge bases.


Large language models often require additional functionalities to complete complex tasks. For example, a travel agent may need to retrieve the latest flight prices, hotel rates, tour packages, and available transportation.

Functions are blocks of code that you can call in your agent to enable the foundation model to access external data sources, APIs, and databases to gather information and perform specific actions on demand. Thus, your agent can get real-time, third-party information that may not be present in a static knowledge base and can go beyond static, pre-programmed responses to engage with users in a dynamic, information-rich dialog.

When to Call Functions

You can call functions if your agent needs to perform any of the following:

  • Fetch external data to enrich knowledge and provide timely, relevant information from 3rd-party APIs.

  • Retrieve and analyze user profile data from application user databases to provide personalized recommendations and support.

  • Trigger reports, alerts, or workflows based on API data and user context.

  • Monitor user activity patterns to detect anomalies or issues that require proactive intervention.

  • Consolidate support tickets from various databases, analyze them to identify trending issues or common problems, or automatically escalate high-priority tickets that require immediate attention based on predefined criteria.

Note

In addition to the function, we charge for tokens the agent uses to determine:

  • Whether the user question needs to be routed to a function.

  • Which functions and in what order they are required.

Function Requirements

You need to first create a function using DigitalOcean Functions that meets the following requirements:

  • The function must be a web function.

  • All data types in your schema must be either a string, boolean, or a number.

  • The function’s response must be in a body key as shown below:

    ...
    return { 
        body: {<function response>}
    }
    ...
    
  • Private functions require a secure web function for authentication. When enabled, we apply the function’s password and restrict access to secure mechanisms. We recommend enabling this setting to protect your function from unauthorized access.

We also recommend using Node.js 18 with a 128 MB memory limit and Python 3.9 with a 256 MB memory limit for optimal cold start performance (when the function’s code is downloaded and the execution environment set up). For more information on function runtimes and memory limits, see Configure Functions.

Function Example

To get weather data, you can create a function in JavaScript using the following code:

    
        
            
import axios from 'axios';

async function main(args) {
  const zipCode = args.zipCode || '';
  const measurement = args.measurement || 'F';

  try {
    const response = await axios.get(`https://your-api.com/weather/${zipCode}`, {
      params: { measurement }
    });
    
    const { temperature = 0.0, conditions = 'Unknown' } = response.data;

    return {
      body: {
        temperature,
        conditions,
        measurement
      }
    };
  } catch (error) {
    console.error("Error fetching weather data:", error);
    return {
      body: {
        temperature: 0.0,
        conditions: "Unknown",
        measurement
      }
    };
  }
}

export default main;

        
    

Add a Function Route Using Automation

Adding a function route using the DigitalOcean API requires the unique identifier of the agent and one or more functions to route to. You also need to provide a name and namespace of the function, instructions on when the agent should call the function, and types of data that the function expects as input and output schema.

To obtain a list of agents with their unique identifiers, use the /v2/gen-ai/agents endpoint. You can get the namespace for your function in the Namespace Info section in the Settings tab of your function.

How to Add a Function Route Using the DigitalOcean API
  1. Create a personal access token and save it for use with the API.
  2. Send a POST request to https://api.digitalocean.com/v2/gen-ai/agents/{agent_uuid}/functions

    cURL

    Using cURL:

                                curl -X POST \
      -H "Content-Type: application/json"  \
      -H "Authorization: Bearer $PREVIEW_API_TOKEN" \
      "https://api.digitalocean.com/v2/gen-ai/agents/1b418231-b7d6-11ef-bf8f-4e013e2ddde4/functions" \
      -d '{
        "agent_uuid": "1b418231-b7d6-11ef-bf8f-4e013e2ddde4",
        "function_name": "funzip",
        "description": "Use when you need the zipcode for a city",
        "input_schema": {
          "city": {
            "description": "the city you want a zipcode for",
            "required": true,
            "type": "string"
          }
        },
        "output_schema": {
          "zip_code": {
            "description": "The zipcode of the desired city",
            "type": "number"
          }
        },
        "faas_namespace": "fn-2014dc98-faa1-45f4-ba1f-59910cb3d399",
        "faas_name": "default/get-zipcode"
      }'
     
                            

To view function routes for an agent, use the /v2/gen-ai/agents/{uuid}/child_agents endpoint.

Add a Function Route Using the Control Panel

To call the function in the agent from the control panel, click GenAI Platform from the left menu and then click on the agent you want to add a function route. In the Resources tab, scroll to the Function Route section, and click Add function route to open the Add a function route page.

In the Define the function section, specify the following:

  • Namespace: Select the namespace for your function from the Namespace drop-down list.

  • Function: Select the function name, for example get-weather, from the Select function drop-down list.

  • Function instructions: Specify when the agent should call the function in the text box. For example, you can enter something like Call this function when asked about the weather for a zip code.

Next, define the input and output schema that the agent accepts. The schema provides a detailed description of the inputs, outputs, and the logic required for the agent to call and use these functions:

  • Input schema: Specifies the types of data that the function expects as inputs.

    In the Define input schema section, specify the input schema parameters. For the get-weather function example, add these parameters in the format as shown:

    {
        "zipCode": {
          "type": "string",
          "required": true,
          "description": "The ZIP code for which to fetch the weather"
        },
        "measurement": {
          "type": "string",
          "required": false,
          "description": "The measurement unit for temperature (F or C)"
        }
    }
    
  • Output schema (optional): Specifies the data structure a function returns, helping the agent interpret and use the output. While an input schema and function description work for standalone functions, an output schema helps when functions depend on each other. It shows the agent where data comes from and how it flows through multiple functions. This improves accuracy, especially for complex data structures.

    For example, two functions can get the weather for a location: get-zip-code and get-weather. If get-weather needs a zip code, the function routing starts with get-zip-code, which then routes to get-weather. get-weather retrieves the weather data and passes it to the agent to display or use.

    In the Define output schema section, specify the output schema parameters. For the get-weather function example, you should add these parameters in the format as shown:

    {
        "temperature": {
          "description": "The temperature for the specified location",
          "type": "number"
        },
        "measurement": {
          "description": "The measurement unit used for the temperature (F or C)",
          "type": "string"
        },
        "conditions": {
          "description": "A description of the current weather conditions (Sunny, Cloudy, etc)",
          "type": "string"
        }
    }
    

In the Function route name field, enter a name for the function route and then click Add Function.

Next, you can test the agent in the Agent Playground and then use your agent in a chat bot or embed it in your application.

In this article...