เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การสร้าง Embedding จากคำอธิบายสินค้า

มี list ของ dictionary ชื่อ products ให้พร้อมแล้ว ซึ่งเก็บข้อมูลสินค้าต่าง ๆ ที่จำหน่ายโดยร้านค้าออนไลน์ งานของคุณคือสร้าง embedding จาก 'short_description' ของสินค้าแต่ละรายการ เพื่อเปิดใช้งานการค้นหาเชิงความหมาย (semantic search) บนเว็บไซต์ของร้านค้า

ด้านล่างนี้คือตัวอย่างข้อมูลใน list 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 ไว้แล้วและกำหนดให้กับตัวแปร client

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

การสร้าง Embeddings ด้วย OpenAI API

ดูคอร์ส

คำแนะนำการฝึกหัด

  • สร้าง list ชื่อ product_descriptions ที่เก็บค่า 'short_description' ของสินค้าแต่ละรายการใน products โดยใช้ list comprehension
  • สร้าง embedding สำหรับ 'short_description' ของสินค้าทุกรายการโดยใช้ batching และส่ง input ไปยังโมเดล text-embedding-3-small
  • ดึง embedding ของสินค้าแต่ละรายการจาก response_dict แล้วเก็บไว้ใน products ภายใต้คีย์ใหม่ชื่อ 'embedding'

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Extract a list of product short descriptions from products
product_descriptions = [____ for product in ____]

# Create embeddings for each product description
response = ____
response_dict = response.model_dump()

# Extract the embeddings from response_dict and store in products
for i, product in ____:
    product['embedding'] = response_dict[____][____][____]
    
print(products[0].items())
แก้ไขและรันโค้ด