Ai function calling not working.
-
I’m trying to use a custom WooCommerce function with AI Engine so the chatbot can search products by name and price. I first added this function using Code Engine, but it did not appear in the Functions tab in the chatbot configuration.
function getProductsForAi($name, $min_price, $max_price, $limit) { $meta_query = []; if ($min_price !== null) { $meta_query[] = [ 'key' => '_price', 'value' => (float) $min_price, 'compare' => '>=', 'type' => 'NUMERIC', ]; } if ($max_price !== null) { $meta_query[] = [ 'key' => '_price', 'value' => (float) $max_price, 'compare' => '<=', 'type' => 'NUMERIC', ]; } $query = new WP_Query([ 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => intval($limit), 's' => sanitize_text_field($name), 'meta_query' => $meta_query, ]); $products = []; if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); $product = wc_get_product(get_the_ID()); if (!$product) continue; $products[] = [ 'name' => $product->get_name(), 'price' => wc_get_price_to_display($product), 'url' => get_permalink($product->get_id()), 'stock' => $product->is_in_stock(), ]; } wp_reset_postdata(); } return [ 'count' => count($products), 'products' => $products, ]; }Then I registered it manually like this, and after that it does appear in the chatbot Functions config and can be enabled:
function define_getProductsForAi() { $define = [ 'id' => 'get_products_for_ai', 'type' => 'manual', 'name' => 'getProductsForAi', 'description' => 'Search WooCommerce products by name and price range when user asks for help.', 'args' => [ [ 'name' => 'name', 'type' => 'string', 'required' => true, ], [ 'name' => 'min_price', 'type' => 'number', 'required' => false, ], [ 'name' => 'max_price', 'type' => 'number', 'required' => false, ], [ 'name' => 'limit', 'type' => 'number', 'required' => false, ], ], ]; return Meow_MWAI_Query_Function::fromJson($define); } add_filter('mwai_functions_list', function ($functions) { $functions[] = define_getProductsForAi(); return $functions; }, 10, 1);And I handle the execution using:
add_filter('mwai_ai_feedback', function ($value, $needFeedback) { $function = $needFeedback['function']; if ($function->id === 'get_products_for_ai') { $name = $needFeedback['arguments']['name'] ?? ''; $min_price = $needFeedback['arguments']['min_price'] ?? 0; $max_price = $needFeedback['arguments']['max_price'] ?? 0; $limit = $needFeedback['arguments']['limit'] ?? 0; return getProductsForAi($name, $min_price, $max_price, $limit); } return $value; }, 10, 2);The problem is that although the function is visible and enabled in the chatbot config, the AI never calls it. Even when the user clearly asks to find products by name or price range,
mwai_ai_feedbackis never triggered and the function is ignored. There are no PHP errors.Is there anything else required to make the AI actually decide to call a custom function (prompt instruction, strict JSON output, return schema, or a different hook)?
You must be logged in to reply to this topic.