丰富嵌入向量
之前在对商品信息进行嵌入时,您只能嵌入商品的 '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>; ...
本练习是课程的一部分
使用 OpenAI API 的 Embeddings 入门
练习说明
- 定义函数
create_product_text(),将title、short_description、category和features合并为一个具有上述结构的字符串。 - 使用
create_product_text()为products中的每个商品合并特性,并将结果存入列表。 - 对
product_texts中的文本进行嵌入。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# 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 = ____