Configuring AI-related features in ElasticPress Labs

ElasticPress Labs 2.5.0 introduced three new experimental features related to Artificial Intelligence. This release serves as a foundational framework for integrating AI capabilities into ElasticPress. We will continue to improve and expand upon this feature set. In this article, we go over the process of setting them up.

What you will need:

  • Elasticsearch version 7 or later. All ElasticPress.io servers are running versions more recent than that.
  • OpenAI API (or compatible) credentials.
    • If you are running and hosting your own LLM, make sure it is behind an OpenAI compatibility layer like LiteLLM or similar.

NOTE: To avoid any unexpected costs, make sure you set up limits on your API credentials.

Setting up the AI-related features

A full sync will be required!

A new field needs to be added in Elasticsearch, so a full sync will be required. As the Vector Embeddings generation process takes some time, we recommend you:

  1. Reduce the number of posts sent by sync cycle. In your WordPress Dashboard, go to ElasticPress > Settings and change the Content Items per Index Cycle value to a low number, such as 30.
  2. Preferably run your sync using WP-CLI. Although not required, running your sync using WP-CLI will be faster than running it via the Dashboard.

Vector Embeddings

To support any sort of semantic / natural language search, you need to store a vector representation of your post in Elasticsearch. Through those vectors, Elasticsearch knows that shoes is related to high heels, even if their characters are completely different. The process of getting a post’s content and generating its vector embeddings is done by artificial intelligence.

The Vector Embeddings feature is responsible for sending the post to AI, getting back the vector embeddings, and storing them in Elasticsearch. To do so, you need to set up your OpenAI credentials:

If you have access to WP-CLI, click on Save and sync later. Then, run wp elasticpress sync --setup --show-errors on your server to apply the changes. If you do not have WP-CLI access, click on Save and sync now and wait until the process is over.

In your WP Dashboard, going to ElasticPress > Vector Embeddings will take you to a screen where you can enable and disable post types, vectorize posts with certain terms or post meta, and also configure which fields should be vectorized.

Natural language Search / Semantic Search

Now that Elasticsearch has vector representations of your post, you can set up Semantic Search, our natural language feature.

Start by enabling the Semantic Search feature and clicking on Save changes. Reload the page, then go to Other > Search Algorithm Version. Enable this feature and select one of the newly added search algorithms: kNN, kNN Cosine, and Hybrid (kNN + Regular ES).

As each site is different, you will have to check which search algorithm works best for your content. Changes to these two features do not require a sync.

AI Search Summary

To generate answers to your users’ questions based on your site’s content, enable the AI Search Summary feature. This feature gets the user’s search terms/question, generates a vector representation of it, finds content related to it in your site, and generates an answer.

As you can imagine, the process of “reading” the content found and generating an answer requires artificial intelligence. It is a process totally different from generating vectors, so the endpoint we are going to use will be different from the one we used on the Vector Embeddings feature.

Setting up this feature enables a new block called AI Search Summary. Adding it to your search page will display AI-generated answers to your users.

Implementing AI Security Guardrails for ElasticPress Labs

When implementing AI-powered features like AI Search summaries, proper security guardrails and contextual grounding are essential to ensure safe, accurate, and reliable AI responses. The AISearchSummary.php class provides several hooks and filters that enable integration with Amazon Bedrock Guardrails or similar solutions to mitigate potential risks.

Security Considerations for AI Search Summaries

AI-generated search summaries can present several potential risks, including:

  • Hallucinations (generating incorrect information not present in source content)
  • Inappropriate content generation
  • Prompt injection attacks
  • Data privacy concerns
  • Response bias or misrepresentation of content

The ElasticPress Labs framework has thoughtfully included hooks that allow developers to implement guardrails at critical points in the AI processing pipeline:

Available Hooks for AI Security Implementation

Here are some of the hooks that could be leveraged for implementing AWS Bedrock Guardrails or similar approaches:

By leveraging these and other hooks and filters provided by the AISearchSummary.php class, you can implement comprehensive security measures through existing solutions or custom code and alter the output and behavior of this feature to their needs. This approach ensures that AI-generated search summaries remain relevant, accurate, and safe while protecting against common AI risks like hallucinations, inappropriate content, and potential abuse.

Troubleshooting

If things do not work as expected, there are a couple of things you will want to check. Most of them require our Debugging Add-On.

Vectors are generated and stored

Using the debugging add-on, visit a post, open the debugging panel, and click on Reload and retrieve raw ES document. When the page reloads, open the debugging panel, find the Raw ES document query, open the Query Result, and check for the chunks field. If you see a list of numbers, it worked. If you don’t, there is something wrong with your vector generation.

Some options to debug are:

  • Go to ElasticPress > Status Report and check any failed queries;
  • Run a WP-CLI sync with a single post (wp elasticpress sync --include=<post_id>) and check for any errors;
  • Go to ElasticPress > Query Log, and set it for all failed queries. Save a post and check the Query Log screen again.

No search results found

If you don’t see any search results using the semantic search algorithms, first make sure vectors are properly generated. If they are, try to lower the Minimum score number in the Semantic Search features options.

It is also possible to use our debugging add-on to make sure the search algorithm is being used.

No search summary is being displayed

If the AI Search Summary block does not generate an answer, make sure the AJAX call is being fired by looking at your browser developer tools (generally under the Network tab).

If it is, you can go to ElasticPress > Query Log and set it to store failed REST API requests. Search for some content again and inspect the query stored in the Query Log page.