商品の説明文を埋め込む
オンライン小売業者が販売する各種商品の情報を含む、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 に割り当てられています。
この演習はコースの一部です
OpenAI API ではじめる Embeddings 入門
演習の手順
- リスト内包表記を使って、
productsの各要素から'short_description'を取り出し、product_descriptionsというリストを作成します。 - バッチ処理を用いて、
text-embedding-3-smallモデルに入力を渡し、各商品の'short_description'の埋め込みを作成します。 - 各商品の埋め込みを
response_dictから取り出し、新しいキー'embedding'としてproductsに保存します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# 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())