AI Engine Modify Embedding Query

Here’s a snippet that lets you modify the text sent to Pinecone when using embeddings. This is a proof of concept and a starting point for customizing the way context is retrieved for your chatbots.

class EmbeddingsExtension {
    private $maxContextLength;
    private $botId;
    private $embeddingsName;
    private $addString;
    public function __construct($botId, $embeddingsName, $maxContextLength, $addString) {
        $this->botId = $botId;
        $this->embeddingsName = $embeddingsName;
        $this->maxContextLength = $maxContextLength;
        $this->addString = $addString;
        add_filter('mwai_ai_query', array($this, 'mwai_ai_query'), 99999, 1);
    }
    public function get_id($query) {
		$postData = file_get_contents('php://input');
		$decodedData = json_decode($postData, true);
		$customId = (isset($decodedData['customId']) && !empty($decodedData['customId'])) ? $decodedData['customId'] : null;
		$botId = $customId ? $attr['custom_id'] : (isset($query->botId) ? $query->botId : 'default');
		return $botId;
	}
	public function get_embedding_id($name) {
		global $mwai_core;
		$envs = $mwai_core->get_option( 'embeddings_envs' );
		foreach ( $envs as $env ) {
		  if ( $env['name'] === $name ) {
			return $env['id'];
		  }
		}
		return null;
	}
    public function mwai_ai_query($query) {
        if (!($query instanceof Meow_MWAI_Query_Text) && !($query instanceof Meow_MWAI_Query_Assistant)) { return $query; }
        if ($this->botId == $this->get_id($query)) {
            $embeddingId = $this->get_embedding_id($this->embeddingsName);
            if (!$embeddingId) {
                throw new Exception('Embeddings environment not found');
            }
            $params = array('embeddingsEnvId' => $embeddingId, 'contextMaxLength' => round($this->maxContextLength));
            $contextQuery = new Meow_MWAI_Query_Text( $query->message . '\n' . $this->addString );
            global $mwai_core;
            $context = $mwai_core->retrieve_context( $params, $contextQuery );
            if ( !empty( $context ) ) {
                $query->context = trim($query->context . "\n" . $context['content']);
            }
        }
        return $query;
    }
}

// EDITABLE

$botId = 'chatbot-embeddings-test';
$embeddingsName = 'MyPineconeEnvironment';
$maxContextLength = 50000;
$addString = 'Extra Text To Add To Embedding Query Here';

// END EDITABLE

$embeddingAddon = new EmbeddingsExtension($botId, $embeddingsName, $maxContextLength, $addString);

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

Contact me.

Leave a Reply

Your email address will not be published. Required fields are marked *