avatarPranay Dave

Summary

The article discusses the use of large language models (LLMs) to enhance e-commerce product search by bridging the gap between customer search intent and product descriptions, potentially increasing customer satisfaction and retailer sales.

Abstract

The article titled "How to use Large-Language Models to enhance Product Search" explains that customers often struggle to find the exact products they want on e-commerce platforms due to a mismatch between their search terms and the functional descriptions of products. It highlights the limitations of traditional search algorithms that rely on similarity scores based on search terms and product descriptions. The author suggests that by leveraging LLMs, such as those used in ChatGPT, e-commerce platforms can better understand customer intent through embeddings, which are vector representations of text that capture contextual meaning. This technology can link seemingly different terms and phrases, allowing for more intuitive and accurate product searches. The article also provides a technical implementation guide, including how to obtain embeddings for product descriptions using OpenAI's API, and how to use these embeddings to find products that match the customer's search intent, even if the exact search term isn't present in the product description. The conclusion emphasizes the potential business benefits for retailers and the improved user experience for customers.

Opinions

  • The author believes that current e-commerce search algorithms, despite being powered by strong algorithms, often fail to meet customer expectations due to a lack of understanding of customer intent.
  • It is the author's opinion that embeddings from LLMs can significantly improve the search experience by capturing the contextual meaning behind search terms and product descriptions.
  • The article conveys that the use of LLMs in product search not only enhances customer experience but also opens up new business opportunities for retailers by increasing the likelihood of matching products with customer needs.
  • The author is optimistic about the potential of LLMs to recommend additional search terms, further refining the search process and aiding in customer discovery.
  • There is an emphasis on the practicality of implementing LLMs for product search, as evidenced by the detailed technical implementation provided, which includes batch processing of product descriptions and real-time search term embedding comparisons.
  • The author encourages engagement with their content by inviting readers to subscribe to their Medium posts, visit their website, and follow their YouTube channel for more insights into data science and AI applications.

How to use Large-Language Models to enhance Product Search

Retailers can delight customers and make more business

Photo by Jaclyn Moy on Unsplash

Do you get frustrated while scrolling through a list of products on an e-commerce platform? The platform You are not alone. Despite spending a lot of time scrolling-clicking-scrolling, many customers do not find what they are looking for.

Most websites are powered by powerful algorithms, so what could be wrong? Generally, the algorithms take the search term and then look for the closest match with the product description using a similarity score. Technically the algorithms work great. It takes the search term and then finds all products which have got the search term in their product description.

However what if the customer's intent is not accurately reflected in the search term? Let us take an example of a common search term “foot cream”. There are a variety of reasons why a customer would search for a foot cream such as foot health and maintenance, foot relaxation, control of foot odor, treating dry, cracked skin, foot massage, skin softening, etc.

Generally, the customer enters a search term with a final product in mind. However, most of the product descriptions have the functional description of the product.

