I recently had trouble with AI (chiefly GPT4o-mini) making up the results of function calls.
After running so many of them that the conversation began to overshadow its own instructions, it would start creating products out of thin air.
It happens because the AI sees its outputs, but not the function calls it made, so it starts to mimic the outputs rather than continue running tools.
The solution was simple and a little clever if I say so myself!
function add_hallucination_warning($query, $pre=3) { // Get the messages from the query, or initialize as an empty array if not set $messages = $query->messages ?? array(); // If there are no messages or the number of messages is less than the pre value, return the query as is if (empty($messages) || count($messages) < $pre) { return $query; } $warning_messages = array( array('role' => 'user', 'content' => 'I want to kindly remind you to please use the search_products and get_product functions to find products instead of making them up.'), array('role' => 'assistant', 'content' => 'Thank you for the reminder. I will use the search_products and get_product functions to find products.') ); // Calculate the index of the message to check based on the pre value $index = count($messages) - $pre; // If the message at the calculated index has the role 'assistant', add warning messages if ($messages[$index]['role'] === 'assistant') { // Merge the warning messages into the original messages array at the correct position $messages = array_merge( array_slice($messages, 0, $index + 1), $warning_messages, array_slice($messages, $index + 1) ); // Update the query's messages with the modified messages array $query->messages = $messages; } return $query; } add_filter('mwai_ai_query', function($query) { $query = add_hallucination_warning($query,3); return $query; }, 10, 1);
This code adds a reminder within the message history to look as if the user and assistant had a quick back-and-forth about function call usage.
With a bit of tweaking to match your function names, you too can guide your AI to making accurate function calls!
Note: This does not work for OpenAI Assistants.
Let’s enhance your business! I can help you develop custom AI engine extensions or broader AI strategies.
Contact me.
Leave a Reply