In this tutorial, we will walk you through the process of building an e-commerce chatbot that utilizes Amazon product embeddings, the ChatGPT API (gpt-3.5-turbo) and Langchain to create a seamless and engaging user experience. Our chatbot will take user input, find relevant products from a dataset, and present the information in a friendly and detailed manner. This not only enhances the user experience but also makes the process of finding products much more enjoyable.
We will begin by loading and preprocessing the product data, followed by creating a Redis index and loading vectors into the index. Then, we will use Langchain to create an LLM chain and a prompt template for generating comma-separated product keywords based on the user input. Next, we will query the product embeddings in Redis using the generated keywords and retrieve the top results. Finally, we will present the retrieved products to the user in a nice and engaging way, allowing them to ask follow-up questions.
By the end of this tutorial, you will have a better understanding of how to build an CLI based e-commerce chatbot that can query Amazon product embeddings and generate user-friendly responses using Langchain. This will not only help improve the overall user experience in the e-commerce space but also pave the way for more advanced and personalized chatbot solutions in the future. So, let's get started and build our very own ecommerce chatbot!
Here is a quick example of how a conversation with our chatbot might look like:
Get the dataset CSV file from here.
Before we start, make sure you have the following Python packages installed:
redis
pandas
sentence-transformers
openai
langchain
You can install them using the following commands:
First, we need to load the product data from a CSV file and truncate long text fields. We will use the first 1000 products with non-empty item keywords for our chatbot.
Now, we will create a function to load vectors into the Redis index and a function to create a flat index. We will use these functions later to index our product data.
Next, we will create the Redis connection and load the vectors into the Redis index.
We will use the ChatGPT API (gpt-3.5-turbo) in combination with Langchain to create a response to our questions. If you want to dive deep and learn more about how to integrate the ChatGPT API to your other projects, we have dedicated tutorials for this.
We will use Langchain to create an LLM chain for our chatbot. First, we will create a prompt template to generate comma-separated product keywords from the user input.
Now, let's use our chain.
We will then use the generated keywords to query the product embeddings in Redis and retrieve the top 3 results.
Finally, we will create another LLM chain to generate a nice response from the retrieved products and present it to the user. The user is also able to ask follow up questions. Note, that we added a memory to the chain to keep track of the chat history.
from langchain.memory import ConversationBufferMemory
template = """You are a chatbot. Be kind, detailed and nice. Present the given queried search result in a nice way as answer to the user input. dont ask questions back! just take the given context
{chat_history}
Human: {user_msg}
Chatbot:"""
prompt = PromptTemplate(
input_variables=["chat_history", "user_msg"],
template=template
)
memory = ConversationBufferMemory(memory_key="chat_history")
llm_chain = LLMChain(
llm=OpenAI(model_name="gpt-3.5-turbo", temperature=0.8, openai_api_key="sk-9xxxxxxxxxxxxxxxxxxxx4"),
prompt=prompt,
verbose=False,
memory=memory,
)
answer = llm_chain.predict(user_msg=f"{full_result_string} ---\n\n {userinput}")
print("Bot:", answer)
time.sleep(0.5)
while True:
follow_up = input("Anything else you want to ask about this topic?")
print("User:", follow_up)
answer = llm_chain.predict(
user_msg=follow_up
)
print("Bot:", answer)
time.sleep(0.5)
In this tutorial, we have built an e-commerce chatbot that can query Amazon product embeddings using Redis and generate detailed and friendly responses with Langchain. We have demonstrated how to load and preprocess product data, create a Redis index, and load vectors into the index. We have also shown how to use Langchain to create an LLM chain for generating keywords and responses for user queries.
By utilizing the power of product embeddings and language models, our chatbot can efficiently search for relevant product recommendations and present them in an engaging and informative manner. This approach can be further extended to include more products, handle complex queries, and even provide personalized recommendations by incorporating user preferences.
We hope this tutorial has given you a good starting point for building your own e-commerce chatbot or implementing similar solutions in other domains. With the rapid advancements in AI technologies, there are endless possibilities for creating intelligent, engaging, and helpful chatbots that can improve user experience and drive business success.