Difference between customer thought and product description (source Photo by Mimi Thian on Unsplash, Photo by Bee Naturalles on Unsplash

So if a customer enters the search term “foot cream”, it would return all different products which have foot cream in the product description such as foot cream for skin softening, foot cream for foot massage, etc… However products such as “organic balm for cracked heels” would not be in the search result because it does not have “foot cream” in the description. If the customer is actually looking for organic balm, she would just be scrolling through all products in frustration. It also represents a huge loss of business opportunities for retailers. Generally, retailers have the product which customers would actually buy, however, the sale does not happen due to a mismatch between actual customer intent and product descriptions.

Let's see how large language models can help.

Large language models can help bridge the gap between actual customer intent and product description

ChatGPT has delighted the world. It actually understands what we want, even though what we type in the prompt may not be perfect. The magic of ChatGPT is powered through large language models (LLMs). The LLMs are able to connect different terms and words even if they look very different. The way in which it does this is through the use of embeddings. The embeddings help place text close to each other if they represent the same context.

For example, in an embedding vector space, the terms such as cream, lotion, healing, and stem cell will be close to each other. Even if these words look different, they are close in embedding vector space as they represent a similar context which is skin health.

Shown below of embedding of product descriptions represented as a 2D scatterplot.

Embedding scatter plot (image by author)

Some of the products which are close to foot cream are heel healing lotion, tissue plus lotion, and night cream argan stem cell. Even though the products do not have foot cream in the description, they are still situated close to foot cream in the embedding vector space.

Embeddings are a really powerful way to power search on any e-commerce platform. They can help bridge the gap between customer intent and product description. The technique can be used to delight the customer on an e-commerce platform, just like they get delighted by looking at the ChatGPT responses. It can also translate into huge business benefits for any retailer as it can widen the scope of search results and is not only restricted to search term matching.

The embedding technique can also be used to recommend more refined search terms. For example, if the user types in “healthy food” as a search term, we can use the power of embeddings to recommend additional search terms such as balanced diet, wholesome food, and nutritious food.

Additional search term recommended by LLM (image by author)

Now let us look into technical implementation.

Technical Implementation

The flow of the technical implementation is shown below.

The flow of technical implementation (image by author)

It consists of the following:

  1. All product descriptions are sent to OpenAI GPT using an API call in order to get the embeddings. You can use any LLM, however in the technical implementation shown here, I have used OpenAI GPT LLM
  2. The embeddings received from OpenAI are essentially vector representations of each product description

The two above steps need to be done only once in batch mode.

3. When the user types in search-term, we can again make a call to OpenAI to get the embedding vector of the search term

4. We can then find the closest product embedding related to the search term embedding using vector distance

5. The product details are obtained for the closest products

6. The results are then returned back to the user

Here is a Python implementation

##Import packages
import openai
import pandas as pd
import re
import contextlib
import io
import tiktoken
from openai.embeddings_utils import get_embedding
from sklearn.cluster import KMeans
from sklearn.manifold import TSNE


##Read data
file_name = 'path_to_file'
df = pd.read_csv(file_name)

##Set parameters
embedding_model = "text-embedding-ada-002"
embedding_encoding = "cl100k_base"  # this the encoding for text-embedding-ada-002
max_tokens = 8000  # the maximum for text-embedding-ada-002 is 8191
top_n = 1000
encoding = tiktoken.get_encoding(embedding_encoding)
col_embedding = 'embedding'
n_tsne=2
n_iter = 1000

##Step 1 and 2 - Gets the embedding from OpenAI for product descriptions
def get_embedding(text, model):
  openai.api_key = "YOUR_OPENAPI_KEY"
  text = text.replace("\n", " ")
  return openai.Embedding.create(input = [text], model=model)['data'][0]['embedding']


col_txt = 'product'
col_embedding = 'embedding'
df["n_tokens"] = df[col_txt].apply(lambda x: len(encoding.encode(x)))
df = df[df.n_tokens <= max_tokens].tail(top_n)
df = df[df.n_tokens > 0].reset_index(drop=True) ##Remove if there no tokens, for example blank lines
df[col_embedding] = df[col_txt].apply(lambda x: get_embedding(x, model='text-embedding-ada-002'))
matrix = np.array(df[col_embedding].to_list())
##matrix has the embeddings for all products

##Step 3 to 6- Gets the embedding for search term and find closest product
search_term = "foot massage"
search_text_embeddings = get_embedding(search_term, model='text-embedding-ada-002')

#Find the distance between search_text_embeddings and matrix (for each row)
arr_distance = np.linalg.norm(matrix-search_text_embeddings, axis=1)

# Get the indices of the top 5 minimum values (handling ties correctly)
min_distance_indices = np.argsort(arr_distance)
top_min_distance_indices = min_distance_indices[:top_n]

#Get closest prroducts from dataframe
df1 = df.iloc[top_min_distance_indices]

Conclusion

Large language models can help bridge the gap between customer search intent and product description. This capability can be used to enhance product search and delight customers. It also represents a huge business opportunity for retailers.

Dataset

The data used in the blog has been created by the author based on common retail products

Please subscribe to stay informed whenever I release a new story.

You can also join Medium with my referral link

Additional Resources

Website

You can visit my website to make analytics with zero coding. https://experiencedatascience.com

YouTube channel

Please visit my YouTube channel to learn data science and AI use cases using demos

Large Language Models
Generative Ai Use Cases
Retail
Ecommerce
Llm
Recommended from ReadMedium