Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Introduction to Embeddings with the OpenAI API

Cursus bekijken

Oefeninstructies

  • 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'.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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())
Code bewerken en uitvoeren