Bắt đầu ngayBắt đầu miễn phí

Nhúng mô tả sản phẩm

Bạn được cung cấp một danh sách các dictionary tên là products, chứa thông tin sản phẩm cho nhiều mặt hàng do một nhà bán lẻ trực tuyến cung cấp. Nhiệm vụ của bạn là nhúng 'short_description' cho từng sản phẩm để kích hoạt tìm kiếm ngữ nghĩa cho website của nhà bán lẻ.

Đây là phần xem trước danh sách dictionary 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"
        ]
    },
    ...
]

Một OpenAI client đã được tạo sẵn và gán vào client.

Bài tập này là một phần của khóa học

Nhập môn Embeddings với OpenAI API

Xem khóa học

Hướng dẫn bài tập

  • Tạo danh sách product_descriptions chứa 'short_description' của từng sản phẩm trong products bằng list comprehension.
  • Tạo embedding cho 'short_description' của mỗi sản phẩm bằng cách xử lý theo lô (batching), truyền input vào model text-embedding-3-small.
  • Trích xuất embedding cho từng sản phẩm từ response_dict và lưu vào products dưới một khóa mới tên 'embedding'.

Bài tập tương tác thực hành trực tiếp

Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.

# 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())
Chỉnh sửa và Chạy Mã