豐富化 embeddings
先前在產生商品資訊的嵌入向量時,你只能嵌入商品的 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>; ...
本練習屬於課程
Introduction to Embeddings with the OpenAI API
練習說明
- 定義名為
create_product_text()的函式,將title、short_description、category與features結合為一個具備指定結構的字串。 - 使用
create_product_text()為products中的每個商品組合內容,並將結果儲存成清單。 - 對
product_texts中的文字產生嵌入向量(embeddings)。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# 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 = ____