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.
For demonstration purposes we will be using the Yahuu Finance API.
How about we explore the function calling in this PHP code?
Updated for the latest version of AI Engine!
// 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( "[%s](%s)\n![Image](%s)\n\n", $title, $link, $thumbnailUrl ); } } 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( 'get_company_news', '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 = 'https://yahuu-finance.p.rapidapi.com/news'; 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-Host: yahuu-finance.p.rapidapi.com", "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; unset($reply->needFeedbacks[$index]); 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.