I would like to make AI Engine extremely customizable so that you can achieve really everything you want with it. Here are the current available filters in the plugin.
Filters
Interactions with AI
- mwai_ai_query( $query ) => Modify a query before it’s made
- mwai_ai_allowed( $allowed, $query, $limits ) => Allow a query to be made or not
- mwai_ai_reply( $answer, $query ) => Modify the answer before it’s sent (or do something at that time)
- mwai_ai_exception( $message ) => Modify the error message
- mwai_estimate_tokens( $tokens, $text ) => Modify the estimated number of tokens for the given text (you can modify the default algorithm) used to calculate the tokens usage estimation.
Chatbot
Chatbot System
- mwai_chatbot_params_defaults( $defaults_params ) => Modify the default params
- mwai_chatbot_params_before( $params ) => Modify the params before the params of the shortcode are used
- mwai_chatbot_params( $params ) => Modify the final params just before the query is made
- mwai_chatbot_style( $style, $id ) => Modify the style of the chatbot
- mwai_chatbot( $output, $atts ) => Modify the chatbot itself (HTML, JS, CSS)
Discussion Handling
- mwai_chatbot_takeover( $takeoverText, $query, $params )
- mwai_contentaware_content( $content ) => Handle extra use cases, like looking for content in specific fields, or handling page builders, etc
- mwai_chatbot_reply( $rawText, $query, $params, $extra ) => Modify the answer before it’s sent.
Embeddings
- mwai_context_search( $query, $embeddingIndex = null ): Returns the embedding the most related to the query.
Forms
- mwai_forms_params( $params ) => Modify the final params
- mwai_form_answer( $rawText ) => Modify the answer before it’s sent
Limits
- mwai_stats_credits( $credits, $userId ) => Modify the number of credits (that allows you to create your own system)
- mwai_stats_coins( $price, $stats, $atts ) => Modify the coins (that allows you to convert the price you personally pay to OpenAI to your own system of coins that you share with your users)
Settings / UI
- mwai_languages( $languages ) => Modify the list of languages
Roles
- mwai_allow_setup( $hasAccess ) => Allow access to the Settings of AI Engine in the Admin
- mwai_allow_usage( $hasAccess ) => Allow access to the various screens of AI Engine in the Admin
API
If you would like to access AI features easily through AI Engine, simply use the mwai global. There will be more and more functions available to use. The API uses the whole AI Engine system, so you’ll get the statistics and limitations features as well.
simpleTextQuery( $prompt, $options )
You can use this function to simply run a query against the default model (turbo, currently). It’s as simple as that!
add_filter( 'the_content', function ( $content ) {
global $mwai;
$extra = $mwai->simpleTextQuery( "Write an one-liner about a cat introducing itself." );
return $extra . $content;
} );
moderationCheck( $text )
It will simply return true if the message has been flagged as unsafe to use.
Classes
Meow_MWAI_QueryText
You will find this usually named as $query and this class is used mainly for completion and chat. By default, it uses the turbo model, and relatively safe parameters. But of course, you can override everything you like. Here is an example on how to use it manually:
$query = new Meow_MWAI_QueryText( "Translate 'Hello World' into French.", 512 );
$query->setModel( 'text-davinci-003' ); // Override the model (turbo by default)
// AI Engine will take care of this for you.
global $mwai_core;
$answer = $mwai_core->ai->run( $query );
// And finally, let's output the result.
echo $answer->getResult();
There are the main functions:
- $query->replace( $search, $replace ): Replace a string by another string (in will affect only the prompt)
- $query->setApiKey( $apiKey ): Override the default API Key set in the settings.
- $query->setPrompt( $prompt ): Override the prompt.
- $query->setContext( $context ): Override the context.
- $query->setModel( $model ): Override the model (gpt-3.5-turbo by default).
- $query->setTemperature( $temperature ): Override the temperature (0.8 by default).
- $query->setMaxTokens( $maxTokens ): Override the max tokens (1024 by default). It will be automatically be reduced by AI Engine if it’s too high.
- $query->setMaxResults( $maxResults ): Specific the number of results (1 by default).
- $query->injectContext( $context ): Add an additional context that will only affect this query (this works only with the chat mode with turbo).
- $answer->estimateTokens( $text ): Give an estimation of the number of tokens for this text. Very approximate, don’t make your life depend on it.
Meow_MWAI_Answer
You will find it usually named as $answer. Here are the main functions:
- $answer->getResults(): Return an array with all the results.
- $answer->getResult(): Return simply the first (or the only) result.
- $answer->replace( $search, $replace ): Replace a string by another string (in will affect all the results)
- $answer->getUsage(): Return an array with the number of prompts_tokens, completion_tokens, and total_tokens. But I think I’ll modify this, to show the costs directly instead with more interesting statistics (like what the plugin does normally, in the Statistics module).