Interactive & Static Functions (Function Calling)

ℹ️
You can enrich or write answers from the AI with functions run locally, on your own WordPress. When this feature was first released by OpenAI, it was called Function Calling. Anthropic (Claude) also uses the same name.
 

In AI Engine, there are two types of functions that you can feed to the AI: interactive and static.

Interactive Functions

An interactive function will be used to enrich the final reply from the AI with additional data. Interactive functions can also be called in cascade, depending on what kind of functions you have, and what the AI model decides. For example, if you ask the AI to “send me an email”, it will first need to call a function to get your email, then a function to send an email.

It's recommended to use interactive function, as the final answer will be written by AI, and therefore will flow naturally. You can register your functions using Snippet Vault, or manually.

Snippet Vault

The first step is to download the "Snippet Vault" plugin. This will allow you to store PHP snippet code inside a dashboard and execute them whenever you want, even using a REST API. If you are familiar with Code Snippet, you can even import all of your code from there for an easy switch.

You have a special type of snippet in Snippet Vault called "function". As the name suggests, this snippet can only contain one unique PHP function. You can declare its arguments and define them. Once this is done, AI model can use them easily, and you have nothing more to do on your side.

 
Notion image

Simply navigate to your chatbot settings and locate the new "Functions" section. Then, select the function you want to use for the current chatbot session. From there, function calling is automatically handled, streamlining the process for you.

Notion image
Notion image

Manually

Part 1: Declaring Functions

There are two ways to declare functions for use with AI Engine:

Method 1: Register Functions Naturally

This filter allows functions to be selectable in the Chatbot’s settings, just like Snippet Vault registered functions.

add_filter( 'mwai_functions_list', function ( $functions ) {
  $functions[] = define_userInfo();
  $functions[] = define_sendEmail();
  return $functions;
}, 10, 1 );

To use this filter, you will need to have your function defined. Let’s have a look at how to define functions.

You can define your functions using Meow_MWAI_Query_Function::fromJson():

 return Meow_MWAI_Query_Function::fromJson( [
      'id' => 'userInfo',
      'type' => 'manual',
      'name' => 'getCurrentUserInfo',
      'desc' => 'Get the current user information.',
      'args' => [
        [
          'name' => 'license',
          'type' => 'string',
          'required' => true,
          'desc' => 'User serial key.',
        ],
      ],
    ] );

Method 2: Force-add Functions to the Query

This method adds functions directly to the query, but won't work with OpenAI Assistants, since they need to be added to the Assistants before use.

add_filter( 'mwai_ai_query', function ( $query ) {
  $query->add_function( define_userInfo() );
  $query->add_function( define_sendEmail() );
  return $query;
}, 10, 1 );

Part 2: Handling Function Calls

Once functions are declared, you need to implement how they're called. There are two approaches, depending on how you declared you functions. If you declared them with Method 1 you need handle them with Method 1 as well.

Method 1: Provide Feedback to the AI Model

This method allows the AI model to process the function's output and incorporate it into the final answer.

add_filter( 'mwai_ai_feedback', function ( $value, $needFeedback ) {
  $function = $needFeedback['function'];
  if ( $function->id === 'userInfo' ) {
    return call_userInfo();
  }
  else if ( $function->id === 'sendEmail' ) {
    $subject = $needFeedback['arguments']['subject'];
    $message = $needFeedback['arguments']['message'];
    return call_sendEmail( $subject, $message );
  }
  return $value;
}, 10, 2 );

Method 2: Override the AI Reply

This method allows you to completely override the AI's reply based on the function's output.

add_filter( 'mwai_ai_reply', function ( $reply, $query ) {
  foreach ( $reply->needFeedbacks as $index => $needFeedback ) {
    $function = $needFeedback['function'];
    if ( $function->id === 'userInfo' ) {
      $value = call_userInfo();
      if ( !empty( $value ) ) {
        $reply->result = "Here is your data: " . $value;
        unset( $reply->needFeedbacks[$index] );
        return $reply;
      }
    }
  }
  return $reply;
}, 10, 2 );

Implementation Notes

  • You can add this code to your theme's functions.php file or use a snippet tool like Snippet Vault.
  • When declaring functions, use Meow_MWAI_Query_Function::fromJson() to define the function's properties.
  • For functions with arguments, define them in the args array of the function definition.
  • Choose either Method 1 or Method 2 for both declaring and handling function calls.
Did this answer your question?
😞
😐
🤩