How to Build a Chatbot for Product Recommendations Using ChatGPT API and Embeddings

Rabie Rheribi

--

Are you looking to create a chatbot that can provide personalized product recommendations to your customers based on their unique profiles? Look no further! In this comprehensive guide, we’ll walk you through building a sophisticated chatbot using embeddings to match a user’s profile with relevant products from your company’s extensive database. By the end of this tutorial, you’ll have all the tools you need to develop a customer-facing chatbot capable of boosting engagement and driving sales.

While we will primarily focus on an example in the beauty e-commerce industry, the techniques discussed here are applicable across various domains, making it a versatile solution for businesses.

Table of Contents

  1. Introduction to Embeddings
  2. Obtaining OpenAI API Keys
  3. Creating a Product Dataset
  4. Generating Embeddings for Product Dataset
  5. Building a Customer Profile Dataset
  6. Creating Embeddings for Customer Profile Dataset
  7. Embeddings for Customer Chat Messages
  8. Calculating Similarities for Previous Purchases
  9. Calculating Similarities with Product Database
  10. Constructing a ChatGPT API Prompt
  11. Generating ChatGPT Product Recommendations
  12. Improvements and Next Steps

1. Introduction to Embeddings

What Are Embeddings?

Embeddings, in the realm of natural language processing (NLP), are dense numerical vectors that represent words, phrases, or even entire documents. These vectors, often high-dimensional, are engineered to capture intricate semantic and syntactic relationships within text data. For instance, words with similar meanings or those used in analogous contexts are mapped to nearby vectors.

In our use case, embeddings serve as a bridge between user profiles and product information, enabling the calculation of similarity scores to facilitate product recommendations tailored to each user.

OpenAI Endpoints for Embeddings

The OpenAI API offers two pivotal endpoints for working with embeddings:

  • search: This endpoint is employed to retrieve documents or snippets similar to a given input text. It returns a list of search results, each accompanied by a document ID, score, and matched text. The score quantifies the similarity between the input text and the matched document.
  • similarity: When the task involves measuring similarity between two pieces of text or documents, the similarity endpoint comes into play. It returns a single score ranging from 0 to 1, indicating the similarity between the two input texts. A score of 0 signifies complete dissimilarity, while 1 denotes identical texts.

In this guide, we’ll opt for the similarity endpoint since our objective is to recommend products based on customer profiles.

With this foundational understanding, let’s proceed to obtaining the OpenAI API keys.

2. Obtaining OpenAI API Keys

Before diving into the coding process, you must obtain your OpenAI API credentials. Here’s a step-by-step guide:

  1. Visit https://beta.openai.com/.
  2. Log in to your OpenAI account.
  3. Click on your avatar to access your account settings.
  4. Navigate to “View API Keys.”
  5. Create a new secret key and securely save it for future API requests.

With your API keys in hand, you’re now ready to start building your chatbot.

3. Creating a Product Dataset

The initial step in building our recommendation system is to establish a product dataset. We’ll start by importing essential libraries and setting up the OpenAI API key.

import openai
from openai.embeddings_utils import get_embedding, cosine_similarity
import pandas as pd

api_key = "YOUR_API_KEY" # Replace with your actual API key
openai.api_key = api_key

# Define your product data (this is an example)
product_data = [
{
"prod_id": 1,
"prod": "moisturizer",
"brand": "Aveeno",
"description": "for dry skin"
},
# Add more product entries
]

# Create a DataFrame to store the product data
product_data_df = pd.DataFrame(product_data)

# Create a new column 'combined' for embeddings
product_data_df['combined'] = product_data_df.apply(lambda row: f"{row['brand']}, {row['prod']}, {row['description']}", axis=1)

4. Generating Embeddings for Product Dataset

Now, let’s proceed to create embeddings for the product data. This step is crucial as it transforms product information into numerical representations that can be used to calculate similarity scores.

# Generate embeddings for the 'combined' column using the OpenAI API
product_data_df['text_embedding'] = product_data_df.combined.apply(lambda x: get_embedding(x, engine='text-embedding-ada-002'))

5. Building a Customer Profile Dataset

In order to provide personalized recommendations, we need customer profiles or data that represents their preferences or past interactions. For this guide, we’ll simulate customer data using a made-up order history.

# Define customer order data (this is an example)
customer_order_data = [
{
"prod_id": 1,
"prod": "moisturizer",
"brand": "Aveeno",
"description": "for dry skin"
},
# Add more customer order entries
]

