We're talking new releases & fast AI at Redis Released. Join us in your city.

Register now

Build an E-commerce Chatbot With Redis, LangChain, and OpenAI

April 12, 2023

Given the recent surge of AI-enabling APIs and web development tools, it seems like everyone is building chatbots into their applications. Want to see what’s involved? Here’s an overview.

One new (and wildly popular) framework, LangChain, makes it easy to develop applications that interact with a language model and external sources of data or computation. It does this by focusing on clear and modular abstractions for all the building blocks necessary to build; then it constructs commonly used “chains,” which are combinations of the building blocks. For example, the Conversational Retrieval Chain enables users to have a “conversation” with their data in an external store.

How does it do this? OpenAI language models were not trained on your company’s specific data, or certainly not tuned for it. If you want the chatbot to rely on it, you need to provide OpenAI with your data at runtime. The retrieval step fetches relevant data to the user’s query from Redis using Vector Similarity Search (VSS) and then pipes the data into the language model along with the original question. It asks the model to use only the provided sources – what we in AI circles call “context” – to answer the question.

Most of the complexity in this chain comes down to the retrieval step. That is why we’re so excited to add an integration between LangChain and Redis Enterprise as a vector database. This combination makes it possible to bridge the gap between complex AI and product development – without breaking a sweat.

Don’t believe us? In this short tutorial we build a conversational retail shopping assistant that helps customers find items of interest that are buried in a product catalog. You can follow along with the full code.

Building your chatbot

Before we jump in, we’d like to thank Fabian Stehle from LabLab AI, who put together the initial prototype of this demo. We extended it and layered in additional LangChain components to give it more functionality.

First, let’s collect all the pieces we need for the project.

Install Python requirements

This project needs a few Python libraries. These are stored in the requirements.txt file at the github repo.

Fetch and prepare the products dataset

For the retail chatbot, we chose to work with the Amazon Berkeley Objects dataset. This includes a large selection of Amazon products that are perfect for generating a retail assistant. Download the file from the link, or use the gdown command line interface to download the file from a hosted link.

We use the pandas Python library to load and preprocess the dataset. While it loads, we truncate the longer text fields. That’s to keep our dataset a bit leaner, which saves on memory and compute time.

With our products dataset fully loaded, we perform some final preprocessing steps to clean up the keywords field and to drop missing values.

If you’re following along with the code on github, take a peek at the dataframe with all_prods_df.head(). The full dataset contains over 100,000 products, but for this chatbot, we restrict it to a subset of 2,500.

Here is an example of one of the product JSON objects we have to work with.

Set up Redis as a vector database

LangChain has a simple wrapper around Redis to help you load text data and to create embeddings that capture “meaning.” In this code, we prepare the product text and metadata, prepare the text embeddings provider (OpenAI), assign a name to the search index, and provide a Redis URL for connection.

At this point, we’ve successfully processed the Amazon products dataset and loaded it into the Redis database with vector embeddings.

Then we bring it all together to create the Redis vectorstore.

Now we’re ready to create a chatbot that uses the products’ data (stored in Redis) to inform conversations.

Create the LangChain conversational chain

Chatbots are hugely popular because they can be immensely useful. In the scenario we build below, we assume that you need fashion advice. You can ask the bot for help in finding a pair of shoes suitable for both casual outings and work-related outings. You want something that pops, but doesn’t cause too much of a distraction. Given the data we fed it already, our chatbot should be able to recommend a few pairs of shoes that fit the requirements.

It’s time to bring in more LangChain functionality. To do so, we need to import several LangChain tools.

As mentioned in the introduction, this project uses a ConversationalRetrievalChain to simplify chatbot development.

Redis holds our product catalog including metadata and OpenAI-generated embeddings that capture the semantic properties of the product content. Under the hood, using Redis Vector Similarity Search (VSS), the chatbot queries the catalog for products that are most similar to or relevant to what the user is shopping for. No fancy keyword search or manual filtering is needed; VSS takes care of it.

The ConversationalRetrievalChain that forms the chatbot operates in three phases:

  1. Question creation evaluates the input question and uses the OpenAI GPT model to combine it with knowledge from previous conversational interactions (if any).
  2. Retrieval searches Redis for the best available products, given the items in which the shopper expressed interest.
  3. Question answering gets the product results from the vector search query and uses the OpenAI GPT model to help the shopper navigate the options.

Even though LangChain and Redis greatly expedite this workflow, interacting with a large language model (LLM) like GPT requires a “prompt” for communication. We humans create a prompt (set of instructions) to steer the model’s behavior towards a desired outcome. To get the best results from the chatbot, further prompt engineering may help.

See the two prompts we define for steps 1 and 3 above. You can always start with these and improve them for your own scenario.

Next, we define two OpenAI LLMs and wrap them with chains for question generation and question answering respectively. The streaming_llm allows us to pipe the chatbot responses to stdout, token by token, giving it a charming, chatbot-like user experience.

Finally, we tie it all together with the ConversationalRetrievalChain that wraps all three steps.

Experiment with your friendly virtual shopping assistant

Keep in mind, this is not an all-intelligent being. But with the help of Redis, which stores the example’s entire product inventory knowledge base, we create a pretty neat experience.

The bot interacts with you in realtime, and helps you narrow in on interesting product choices based on what’s in the catalog. Here’s a simple example:

After the chatbot welcomes you with, “Hi! What are you looking for today?” try a few of these sample prompts, or make your own:

  • Fancy earrings that are silver or gold
  • Comfortable walking shoes
  • A durable iPhone case

Customize your chains for better performance

One of the best parts about LangChain is that each class abstraction is made so that you can extend or create your own. Below, we customize the BaseRetriever class to perform some document preprocessing before it returns the results.

We need to update the retrieval class and chatbot to use the custom implementation above.

Done! Now your chatbot can infuse more product information in your conversation as it steers you towards e-commerce glory! Here’s another short conversation example:

Next steps

Building with LangChain and Redis is easy. Try building this chatbot on your own, or customizing it for your use case. Try Redis Enterprise for free or pull our Redis Stack docker container to get started.

Interested in getting your hands dirty with AI? Our partners at LabLab AI are hosting a series of hackathons over the next month featuring Redis. Compete for prizes, hype, and fame. In fact, there’s a hackathon with Stable Diffusion starting April 14th!

Stable diffusion AI hackathon promo image

Like what you’re hearing about Generative AI from Tyler and Harrison? See them share more practical examples during their joint session at RedisDays Virtual, a free virtual event on May 24th.

Learn more about Vector Similarity Search in Redis. You may be amazed by what you can accomplish.