3 min read 1 wk ago

Can I restrict what the chatbot talks about?

Unfortunately, you cannot inhibit the AI model from accessing its pre-trained knowledge, as it forms the foundation of its capabilities. So you cannot have a chatbot that relies ONLY on your website content and nothing else.

Hence, we recommend implementing “smart prompts” and using Embeddings to restrain your chatbot’s topical scope. This method is often referred to as ‘prompt engineering’. The key lies in framing the chatbot’s context as precise and lucid as possible, thereby concentrating on a specific subject matter. Do not hesitate to script prompts like, “If this query isn’t related to our business, reply with: ‘I cannot assist with this matter.” Although this might not completely limit the topics, it provides greater control over the chatbot’s responses. Fine-tune and experiment until you find the most effective solution.

Here is an example:

The AI responses are based on its context. If the context is empty, it will reply using only its default knowledge. So make sure you are actually feeding the right data into your chatbot’s context for it to generate meaningful responses. This context can come from various sources — you can learn more by reading the documentation.

If you have technical knowledge, you can use a filter like mwai_context_search to manually check if any content (from embeddings or other sources) was added to the context. If the context is empty, you can force the chatbot to reply with a message indicating that it doesn’t have the necessary information to answer the query.

Note that this is not recommended. This is more of a hacky way to ensure Knowledge content has been added to the current context of your chatbot and then block a response if it doesn’t contain anything. Also, this does not mean the response will be accurate.

// Hook into the "mwai_context_search" filter to handle context verification.
add_filter( "mwai_context_search", 'my_context_check', 10, 3 );

// Function to handle context verification.
function my_context_check( $context, $query, $options = [] ) {
  if ( !empty( $context ) ) {
    // If context is available, let the conversation proceed naturally.
    return $context;
  } else {
    // If no context is found, set up a placeholder to indicate an empty context.
    $context["content"] = '{EMPTY}';
    $context["type"] = "filter";
    return $context;
  }

  return null;
}

// Hook into the "mwai_ai_reply" filter to modify the AI reply.
add_filter('mwai_ai_reply', 'my_mwai_reply', 10, 2);

// Function to modify the AI reply when an empty context is detected.
function my_mwai_reply( $reply, $query ){
  if ( $query instanceof Meow_MWAI_Query_Text ) {
    // Retrieve the context and check if it was our {EMPTY} keyword.
    $context = $query->context;
    if ( $context === '{EMPTY}' )
    {
      // If an empty context is detected, change the reply to a default message.
      $reply->set_reply("Sorry, I don't have enough information to answer that right now. Please try asking something else or provide more details.");
    }
  }
  return $reply;
}