การเพิ่มความสมบูรณ์ให้เอ็มเบดดิ้ง
ก่อนหน้านี้ เมื่อสร้างเอ็มเบดดิ้งจากข้อมูลสินค้า เราใช้เพียง '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"
]
},
...
]
เมื่อรวม features เป็นสตริงเดียว โครงสร้างควรเป็นดังนี้:
Title: <product title>
Description: <product description>
Category: <product category>
Features: <feature 1>; <feature 2>; <feature 3>; ...
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การสร้าง Embeddings ด้วย OpenAI API
คำแนะนำการฝึกหัด
- กำหนดฟังก์ชันชื่อ
create_product_text()เพื่อรวมข้อมูลtitle,short_description,categoryและfeaturesเข้าเป็นสตริงเดียวตามโครงสร้างที่ต้องการ - ใช้
create_product_text()เพื่อรวมข้อมูล features ของสินค้าแต่ละรายการใน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 = ____