AWS Machine Learning Blog
Building an NLU-powered search application with Amazon SageMaker and the Amazon OpenSearch Service KNN feature
September 8, 2021: Amazon Elasticsearch Service has been renamed to Amazon OpenSearch Service. See details. This post has also been updated with changes required for SageMaker SDK v2 and an improved notebook experience.
The rise of semantic search engines has made ecommerce and retail businesses search easier for its consumers. Search engines powered by natural language understanding (NLU) allow you to speak or type into a device using your preferred conversational language rather than finding the right keywords for fetching the best results. You can query using words or sentences in your native language, leaving it to the search engine to deliver the best results.
Amazon SageMaker is a fully managed service that provides every developer and data scientist with the ability to build, train, and deploy machine learning (ML) models quickly. Amazon OpenSearch Service is a fully managed service that makes it easy for you to deploy, secure, and run Elasticsearch cost-effectively at scale. Amazon ES offers KNN search, which can enhance search in use cases such as product recommendations, fraud detection, and image, video, and some specific semantic scenarios like document and query similarity. Alternatively, you can also choose Amazon Kendra, a highly accurate and easy to use enterprise search service that’s powered by machine learning, with no machine learning experience required. In this post, we explain how you can implement an NLU-based product search for certain types of applications using Amazon SageMaker and the Amazon ES k-nearest neighbor (KNN) feature.
In the post Building a visual search application with Amazon SageMaker and Amazon ES, we shared how to build a visual search application using Amazon SageMaker and the Amazon ES KNN’s Euclidean distance metric. Amazon ES now supports open-source Elasticsearch version 7.7 and includes the cosine similarity metric for KNN indexes. Cosine similarity measures the cosine of the angle between two vectors in the same direction, where a smaller cosine angle denotes higher similarity between the vectors. With cosine similarity, you can measure the orientation between two vectors, which makes it the ideal choice for some specific semantic search applications. The highly distributed architecture of Amazon ES enables you to implement an enterprise-grade search engine with enhanced KNN ranking, with high recall and performance.
In this post, you build a very simple search application that demonstrates the potential of using KNN with Amazon ES compared to the traditional Amazon ES ranking method, including a web application for testing the KNN-based search queries in your browser. The application also compares the search results with Elasticsearch match queries to demonstrate the difference between KNN search and full-text search.
Overview of solution
Regular Elasticsearch text-matching search is useful when you want to do text-based search, but KNN-based search is a more natural way to search for something. For example, when you search for a wedding dress using KNN-based search application, it gives you similar results if you type “wedding dress” or “marriage dress.” Implementing this KNN-based search application consists of two phases:
- KNN reference index – In this phase, you pass a set of corpus documents through a deep learning model to extract their features, or embeddings. Text embeddings are a numerical representation of the corpus. You save those features into a KNN index on Amazon ES. The concept underpinning KNN is that similar data points exist in close proximity in the vector space. As an example, “summer dress” and “summer flowery dress” are both similar, so these text embeddings are collocated, as opposed to “summer dress” vs. “wedding dress.”
- KNN index query – This is the inference phase of the application. In this phase, you submit a text search query through the deep learning model to extract the features. Then, you use those embeddings to query the reference KNN index. The KNN index returns similar text embeddings from the KNN vector space. For example, if you pass a feature vector of “marriage dress” text, it returns “wedding dress” embeddings as a similar item.
Next, let’s take a closer look at each phase in detail, with the associated AWS architecture.
KNN reference index creation
For this use case, you use dress images and their visual descriptions from the Feidegger dataset. This dataset is a multi-modal corpus that focuses specifically on the domain of fashion items and their visual descriptions in German. The dataset was created as part of ongoing research at Zalando into text-image multi-modality in the area of fashion.
In this step, you translate each dress description from German to English using Amazon Translate. From each English description, you extract the feature vector, which is an n-dimensional vector of numerical features that represent the dress. You use a pre-trained BERT HuggingFace model hosted in Amazon SageMaker to extract 768 feature vectors of each visual description of the dress, and store them as a KNN index in an Amazon OpenSearch Service domain.
The following screenshot illustrates the workflow for creating the KNN index.
The process includes the following steps:
- Users interact with a Jupyter notebook on an Amazon SageMaker notebook instance. An Amazon SageMaker notebook instance is an ML compute instance running the Jupyter Notebook app. Amazon SageMaker manages creating the instance and related resources.
- Each item description, originally open-sourced in German, is translated to English using Amazon Translate.
- A pre-trained BERT HuggingFace model is downloaded, and the model artifact is serialized and stored in Amazon Simple Storage Service(Amazon S3). The model is used to serve from a PyTorch model server on an Amazon SageMaker real-time endpoint.
- Translated descriptions are pushed through the SageMaker endpoint to extract fixed-length features (embeddings).
- The notebook code writes the text embeddings to the KNN index along with product Amazon S3 URI in an Amazon ES domain.
KNN search from a query text
In this step, you present a search query text string from the application, which passes through the Amazon SageMaker hosted model to extract 768 features. You use these features to query the KNN index in Amazon OpenSearch Service. KNN for Amazon OpenSearch Service lets you search for points in a vector space and find the nearest neighbors for those points by cosine similarity (the default is Euclidean distance). When it finds the nearest neighbors vectors (for example, k = 3 nearest neighbors) for a given query text, it returns the associated Amazon S3 images to the application. The following diagram illustrates the KNN search full-stack application architecture.
The process includes the following steps:
- The end-user accesses the web application from their browser or mobile device.
- A user-provided search query string is sent to Amazon API Gateway and AWS Lambda.
- The Lambda function invokes the Amazon SageMaker real-time endpoint, and the model returns a vector of the search query embeddings. Amazon SageMaker hosting provides a managed HTTPS endpoint for predictions and automatically scales to the performance needed for your application using Application Auto Scaling.
- The function passes the search query embedding vector as the search value for a KNN search in the index in the Amazon ES domain. A list of k similar items and their respective Amazon S3 URIs are returned.
- The function generates pre-signed Amazon S3 URLs to return back to the client web application, used to display similar items in the browser.
For this walkthrough, you should have an AWS account with appropriate AWS Identity and Access Management (IAM) permissions to launch the AWS CloudFormation template.
Deploying your solution
You use a CloudFormation stack to deploy the solution. The stack creates all the necessary resources, including the following:
- An Amazon SageMaker notebook instance to run Python code in a Jupyter notebook
- An IAM role associated with the notebook instance
- An Amazon ES domain to store and retrieve sentence embedding vectors into a KNN index
- Two S3 buckets: one for storing the source fashion images and another for hosting a static website
From the Jupyter notebook, you also deploy the following:
- An Amazon SageMaker endpoint for getting fixed-length sentence embedding vectors in real time.
- An AWS Serverless Application Model (AWS SAM) template for a serverless backend using API Gateway and Lambda.
To get started, complete the following steps:
- Sign in to the AWS Management Console with your IAM user name and password.
- Choose Launch Stack and open it in a new tab:
- On the Quick create stack page, select the check-box to acknowledge the creation of IAM resources.
- Choose Create stack.
- Wait for the stack to complete.
You can examine various events from the stack creation process on the Events tab. When the stack creation is complete, you see the status CREATE_COMPLETE.
You can look on the Resources tab to see all the resources the CloudFormation template created.
- On the Outputs tab, choose the SageMakerNotebookURL
This hyperlink opens the Jupyter notebook on your Amazon SageMaker notebook instance that you use to complete the rest of the lab.
You should be on the Jupyter notebook landing page.
- Choose nlu-based-item-search.ipynb.
Building a KNN index on Amazon ES
For this step, you should be at the beginning of the notebook with the title NLU based Item Search. Follow the steps in the notebook and run each cell in order.
You use a pre-trained BERT model (distilbert-base-nli-stsb-mean-tokens) from HuggingFace and host it on an Amazon SageMaker PyTorch model server endpoint to generate fixed-length sentence embeddings. The embeddings are saved to the Amazon OpenSearch Service domain created in the CloudFormation stack. For more information, see the markdown cells in the notebook.
Continue when you reach the cell
Deploying a full-stack NLU search application in your notebook.
The notebook contains several important cells; we walk you through a few of them.
Download the multi-modal corpus dataset from Feidegger, which contains fashion images and descriptions in German. See the following code:
Upload the dataset to Amazon S3:
This dataset has product descriptions in German, so you use Amazon Translate for the English translation for each German sentence:
Save the sentence transformers model to notebook instance:
Upload the model artifact (model.tar.gz) to Amazon S3 with the following code:
Deploy the model into an Amazon SageMaker PyTorch model server using the Amazon SageMaker Python SDK. See the following code:
Define a cosine similarity Amazon OpenSearch KNN index mapping with the following code (to define cosine similarity KNN index mapping, you need Amazon OpenSearch Service 7.7 and above):
Each product has five visual descriptions, so you combine all five descriptions and get one fixed-length sentence embedding. See the following code:
Import the sentence embeddings and associated Amazon S3 image URI into the Amazon OpenSearch Service KNN index with the following code. You also load the translated descriptions in full text, so that later you can compare the difference between KNN search and standard match text queries in Amazon OpenSearch Service.
Building a full-stack KNN search application
Now that you have a working Amazon SageMaker endpoint for extracting text features and a KNN index on Amazon OpenSearch Service, you’re ready to build a real-world, full-stack ML-powered web app. You use an AWS SAM template to deploy a serverless REST API with API Gateway and Lambda. The REST API accepts new search strings, generates the embeddings, and returns similar relevant items to the client. Then you upload a front-end website that interacts with your new REST API to Amazon S3. The front-end code uses Amplify to integrate with your REST API.
- In the following cell, prepopulate a CloudFormation template that creates necessary resources such as Lambda and API Gateway for full-stack application:
The following screenshot shows the output: a pre-generated CloudFormation template link.
- Choose the link.
You are sent to the Quick create stack page.
- Select the check-boxes to acknowledge the creation of IAM resources, IAM resources with custom names, and CAPABILITY_AUTO_EXPAND.
- Choose Create stack.
When the stack creation is complete, you see the status CREATE_COMPLETE. You can look on the Resources tab to see all the resources the CloudFormation template created.
- After the stack is created, proceed through the cells.
The following cell indicates that your full-stack application, including front-end and backend code, are successfully deployed:
The following screenshot shows the URL output.
- Choose the link.
You are sent to the application page, where you can provide your own search text to find products using both the KNN approach and regular full-text search approaches.
- When you’re done testing and experimenting with your KNN search application, run the last two cells at the bottom of the notebook:
These cells end your Amazon SageMaker endpoint and empty your S3 buckets to prepare you for cleaning up your resources.
To delete the rest of your AWS resources, go to the AWS CloudFormation console and delete the nlu-search-api and nlu-search stacks.
In this post, we showed you how to create a KNN-based search application using Amazon SageMaker and Amazon OpenSearch Service KNN index features. You used a pre-trained BERT model from the HuggingFace Model Hub. You used a pre-trained BERT model from the sentence-transformers Python library. You can also fine-tune your BERT model using your own dataset. For more information, see Fine-tuning a PyTorch BERT model and deploying it with Amazon Elastic Inference on Amazon SageMaker.
For more information about the code sample in the post, see the GitHub repo.
About the Authors
Amit Mukherjee is a Sr. Partner Solutions Architect with a focus on data analytics and AI/ML. He works with AWS partners and customers to provide them with architectural guidance for building highly secure and scalable data analytics platforms and adopting machine learning at a large scale.
Laith Al-Saadoon is a Principal Solutions Architect with a focus on data analytics at AWS. He spends his days obsessing over designing customer architectures to process enormous amounts of data at scale. In his free time, he follows the latest in machine learning and artificial intelligence.
Eitan Sela is a Machine Learning Specialist Solutions Architect with Amazon Web Services. He works with AWS customers to provide guidance and technical assistance, helping them build and operate machine learning solutions on AWS. In his spare time, Eitan enjoys jogging and reading the latest machine learning articles.