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.
Learning to write clear and concise function instructions helps agents determine when and how to call functions effectively. This improves access to external data sources, APIs, and databases while ensuring accurate responses. Well-structured function instructions define the function’s purpose, inputs, outputs, and constraints, making them more reliable. Following these best practices when routing functions in your agent reduces errors and improves execution.
The following examples provide well-structured function instructions for a get-weather.js
JavaScript function. We have provided the get-weather.js
function along with its input and output JSON below for reference.
Below is an example implementation of the get-weather.js
function with input and output schema examples.
The input schema defines the data the get-weather.js
function requires. The function needs a ZIP code and optionally accepts a temperature measurement unit.
{
"zipCode": {
"type": "string",
"required": true,
"description": "The ZIP code of the location. Only US ZIP codes are supported."
},
"measurement": {
"type": "string",
"required": false,
"description": "The temperature measurement unit, either 'C' or 'F'. The default is 'F'"
}
}
The get-weather.js
function retrieves weather details based on the input schema and returns data in the output schema.
import axios from 'axios';
async function main(args) {
const zipCode = args.zipCode;
const measurement = args.measurement || 'F';
const weatherResponse = await axios.get(`https://your-api.com/weather/${zipCode}`, {
params: { measurement }
});
const { temperature, conditions } = weatherResponse.data;
return {
body: { temperature, measurement, conditions }
};
}
The output schema defines what the get-weather.js
function returns. The function returns the temperature, the measurement unit used, and the weather conditions.
{
"temperature": {
"description": "The current temperature",
"type": "number"
},
"measurement": {
"description": "The measurement unit used for the temperature (F or C)",
"type": "string"
},
"conditions": {
"description": "The unit of temperature measurement.",
"type": "string"
}
}
Agents need straightforward, concise function instructions to determine when and how to call a function. Avoid vague or overly technical descriptions that obscure the function’s purpose.
Define the function’s purpose by:
get_weather
.Prefer | Avoid |
---|---|
Function Name: |
Function Name: |
Define the function’s parameters to ensure agents pass the correct values. Avoid vague descriptions, and explicitly state required and optional parameters.
Describe the function’s input and parameters by:
Prefer | Avoid |
---|---|
Parameters: |
Parameters: |
Agents need to understand what data a function returns, not how it retrieves it. Avoid mentioning algorithms, API calls, or database queries, as they don’t help the agent determine when or how to use the function. Define what the function returns and any limitations on the data provided.
Define the function’s output by:
Prefer | Avoid |
---|---|
Response: |
Response: Calls an external API to fetch weather data and returns the results in JSON format. |
This example integrates function purpose, output, description, and constraints into a complete instruction set, ensuring clarity and usability for agents.
Prefer | Avoid |
---|---|
Function Name: |
Function Name: |