Site icon CodeRevolution Knowledge Base

Aiomatic Plugin Function Calling Documentation

The Function Calling feature in the Aiomatic plugin empowers developers to extend the capabilities of the AI writer by instructing it to execute custom functions. With this powerful functionality, developers can seamlessly integrate AI-generated content with practical actions, such as sending emails, creating files, writing to databases, and performing other tasks that were previously not possible. This documentation provides a step-by-step guide on how to use the Function Calling feature to leverage the full potential of the Aiomatic plugin.

Prerequisites

Before you proceed, ensure that you have the following prerequisites in place:

  1. Latest Version of Turbo or GPT-4:
    • To use the Function Calling feature, you need the latest version of Turbo (gpt-3.5-turbo-0613 or higher) or GPT-4 (gpt-4-0613 or higher). Please verify that your AI model is up-to-date, especially if you are using Azure with deployments of models that might not be the latest ones.

Enabling Function Calling

To enable Function Calling in Aiomatic, follow these steps:

  1. Add AI Functions: The first step is to define the custom functions that you want the AI to be able to call. These functions will be accessible to the AI as part of the AI query. Each function requires a name, description, and a list of parameters it accepts.
  1. Hook into ‘aiomatic_ai_functions’ Filter: Use the add_filter function to hook into the ‘aiomatic_ai_functions’ filter and add your custom functions. This filter provides a structured array that contains the custom functions, their descriptions, and their respective parameters.
add_filter('aiomatic_ai_functions', function ($query) {
    $functions = array();
    $functions['functions'][] = new Aiomatic_Query_Function(
        'send_email',
        'Send an email to the administrator of this website',
        [
            new Aiomatic_Query_Parameter('subject', 'The subject of the email', 'string', true),
            new Aiomatic_Query_Parameter('message', 'The message of the email', 'string', true)
        ]
    );
    $functions['message'] = 'Sure, I just sent an email to admin, he will respond soon!';
    return $functions;
}, 999, 1);
  1. Handle AI Replies with Custom Functions: The next step is to handle AI replies and identify if a function call is present in the reply. If the AI generates a function call, extract the function name and its arguments from the reply and execute the corresponding custom function. You will be able to do this, by using the aiomatic_ai_reply_raw filter. Use the add_filter function to hook into the ‘aiomatic_ai_reply_raw’ filter and process AI replies. In this filter, you will check if a function call exists in the AI reply and execute the corresponding custom function with the provided arguments. Be sure to json_decode the arguments of functions, as at this stage, they are still in encoded textual format. For example:
add_filter('aiomatic_ai_reply_raw', function ($reply, $query) {
    if (isset($reply->function_call) && !empty($reply->function_call)) {
        $function_call = $reply->function_call;
        if (isset($function_call->arguments) && is_string($function_call->arguments)) {
            $function_call->arguments = json_decode($function_call->arguments);
        }
        if ($function_call->name === 'send_email') {
            $subject = $function_call->arguments->subject;
            $message = $function_call->arguments->message;
            mail("admin@yoursite.com", $subject, $message);
            if(!isset($reply->choices))
            {
                $reply->choices = array();
                $reply->choices[0] = new stdClass();
            }
            //this is optional, here you can set the text which will be displayed as the AI response (only in the response streaming mode). You can output a simple text, directly the result of your function call or parse the function call result through the AI writer, for a sintetized response.
            $reply->choices[0]->text = 'Email Sent!';
        }
    }
    return $reply;
}, 10, 2);

Using Function Calling in AI Queries

After the above steps are done, you can have a conversation with the chatbot, which will actually send an email to the email address you defined in the custom code you added:

Important Considerations

Exit mobile version