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

You can use Snippet Vault for this task, or edit your functions.php file. You'll need to register your functions via mwai_ai_query, then execute them using mwai_ai_feedback.

// Register the functions
add_filter( 'mwai_ai_query', function ( $query ) {
  $query->add_function( new Meow_MWAI_Query_Function( 'current_user_interactive', 'Get the current user information.', [] ) );
  $paramSubject = new Meow_MWAI_Query_Parameter( 'subject', 'The subject of the email', 'string', true );
  $paramMessage = new Meow_MWAI_Query_Parameter( 'message', 'The message of the email', 'string', true );
  $query->add_function( new Meow_MWAI_Query_Function( 'send_email', 'Send an email to the admin.',
    [ $paramSubject, $paramMessage ]
  ) );
  return $query;
}, 999, 1 );

// Give feedback to AI Engine when needed
add_filter( 'mwai_ai_feedback', function ( $value, $needFeedback ) {
  $name = $needFeedback['name'];
  if ( $name === 'send_email' ) {
    $subject = $needFeedback['arguments']['subject'];
    $message = $needFeedback['arguments']['message'];
    mail( "neko@shrine.com", $subject, $message );
    return "Email sent.";
  }
  else if ( $name === 'current_user_interactive' ) {
    $current_user = wp_get_current_user();
    if ( $current_user->exists() ) {
      return json_encode( [
        'id' => $current_user->ID,
        'name' => $current_user->display_name,
        'email' => $current_user->user_email,
      ] );
    }
  }
  return $value;
}, 10, 2 );

Static Functions

This type of functions will directly run a function on your end, and the output of that function will be the final output.

Snippet Vault

The feature is not yet available in Snippet Vault; all functions within Snippet Vault + AI Engine are, by default, interactive.

Manually

Similar to working with interactive functions, you'll need to modify the $query to include the necessary functions, ensuring the AI model recognizes them. Afterwards, you should override the $reply, evaluate if feedback is required, and directly return the results from your function, bypassing further AI model intervention by clearing the needFeedbacks.

Think of this as a temporary workaround; eventually, the AI Engine will inherently support static functions too.

// Register the functions
add_filter( 'mwai_ai_query', function ( $query ) {
  $query->add_function( new Meow_MWAI_Query_Function( 'current_user', 'Get the current user information.', [] ) );
  $query->add_function( new Meow_MWAI_Query_Function( 'current_time', 'Get the current time.', [] ) );
  return $query;
}, 999, 1 );

// Override the reply if the function is called
add_filter( 'mwai_ai_reply', function ( $reply, $query ) {  
  foreach ( $reply->needFeedbacks as $index => $needFeedback ) {
    switch ( $needFeedback['name'] ) {
      case 'current_user':
        // Retrieve current user information and format it for the reply.
        $current_user = wp_get_current_user();
        if ( $current_user->exists() ) {
          $reply->result = "Your email is " . $current_user->user_email . " and your name is " . $current_user->display_name . ".";
          // This will break the feedback loop in AI Engine and return the result directly instead.
          unset( $reply->needFeedbacks[$index] );
          return $reply;
        }
        break;
      case 'current_time':
        // Directly return the current time if the function 'current_time' is called.
        $reply->result = "Current time is " . date('Y-m-d H:i:s') . ".";
        unset( $reply->needFeedbacks[$index] );
        return $reply;
    }
  }
  return $reply;
}, 10, 2 );
 
Did this answer your question?
😞
😐
🤩