1. 学ぶ
  2. /
  3. コース
  4. /
  5. OpenAI API ではじめる Embeddings 入門

Connected

演習

商品のレコメンデーションシステム

この演習では、さまざまな商品を販売するオンライン小売向けにレコメンデーションシステムを作成します。商品ページを閲覧したものの購入しなかったユーザーに対して、最後に閲覧した商品に基づいて、類似する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 個の距離とそのインデックスを返します。

指示

100 XP
  • create_product_text() を使って、last_product と products の各商品のテキスト特徴を結合します。
  • create_embeddings() で last_product_text と product_texts を埋め込みに変換し、last_product_embeddings が単一のリストになるようにします。
  • find_n_closest() を使って、最も小さいコサイン距離とそのインデックスを3件見つけます。