始める無料で始める

商品説明の埋め込み

オンライン小売業者が販売するさまざまな商品の情報を含む、products という辞書のリストが用意されています。この小売業者のWebサイトでセマンティック検索を実現するために、各商品の 'short_description' を埋め込む作業を行いましょう。

products リストのプレビューは以下のとおりです。

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",
            "Quad-camera system with 48MP main sensor",
            "Face recognition and fingerprint sensor",
            "Fast wireless charging"
        ]
    },
    ...
]

OpenAI クライアントはすでに作成され、client に割り当てられています。

この演習はコースの一部です

OpenAI API で学ぶ埋め込み入門

コースを見る

演習の手順

  • リスト内包表記を使って、products 内の各商品の 'short_description' を含む product_descriptions というリストを作成してください。
  • バッチ処理を用いて、各商品の 'short_description' の埋め込みを作成してください。入力を text-embedding-3-small モデルに渡します。
  • response_dict から各商品の埋め込みを取り出し、 products を新しいキー 'embedding' として保存してください。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

# Extract a list of product short descriptions from products
product_descriptions = [____ for product in ____]

# Create embeddings for each product description
response = ____
response_dict = response.model_dump()

# Extract the embeddings from response_dict and store in products
for i, product in ____:
    product['embedding'] = response_dict[____][____][____]
    
print(products[0].items())
コードを編集して実行