商品レコメンデーションシステム
この演習では、さまざまな商品を販売するオンライン小売業者向けのレコメンデーションシステムを作成します。このシステムは、商品ページを訪問したものの購入しなかったユーザーに対して、最後に閲覧した商品をもとに類似商品を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 で学ぶ埋め込み入門
演習の手順
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'])