始める無料で始める

埋め込みの拡張

以前に商品情報を埋め込んだ際は '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"
        ]
    },
    ...
]

情報を1つの文字列にまとめる際は、次の構造にしてください。

Title: <product title>
Description: <product description>
Category: <product category>
Features: <feature 1>; <feature 2>; <feature 3>; ...

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

OpenAI API で学ぶ埋め込み入門

コースを見る

演習の手順

  • 関数 create_product_text() を定義し、titleshort_descriptioncategoryfeatures のデータを指定の構造で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 = ____
コードを編集して実行