埋め込みをリッチ化する
以前は、商品情報を埋め込む際に 'short_description' のみを対象としており、商品に関する重要な情報の一部しか取り込めていませんでした。この演習では、より多くの情報を捉えるために、'title'、'short_description'、'category'、'features' を埋め込み対象にします。
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"
]
},
...
]
機能一覧(features)を1つの文字列にまとめるときは、次の構造にしてください:
Title: <product title>
Description: <product description>
Category: <product category>
Features: <feature 1>; <feature 2>; <feature 3>; ...
この演習はコースの一部です
OpenAI API ではじめる Embeddings 入門
演習の手順
create_product_text()という関数を定義し、title、short_description、category、featuresを所定の構造で1つの文字列に結合します。create_product_text()を使ってproductsの各商品の情報を結合し、結果をリストに保存します。product_textsのテキストを埋め込みます。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Define a function to combine the relevant features into a single string
def create_product_text(product):
return f"""Title: {____}
Description: {____}
Category: {____}
Features: {____}"""
# Combine the features for each product
product_texts = [____ for product in ____]
# Create the embeddings from product_texts
product_embeddings = ____