Seamlessly integrate and switch between language and vision models using the mwai_ai_query filter. The primary goal of this guide is to demonstrate the implementation of a dynamic model-switching mechanism, allowing you to send the heavier model only when it is actually needed. Note that modern models handle images natively, so you no longer need a separate vision model; this example now switches to a faster and cheaper model when the request is a simple one. By incorporating this approach into your code, you can optimize resource usage and enhance the efficiency of your applications that leverage AI capabilities.
add_filter( 'mwai_ai_query', function ( $query ) {
// If it's an embbeding request, return the query as is
if ( $query->mode == "embedding" ) {
return $query;
}
// Check ithe type of uploaded file
$needVision = ( $query->filePurpose === 'vision' );
// Switch the model based on the need for vision capabilities
if ( $needVision ) {
$query->set_model( 'gpt-5.5' );
} else {
$query->set_model( 'gpt-5.4-mini' );
}
// Log the chosen model to error log for debugging purposes
error_log("🗒️: " . print_r( $query->model, true ) );
return $query;
}, 10, 2 );