Skip to main content

Functions

Automate actions for your users with functions. Convert your AI Assistant into an AI Agent that can perform tasks on behalf of your users.

Functions Page

How do Functions Work?

To understand how functions work, let's take a look at a simple example. Assume you have a support form that your users can use to contact your team. You want to allow them to fill out and submit a ticket, but only using the chat interface of your AI Assistant. Here's what that flow would look like:

  1. The user tells the assistant that they want to contact support.
  2. The assistant prompts the user to provide the details required for the ticket. For example: Please provide a description of the issue you are facing and your email address.
  3. The user provides the required information.
  4. The assistant processes the information, creates a ticket and responds to the user.

Functions enable you to do exactly this. You can define functions that you already have in your existing codebase and automate them via the Assistant. Simply create a definition for the function you want to automate and the Assistant will handle the rest.

How did that work?

Let's break down the steps that happened in the background:

Functions Flow

  1. The user tells the assistant that they want to contact support.

  2. The assistant prompts the user to provide the details required to run the raise_a_support_ticket function. In this case, the function expects the user to provide a description of the issue and their email address.

  3. The user provides the required information.

  4. The assistant processes the information and calls the raise_a_support_ticket function, providing the user's email address and issue description as parameters.

    {
    "issue": "<the description of the issue>",
    "email": "<the user's email address>"
    }
  5. Your website makes an API call with the provided parameters to your backend, which then creates a ticket and returns a response

  6. This response is sent back to the Assistant, which then responds to the user.

tip

Use descriptive responses along with the return value of the function to allow the Assistant to provide a more apt response to the user.

Add a Function

  1. Go to the Functions section in the navigation panel.

  2. Click on the Plus button to the right of the search bar to add a new function.

    Add Function Button

  3. From the dropdown, select the related action for the function you want to automate. If you haven't added an action for it, you can add it via the Actions section.

    Add Function Form

    Alternatively, if you want to add a tool that's not related to any function, you can add it as a custom function. Simply click on the checkbox that says "This tool is not related to any action.". This will let you create a custom tool with a name and description instead of using a related action.

    Function Custom

  4. Optionally, you can add parameters for the function. Parameters are the data needed to execute the function. The Assistant will collect the parameters from the user and pass them to the function.

    Function Parameters

    Parameters have the following format:

    • Name: The name of the parameter.
    • Description: A description of what the parameter is for. Provide a detailed description to allow the Assistant to understand the purpose of the parameter.
    • Type: The data type of the parameter. Use the dropdown to select the appropriate data type. Currently supported types are: string, number and boolean.
    • Required: A checkbox to indicate whether the parameter is required or not.
tip

If you don't have any parameters, consider using an action instead. Add an action and add the required logic to the action handler. This will allow the Assistant to handle the function automatically (if you don't need to collect any parameters).

  1. Click Save. You’ll see a success message confirming the function creation.

    Add Function Success

Edit a Function

  1. Go to the Functions section in the navigation panel.

  2. Locate the function you want to edit.

  3. Click on the Pencil icon on the top-right of the function.

    Edit Function Button

  4. Make the necessary changes to the function.

  5. Click Save. You’ll see a success message confirming the function update.

Delete a Function

  1. Go to the Functions section in the navigation panel.

  2. Locate the function you want to delete.

  3. Click on the Trash icon on the top-right of the function.

    Delete Function Button

  4. Click Delete. You’ll see a modal to confirm the deletion.

    Delete Function Modal

  5. Click Confirm. You’ll see a success message confirming the function deletion.

How to integrate Functions?

info

Functions can be used with actions that the model is not trained on. However, it is recommended to use only with actions that the model is trained on.

Just like Actions, functions need to be provided via a key-value pair, where the key is the function name and the value is a function handler.

const functions = {
raise_a_support_request: function ({ issue, email }) {
// Your function logic here
if(/* success */) {
return "Ticket created successfully";
}else{
return "Ticket creation failed";
}
},
};

Use the functions map provided in the Functions tab of the API Key Integration Modal to get the list of functions along with their correct signatures.

The return value of the function should be the status of the function execution. If the function is successful, return success, else return error. You can also return detailed success messages, this will be used by the assistant to provide more context to the user.