Add A Context Sentences For Non Required Fields

If you are using an optional field, and you've incorporated it into your prompt using a template like "This is the user’s name: {USER_NAME}", it may not make sense to include this part in the prompt if the field is left empty. In such cases, it will result in the prompt reading as: "This is the user’s name: ". To avoid this, consider the following example:

 
Notion image
 
add_filter( 'mwai_ai_query', function ( $query ) {

    $message = $query->get_message();
  
    if ( str_contains( $message, '[NOT_R_CONTEXT]' ) ) {
      $query->replace( '[NOT_R_CONTEXT]', '' );
    }
    
    if ( str_contains ($message, '[NOT_R_CONTEXT' ) ) {
      $query->replace( '[NOT_R_CONTEXT', '[This is the user\'s name: ' );
    }
  
    return $query;
  } );
 

Prompt when {USER_NAME} is filled:

Describe cats in one sentence. [This is the user’s name: Nyao]

Prompt when {USER_NAME} isn’t filled:

Describe cats in one sentence.
Did this answer your question?
😞
😐
🤩