Enriching embeddings
Previously, when you embedded product information, you were limited to only embedding the product 'short_description'
, which captured some, but not all of the relevant product information available. In this exercise, you'll embed 'title'
, 'short_description'
, 'category'
, and 'features'
to capture much more information.
Here's a reminder 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"
]
},
...
]
When combining the features into a single string, it should have the following structure:
Title: <product title>
Description: <product description>
Category: <product category>
Features: <feature 1>; <feature 2>; <feature 3>; ...
This exercise is part of the course
Introduction to Embeddings with the OpenAI API
Exercise instructions
- Define a function called
create_product_text()
to combine thetitle
,short_description
,category
, andfeatures
data into a single string with the desired structure. - Use
create_product_text()
to combine the features for each product inproducts
, storing the results in a list. - Embed the text in
product_texts
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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 = ____