商品のレコメンデーションシステム
この演習では、さまざまな商品を販売するオンライン小売向けにレコメンデーションシステムを作成します。商品ページを閲覧したものの購入しなかったユーザーに対して、最後に閲覧した商品に基づいて、類似する3つの商品をおすすめします。
サイトで販売中の商品を表す辞書のリストが用意されています。
products = [
{
"title": "Smartphone X1",
"short_description": "The latest flagship smartphone with AI-powered features and 5G connectivity.",
"price": 799.99,
"category": "Electronics",
"features": [
"6.5-inch AMOLED display",
...
"Fast wireless charging"
]
},
...
]
また、ユーザーが最後に閲覧した商品を表す辞書が last_product に格納されています。
コース前半で定義した次のカスタム関数も利用できます。
create_embeddings(texts)→textsの各テキストに対する埋め込みのリストを返します。create_product_text(product)→productの特徴を埋め込み用の1つの文字列に結合します。find_n_closest(query_vector, embeddings, n=3)→ コサイン距離に基づいて、query_vectorとembeddingsの間で最も近いn個の距離とそのインデックスを返します。
この演習はコースの一部です
OpenAI API ではじめる Embeddings 入門
演習の手順
create_product_text()を使って、last_productとproductsの各商品のテキスト特徴を結合します。create_embeddings()でlast_product_textとproduct_textsを埋め込みに変換し、last_product_embeddingsが単一のリストになるようにします。find_n_closest()を使って、最も小さいコサイン距離とそのインデックスを3件見つけます。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Combine the features for last_product and each product in products
last_product_text = ____
product_texts = ____
# Embed last_product_text and product_texts
last_product_embeddings = ____
product_embeddings = ____
# Find the three smallest cosine distances and their indexes
hits = ____(____, product_embeddings)
for hit in hits:
product = products[hit['index']]
print(product['title'])