In Delphi, agent functions are a versatile tool allowing your conversational agent to execute specific tasks, like data retrieval or complex computations, dynamically during a conversation. Let's dive into how you can utilize these functions in your Delphi projects.
- Custom Functions: You can define custom functions tailored to your specific needs. These functions can perform various tasks like fetching data from an API, processing input, or carrying out computations.
- Schema Validation: Delphi uses JSON schemas to validate the inputs of these functions, ensuring data integrity and correctness.
-
Function Registration: After defining a function, you need to register it with the context. This registration process tells the context which functions are available for use during a conversation.
const myFunction = new AgentFunction(/* ... configuration ... */); context.addFunction(myFunction, true); // second argument is whether to enable immediately
For more information on the
AgentFunction
class, see the API reference.
-
Enabling: Once registered, a function may not be immediately enabled. This step offers control over which functions are active at any given time. To enable a function, use the
enable
method on thefunctions
property of the context.context.functions.enable(myFunction); // or by name
-
Disabling: To disable a function, use the
disable
method on thefunctions
property of the context.context.functions.disable(myFunction); // or by name
Disabled functions will not be available to the agent during conversations.
- During Conversations: The model may invoke functions during a conversation. This will enhance the agent's context, allowing it to respond more intelligently to the user, and to perform more complex tasks.
- Response Integration: The results from function calls are seamlessly integrated into the conversation. Depending on your function's design, it can directly influence the agent's next response or modify the conversation's context.
import { AgentFunction, type JSONSchemaType } from "@wecandobetter/delphi";
interface WeatherParameters {
location: string;
}
interface WeatherData {
temperature: number;
humidity: number;
windSpeed: number;
}
const weatherSchema: JSONSchemaType<WeatherParameters> = {
type: "object",
properties: {
location: {
type: "string",
description:
"The location to fetch weather information for (e.g. 'The Hague, NL')",
},
},
required: ["location"],
additionalProperties: false,
};
// Define a custom function
const fetchWeatherInfo = new AgentFunction<WeatherParameters, WeatherData>(
"getWeather",
"Fetch current weather information",
weatherSchema, // Your defined JSON schema for input validation
async (params) => {
// Logic to fetch weather information
return {
temperature: 22.5, // in Celsius
humidity: 0.5,
windSpeed: 10,
},
);
Agent functions in Delphi provide a powerful way to extend the capabilities of your conversational agents. By defining custom functions, registering them with your agent, and then utilizing them in conversation flows, you can create rich, interactive, and dynamic conversational experiences. The key is to tailor these functions to fit the specific needs and contexts of your application. Happy coding!