Adding user history to the recommendation engine
For many recommendation cases, such as film or purchase recommendation, basing the next recommendation on one data point will be insufficient. In these cases, you'll need to embed all or some of the user's history for more accurate and relevant recommendations.
In this exercise, you'll extend your product recommendation system to consider all of the products the user has previously visited, which are stored in a list of dictionaries called user_history
.
The following custom functions are available for you to use: create_embeddings(texts)
, create_product_text(product)
, and find_n_closest(query_vector, embeddings, n=3)
. numpy
has also been imported as np
.
This exercise is part of the course
Introduction to Embeddings with the OpenAI API
Exercise instructions
- Combine the text features for each product in
user_history
, embed the resulting strings, and calculate the mean embeddings usingnumpy
. - Filter
products
to remove any products that are present inuser_history
. - Combine the features for each product in
products_filtered
and embed the resulting strings.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Prepare and embed the user_history, and calculate the mean embeddings
history_texts = [____ for article in user_history]
history_embeddings = ____
mean_history_embeddings = ____
# Filter products to remove any in user_history
products_filtered = ____
# Combine product features and embed the resulting texts
product_texts = ____
product_embeddings = ____
hits = find_n_closest(mean_history_embeddings, product_embeddings)
for hit in hits:
product = products_filtered[hit['index']]
print(product['title'])