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

Exercise

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>; ...

Instructions

100 XP
  • Copy your API key and provide it to the OpenAI client.
  • Define a function called create_product_text() to combine the title, short_description, category, and features data into a single string with the desired structure.
  • Use create_product_text() to combine the features for each product in products, storing the results in a list.
  • Embed the text in product_texts.