Switch Model At Request Time

 

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 utilize the Vision model only when necessary. 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-4-vision-preview' );
    } else {
        $query->set_model( 'gpt-4-1106-preview' );
    }

    // Log the chosen model to error log for debugging purposes
    error_log("🗒️: " . print_r( $query->model, true ) );
  
    return $query;
}, 10, 2 );
Did this answer your question?
😞
😐
🤩