1. Learn
  2. /
  3. 课程
  4. /
  5. 使用 OpenAI API 的 Embeddings 入门

Connected

道练习

丰富嵌入向量

之前在对商品信息进行嵌入时,您只能嵌入商品的 'short_description'。这能覆盖部分信息,但无法囊括所有可用的关键信息。在本练习中,您将同时嵌入 'title'、'short_description'、'category' 和 'features',以捕捉更完整的商品信息。

下面是 products(字典列表)的提醒:

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

将各项特性合并为单个字符串时,目标结构如下:

Title: <product title>
Description: <product description>
Category: <product category>
Features: <feature 1>; <feature 2>; <feature 3>; ...

说明

100 XP
  • 定义函数 create_product_text(),将 title、short_description、category 和 features 合并为一个具有上述结构的字符串。
  • 使用 create_product_text() 为 products 中的每个商品合并特性,并将结果存入列表。
  • 对 product_texts 中的文本进行嵌入。