Functions represent a powerful tool for extending the capabilities of your AI, allowing it to perform specific actions and interact with external systems. With AIsuru, you can easily create custom functions to meet the unique needs of your project.
What are functions?
Functions are instructions that allow the AI to perform operations outside its normal conversational scope - this is known as “function calling.” They can be used to:
- Retrieve information from external databases;
- Send emails or notifications;
- Perform complex calculations;
- Interact with third-party APIs.
By using functions, you can transform your AI from a simple conversational assistant to a powerful tool capable of automating processes and providing real-time data-driven responses.
How to create a function in AIsuru
Where is the functions section?
To create a new function in AIsuru, you need to:
- Access the platform;
- Create a twin or open the management panel of an existing twin;
- Select “Dynamic Intents” from the side menu;
- Click on the “Functions” tab at the top of the page;
- Press "New Function" and fill out all the fields.
Creating a new function
At this point, a new screen will open where you can define the details of your function:
- Name: Enter a unique and descriptive name for your function. This name will be used internally by the system to identify the function.
- Description: Provide a brief explanation of the function’s purpose. This description is essential as it helps the generative AI understand when and how to use the function in the context of a conversation.
- Webhook: Enter the full URL of the webhook that will be called when the function is invoked. This URL should point to an endpoint that executes your function’s custom logic.
- HTTP Method: Select the appropriate HTTP method (GET, POST, PUT, DELETE) for your request to the webhook. The choice depends on the action the function needs to perform.
- HTTP Header: Here you can add any custom HTTP headers needed for your request. These may include authentication tokens or other metadata required by your endpoint.
- Template Query String: Define the format of the query string for the HTTP request. Use the syntax {parameter} to indicate where dynamic values will be inserted in the query string.
- Template HTTP Body: Specify the body of the HTTP request. As with the query string, you can use {parameter} to indicate where to insert dynamic values in the request body.
- Send Extension Header: Select which additional conversation context information you want to include in the request. These may include session ID, user tags, current date and time, location, and context variables.
In the "Parameters" section, you can define the parameters accepted by your function, specifying name, description, and type for each. These are dynamic values (name, email, a message) that the AI can ask the user or deduce from the conversation.
You can use the "Test" section to verify that your function works correctly before implementing it.
Integrating functions into your project
Once created, functions can be easily integrated into your AI twins. The generative AI will be able to recognize when it’s appropriate to use a function and will invoke it automatically, thereby enriching its responses with customized data and actions.
With functions, you can create more dynamic and interactive AI experiences, perfectly tailored to the needs of your project or organization.
Note: Functions are executed directly by the AIsuru platform, not by the underlying language model (LLM). This way, AIsuru maximizes the AI’s capabilities, allowing it to interact with external systems and perform complex actions safely and securely.
Example of a Function Configuration
Scenario: Let’s create an AI assistant that provides the current weather conditions for any city, using the free OpenWeatherMap API.
Function Configuration
- Log in to AIsuru and open your twin’s panel;
- Go to "Dynamic Intents" > "Functions" tab;
- Click "New Function".
Fill in the fields:
- Name: getCurrentWeather;
- Description: Retrieves the current weather conditions for a specific city;
- Webhook: https://api.openweathermap.org/data/2.5/weather;
- HTTP Method: GET;
- HTTP Header: Content-Type/json;
- Template Query String: q={city}&appid={API_KEY}&units=metric&lang=it
- Template HTTP Body: (leave empty).
Important Security Note: The API key is included directly in the query string as "appid." Replace {API_KEY} with your OpenWeatherMap API key. For optimal security in a production environment, consider using environment variables or secret management systems to store and access your API key.
To obtain an OpenWeatherMap API key:
- Go to https://openweathermap.org/;
- Create a free account;
- Access the "API keys" section in your profile;
- Use the generated key.
Add the parameter to the function:
- Name: CITY;
- Description: Name of the city;
- Type: String;
- Set the parameter as required.
Test
In the "Test" section, enter a city (e.g., "Rome"); Click "Run Test"; Verify that the response contains the correct weather data.
Instructions
Remember to explain the function’s usage to the AI twin in Settings > Prompt.Instructions for the AI Twin (Settings > Prompt).
Example Interaction
- User: "What's the weather like in Milan today?"
- The Twin invokes getCurrentWeather with "Milan" as the parameter, obtains the response from the API, and replies;
- The Twin: "In Milan, the temperature is 22°C with clear skies. Humidity is at 45%, and the wind is blowing at 3.5 m/s. Do you want to know the weather in another city?".