1. Learn
  2. /
  3. Courses
  4. /
  5. Introduction to Embeddings with the OpenAI API

Connected

Exercise

Embedding product descriptions

You've been provided with a list of dictionaries called products, which contains product information for different products sold by an online retailer. It's your job to embed the 'short_description' for each product to enable semantic search for the retailer's website.

Here's a preview of the products list of dictionaries:

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"
        ]
    },
    ...
]

An OpenAI client has already been created as assigned to client.

Instructions

100 XP
  • Create a list called product_descriptions containing the 'short_description' for each product in products using a list comprehension.
  • Create embeddings for each product 'short_description' using batching, passing the input to the text-embedding-3-small model.
  • Extract the embeddings for each product from response_dict and store them in products under a new key called 'embedding'.