AI Engine Conditional Forms

Tired of static AI prompts? This quick snippet empowers you to create dynamic forms that send tailored prompts to AI Engine, resulting in more relevant and engaging responses.

Templating Basics:

  • Use curly braces {} for variables (e.g., {SUBJECT})
  • Conditional logic: $[if KEYWORDS: "including the following keywords: {KEYWORDS}." else: "include lots of cats."]

How it Works:

  1. Website admins set up an input box for custom prompts on AI-powered forms.
  2. The code uses a simple templating system to insert form field values.
  3. Conditional logic (if-else) tailors the prompts sent to AI Engine.

Example:

An email writing form with fields for “SUBJECT” and “KEYWORDS” could use a prompt like this:

An email on {SUBJECT} and $[if KEYWORDS: "including the following keywords: {KEYWORDS}." else: "include lots of cats."]

.. or if you’d like to omit an else statement:

An email on {SUBJECT} $[if KEYWORDS: "and including the following keywords: {KEYWORDS}."]

Get Creative!

Experiment to build dynamic questionnaires, personalized content requests, and much more.

Installation

Simply add the following PHP snippet:

add_filter('mwai_ai_query', function ($query) {
    if (!($query instanceof Meow_MWAI_Query_Text) || $query->scope != 'form' || !isset($query->extraParams) || !isset($query->extraParams['fields'])) {
        return $query;
    }

    $data = $query->extraParams['fields'];
    $message = $query->message;
	
    $pattern = '/\$\[if (\w+): "([^"]*)" *(?:else: "([^"]*)")?\]/';

    // Define the callback function for conditional replacement
    $callback = function ($matches) use ($data) {
        $key = $matches[1];
        $trueText = $matches[2];  // Text to use if condition is true
        $falseText = isset($matches[3]) ? $matches[3] : '';  // Text to use if condition is false or not set

        if (isset($data[$key]) && trim($data[$key]) !== '') {
            return $trueText; // If the key exists and is not empty, use the true text
        } else {
            return $falseText; // Otherwise, use the false text or an empty string if 'else' part is not provided
        }
    };

    // Apply the regular expression and callback to the message
    $message = preg_replace_callback($pattern, $callback, $message);

    $query->message = $message;

    return $query;
}, 10, 1);

Let’s enhance your business! I can help you develop custom AI engine extensions or broader AI strategies.

Contact me.