# Create a DataFrame for customer order data
customer_order_df = pd.DataFrame(customer_order_data)

# Create a new column 'combined' for embeddings
customer_order_df['combined'] = customer_order_df.apply(lambda row: f"{row['brand']}, {row['prod']}, {row['description']}", axis=1)

6. Creating Embeddings for Customer Profile Dataset

Next, let’s generate embeddings for the customer profiles using the same approach as before:

# Generate embeddings for the 'combined' column in the customer order dataset
customer_order_df['text_embedding'] = customer_order_df.combined.apply(lambda x: get_embedding(x, engine='text-embedding-ada-002'))

7. Embeddings for Customer Chat Messages

As part of the chatbot interaction, customers will provide chat messages to inquire about product recommendations. Let’s generate embeddings for these customer chat messages.

# Define a customer chat message
customer_input = "Hi! Can you recommend a good moisturizer for me?"

# Generate embeddings for the customer input message
response = openai.Embedding.create(
input=customer_input,
model="text-embedding-ada-002"
)
embeddings_customer_question = response['data'][0]['embedding']

8. Calculating Similarities for Previous Purchases

Now, we’ll compare the embeddings of the customer’s previous purchases with the embedding of the customer’s chat input message using cosine similarity. This will help us identify products similar to what the customer has previously bought.

# Calculate similarities between customer's previous purchases and the customer's chat input question
customer_order_df['search_purchase_history'] = customer_order_df.text_embedding.apply(lambda x: cosine_similarity(x, embeddings_customer_question))

# Sort the DataFrame in descending order based on similarity score
customer_order_df = customer_order_df.sort_values('search_purchase_history', ascending=False)

10. Constructing a ChatGPT API Prompt

Now that we have calculated similarities, it’s time to construct a ChatGPT API prompt to engage with the chatbot. The prompt consists of a series of messages between the user and the assistant.

# Create a list to store message objects
message_objects = []

# Add a system message to set the assistant's behavior
message_objects.append({"role": "system", "content": "You're a chatbot helping customers with beauty-related questions and providing product recommendations"})

# Add the user's initial message
message_objects.append({"role": "user", "content": customer_input})

# Add instructions for a detailed explanation and friendly interaction
message_objects.append({"role": "user", "content": "Please give me a detailed explanation of your recommendations"})
message_objects.append({"role": "user", "content": "Please be friendly and talk to me like a person, don't just give me a list of recommendations"})

# Add an assistant message
message_objects.append({"role": "assistant", "content": "I found these 3 products I would recommend"})

# Create a list of the top 3 product recommendations
products_list = []

for index, row in product_data_df.head(3).iterrows():
brand_dict = {'role': "assistant", "content": f"{row['combined']}"}
products_list.append(brand_dict)

# Extend the message_objects list with product recommendations
message_objects.extend(products_list)

# End the prompt with a summary instruction
message_objects.append({"role": "assistant", "content": "Here's my summarized recommendation of products, and why they would suit you:"})

11. Generating ChatGPT Product Recommendations

The final step involves calling the ChatGPT API to obtain product recommendations based on the customer’s profile and input. Here’s how to do it:

# Call the ChatGPT API to generate product recommendations
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=message_objects
)

# Print the model's response
print(completion.choices[0].message['content'])

And there you have it! The chatbot will generate product recommendations based on the customer’s profile, previous purchases, and the product database.

12. Improvements and Next Steps

While this guide provides a strong foundation for building a chatbot that recommends products based on user profiles and previous purchases, there are several areas for further improvement:

Product Data: In this guide, we used a minimal dataset with basic product information. To improve recommendations, consider using a more extensive dataset with detailed product descriptions.

Increase Previous Purchases: Expanding the number of previously purchased products considered for recommendations can provide a broader scope of suggestions.

Extract Product Type: Extracting the product type from the user input question can enhance the accuracy of recommendations by including relevant product types.

Troubleshooting

AttributeError: module ‘openai’ has no attribute ‘ChatCompletion’: Upgrade your OpenAI Python client library to at least version 0.27.0 using pip install openai --upgrade.

InvalidRequestError: This model’s maximum context length is 4096 tokens: To resolve this issue, shorten your messages to fit within the 4096-token limit.

Get Code

  1. GitHub Repository: Find the complete source code in this GitHub repository to implement

--

--

Rabie Rheribi
Rabie Rheribi

Written by Rabie Rheribi

Rabie Rheribi founded Affstuff.com, a top affiliate marketing platform that uses AI to revolutionize online product promotion and monetization.

Responses (1)