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.
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
AgentFunctionclass, 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 the functions property of the
context.
context.functions.enable(myFunction); // or by name
Disabling: To disable a function, use the disable method on the
functions property of the context.
context.functions.disable(myFunction); // or by name
Disabled functions will not be available to the agent during conversations.
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!