Your First Function Call
Function Calling allows your chatbot to run code on itself. Running code means enabling interactions with virtually anything you want, making it a very powerful tool. Of course, this requires technical knowledge. We will make a simple example here to help you get started. To make things extra simple, we will use Snippet Vault so that the function we create will be automatically selectable in AI Engine and even usable with OpenAI Assistants.
First, make sure you are using a model compatible with Function Calling, as not all models are. Here, we are using OpenAI GPT-4. If you are selecting an OpenAI Assistant, ensure that it is also using a suitable model.
Then we can go into Snippet Vault to create our new function. Snippet Vault is a Meow Apps plugin that can be used to store code snippets for your WordPress website. If you are familiar with Code Snippet, you should already understand this concept. However, Snippet Vault has a special feature: you can create a specific type of snippet called “Functions.” As the name suggests, this will be a PHP function that runs on your server once executed and can be used by an AI model through function calling.
Let’s get started by going to the Snippet Vault dashboard. Click the “+” button to open a new code editor.
Now, for this tutorial, let’s create a function that sends an email notification when a new conversation starts on your chatbot. Of course, this can only work if your server is already configured to send emails, either through an SMTP server or a third-party service. In this example, we will use only built-in WordPress functions.
function send_mail_notifier( $first_message, $topic ) {
// Get the website admin email
$admin_email = "admin@website.com";
// You can of course get your mail from an option if you have this setup already:
// $admin_email = get_option( 'admin_email' );
// Get the current date
$current_date = date('Y-m-d H:i:s');
// Check if user is logged in
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$user_info = "User ID: " . $current_user->ID . "\n";
$user_info .= "Username: " . $current_user->user_login . "\n";
$user_info .= "Email: " . $current_user->user_email . "\n";
} else {
$user_info = "User: Guest\n";
}
// Prepare the email subject and message
$subject = "Notification from your website";
$message = "Date: " . $current_date . "\n";
$message .= $user_info;
$message .= "Message: " . $first_message . "\n";
$message .= "Topic: " . $topic . "\n";
// Send the email
wp_mail( $admin_email, $subject, $message );
}
So the code above will effectively send you an email containing information about the user (if they are logged in), the content of the first message, and the initiated topic. If you are wondering how these parameters ($first_message, $topic) are going to be handled, don't worry. The AI will determine these values for you, so you don't have to do anything. Feel free to modify the code to fit your use case.
Now that the code is ready, we are going to paste it inside the Snippet Vault code editor and make sure that we have selected the “Function” type.
In the image above, we also inserted a description (2) of what the code is supposed to do. The AI has access to this information, so it will help it understand why and when to use this specific code. We can also add descriptions for the parameters, as their usage may not be obvious to the AI. Consider these descriptions as “little prompts” that tell the AI what to use for each parameter.
Here are the different descriptions used in this example:
- “$first_message: The first message of the conversation, from the user.”
- “$topic: If the first message evokes a certain specific topic, specify it.”
You can also see a “default value” setting here. This is used if, for some reason, the parameter is null, allowing you to manually choose a default value.
Before finalizing this code, you have the ability to test it to ensure everything is running smoothly and there are no mistakes in your code. This step is not mandatory, so you can skip it if you want, but it might be handy when you create your own snippets. What we are going to do is temporarily replace the function that sends us an email with an “echo” statement. This way, it won't send anything, but we will have a chance to check the values that would have been sent in the “test console.” So, the last lines of our code should be replaced as shown below.
// Send the email
// wp_mail($admin_email, $subject, $message);
echo( json_encode( [ $subject, $message ] ) );
Now you can head to the “Test” tab. Here, you have the possibility to select all the parameters your function has and attribute them a test value. In the example below, the parameter “$first_message” has the test value “This is a message.” We don’t see it here, but the “$topic” has the value “This is a topic.” If you have previously inserted a “default value” for your parameters, you don’t even have to choose any test values; leaving them empty will cause the default value to be used. So, if we now run the test, we should see an output containing a preview of our email.
So far, so good! Don’t forget to replace that temporary “echo” statement back with the actual code to send an email, of course, because we are now ready to test it live with the chatbot. Go back to AI Engine, where you have your chatbot ready to use functions. You can now see in the “Functions” dropdown menu our newly created snippet. Just select it, and you're done. Function Calling becomes that simple with AI Engine!
To make things extra clear for our Chatbot, let’s modify our instructions to specify that whenever a new conversation starts, it should use our function. Here's the updated instruction for the current example:
“When a new conversation starts, use the send_mail_notifier function. This function will include the customer's first message and the related topic. If there doesn't seem to be a topic, such as only greetings or a simple question, ask the customer what specific topic they are inquiring about, and then use the function to send the mail. Once this function has been used, it should not be used again in the current discussion.”
Feel free to adjust this for your specific needs. Now, let’s see it in action.
It sounds like everything is running smoothly! While the chatbot is engaging in conversation as usual, let's check our email to see what's waiting for us.
Beautiful! Imagine all that you can achieve with Function Calling. Now, it’s your turn to give it a try! Here’s a little exercise to see if everything is clear for you: instead of using "Notification from your website" as the email title, let’s use our "Topic" which will convey more information. It’s just a little tweak to do in the snippet code, but I’m sure you can handle it.