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
.
This exercise is part of the course
Introduction to Embeddings with the OpenAI API
Exercise instructions
- Create a list called
product_descriptions
containing the'short_description'
for each product inproducts
using a list comprehension. - Create embeddings for each product
'short_description'
using batching, passing the input to thetext-embedding-3-small
model. - Extract the embeddings for each product from
response_dict
and store them inproducts
under a new key called'embedding'
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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())