A recommendation chatbot uses ChatGPT to guide a customer to the right product. Before ChatGPT, chatbots only had a fixed set of responses. With ChatGPT's API and prompt engineering, a chatbot can talk naturally and point users to a large range of products and services.
Structure
graph TD U[User interface: chatbot] -->|prompt engineering| C[ChatGPT API] C -->|embedding of customer input| S[Server: embedded data + compute] P[Product database] -->|OpenAI embedding API| S H[Customer history database] -->|OpenAI embedding API| S S -->|best matches, prompt engineering| C C --> U
How it works:
- embed the databases: every product and every past order gets an embedding from OpenAI's API, and the embeddings are saved for future use
- embed the request: the customer chats with the bot, the relevant request is extracted, and an embedding is made for it
- match: cosine similarity finds the closest past purchases, then the closest products in the catalogue
- answer: the best matches go into a prompt, and ChatGPT presents the recommendation to the customer
Why cosine similarity
Cosine similarity is a very useful similarity measure for high-dimensional data such as embeddings. scipy's
cosinereturns a distance, so the similarity is1 - cosine(x, y).
Code
Each row of the table is turned into one string before it is embedded:
from scipy.spatial.distance import cosine
def cosine_similarity(x, y):
return 1 - cosine(x, y)
product_data_df['combined'] = product_data_df.apply(
lambda row: f"{row['brand']}, {row['prod']}, {row['description']}", axis=1)These are the 2 ways the course calls the OpenAI embedding API:
| Style | Call | Get the vector |
|---|---|---|
| Client (current) | client.embeddings.create(input=[text], model="text-embedding-ada-002") | .data[0].embedding |
| Module (older) | openai.Embedding.create(input=text, model="text-embedding-ada-002") | ['data'][0]['embedding'] |
def get_embedding(text, model="text-embedding-ada-002"):
text = text.replace("\n", " ")
return client.embeddings.create(input=[text], model=model).data[0].embedding
product_data_df['text_embedding'] = product_data_df.combined.apply(lambda x: get_embedding(x))Score, sort from most to least similar, and keep the top 3. The same 3 lines run on the order history and on the product catalogue:
customer_order_df['search_purchase_history'] = customer_order_df.text_embedding.apply(
lambda x: cosine_similarity(x, embeddings_customer_question))
customer_order_df = customer_order_df.sort_values('search_purchase_history', ascending=False)
top_3_purchases_df = customer_order_df.head(3)Building the prompt
The message list sets the stage, adds the customer's context, then plants the products as assistant turns:
| Role | Content |
|---|---|
| system | "You're a chatbot helping customers with beauty-related questions..." |
| user | The customer's question |
| user | "Here're my latest product orders: ..." (top 3 past purchases joined with ". ") |
| user | Ask for a detailed explanation, in a friendly human tone |
| assistant | "I found these 3 products I would recommend" |
| assistant (x3) | One message per recommended product |
| assistant | "Here's my summarized recommendation of products, and why it would suit you:" |
message_objects.extend(products_list) # extend adds 3 separate messages; append would add 1 nested list
completion = client.chat.completions.create(model="gpt-3.5-turbo", messages=message_objects)
print(completion.choices[0].message.content)Remembering tip
Embed, Match, Prompt, Chat: embed everything once, match the request with cosine similarity, put the top 3 into the prompt, and let ChatGPT do the talking.
Finding the closest stored items first and then letting the LLM write the answer is the same idea as Retrieval Augmented Generation (RAG).