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

Connected

演習

商品の説明文を埋め込む

オンライン小売業者が販売する各種商品の情報を含む、products という辞書のリストが用意されています。小売業者のサイトでセマンティック検索を可能にするため、各商品の '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 に割り当てられています。

指示

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