AI Engine Function Calling

Powered by the OpenAI TTS plugin and the Yahuu Finance API.
Updated for the latest version of AI Engine!

This example bypasses the typical process of sending API results back to the AI model for final output processing. We achieve consistency and speed by directly formatting the API results ourselves, eliminating the need for additional AI intervention.

How about we explore the function calling in this PHP code?

// Takes JSON data and turns it into a markdown formatted response
function formatArticlesFromJson($jsonData) {
    // Decode the JSON data
    $data = json_decode($jsonData, true);

    // Initialize an empty string to hold our formatted text
    $formattedText = '';

    // Check if 'data' key exists
    if (isset($data['data']) && is_array($data['data'])) {
        // Loop through each article in the 'data' array
        foreach ($data['data'] as $article) {
            $title = $article['title'] ?? 'No Title';
            $link = $article['link'] ?? '#';
            $thumbnailUrl = $article['thumbnail']['resolutions'][0]['url'] ?? '';

            // Append the formatted string for this article to the overall text
            $formattedText .= sprintf(
    return $formattedText;

// Check for the specific chatbot and add get_company_news function calling
add_filter( 'mwai_ai_query', function ( $query ) {
    // Ensure we are modifying the correct chatbot
    if ( $query->scope == 'chatbot' && $query->botId == 'chatbot-news' ) {
        $query->add_function( new Meow_MWAI_Query_Function(
            'Query news API for latest news on a company',
                new Meow_MWAI_Query_Parameter( 'symbol', 'The company ticker symbol e.g. CNN or TWTR', 'string', true )
    return $query;
}, 10, 1 );

// Add a custom filter to modify the AI reply.
add_filter( 'mwai_ai_reply', function ( $reply, $query ) {
    // Check if the query scope and bot ID match specific conditions.
    if ( $query->scope != 'chatbot' || $query->botId != 'chatbot-news' || !isset($reply->needFeedbacks)) { 
        return $reply; // If not, return the original reply.
    foreach ( $reply->needFeedbacks as $index=>$needFeedback ) {
        // Check if the reply has a function call for 'get_company_news'.
        if ( $needFeedback['name'] === 'get_company_news' && isset($needFeedback['arguments']['symbol']) ) {
            // Convert the stock symbol to uppercase.
            $input = strtoupper($needFeedback['arguments']['symbol']);

            // Initialize cURL session.
            $curl = curl_init();
            $URL = '';
            curl_setopt($curl, CURLOPT_URL, $URL);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // Return the transfer as a string.
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); // Set request method to POST.
            // Set POST fields as JSON containing the symbol.
            curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode([
                'symbol' => $input
            curl_setopt($curl, CURLOPT_HTTPHEADER, [
                "X-RapidAPI-Key: xxxxxxxxxxxxxxxxxxxxx", ##################### Place your API key here!
                "content-type: application/json"
            // Execute cURL session and capture the response.
            $response = curl_exec($curl);
            $error = curl_error($curl); // Capture any cURL error if it occurs.
            curl_close($curl); // Close cURL session.

            // Format the API response for output.
            $replyText = formatArticlesFromJson($response);
            // Set the formatted text as the result of the reply object.
            $reply->result = $replyText;
            return $reply; // Return the modified reply.
    return $reply; // Return the original reply if no conditions are met.
}, 10, 2 ); // The filter function accepts 2 arguments and has a priority of 10.

Important: Anthropic documentation refers to ‘function calling’ as ‘tool use’. The same code works with OpenAI chatbots, where you should use the term ‘function calling’ in the chatbot instructions.

Let’s enhance your business! I can help you develop custom AI engine extensions or broader AI strategies.
Contact me.