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

Connected

道练习

为产品描述创建嵌入向量

您拿到一个名为 products 的字典列表,其中包含一家在线零售商所售不同产品的信息。您的任务是为每个产品的 'short_description' 创建嵌入向量(embedding — 向量表示),以支持该网站的语义搜索。

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

一个已创建好的 OpenAI 客户端已赋值给 client。

说明

100 XP
  • 使用列表推导式创建名为 product_descriptions 的列表,包含 products 中每个产品的 'short_description'。
  • 使用批处理(batching)为每个产品的 'short_description' 创建嵌入向量,输入到 text-embedding-3-small 模型。
  • 从 response_dict 中提取每个产品的嵌入向量,并将其存入 products,键名为 'embedding'。