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

Làm giàu embeddings

Trước đây, khi bạn tạo embedding cho thông tin sản phẩm, bạn chỉ có thể dùng 'short_description' của sản phẩm, phần nào nắm bắt được thông tin nhưng chưa đầy đủ. Trong bài này, bạn sẽ embed cả 'title', 'short_description', 'category', và 'features' để thu thập nhiều thông tin hơn.

Dưới đây là danh sách từ điển products để bạn tiện tham chiếu:

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"
        ]
    },
    ...
]

Khi gộp các thuộc tính thành một chuỗi, cấu trúc cần như sau:

Title: <product title>
Description: <product description>
Category: <product category>
Features: <feature 1>; <feature 2>; <feature 3>; ...

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

  • Định nghĩa hàm create_product_text() để kết hợp dữ liệu title, short_description, category, và features vào một chuỗi duy nhất theo cấu trúc yêu cầu.
  • Dùng create_product_text() để kết hợp các thuộc tính cho từng sản phẩm trong products, lưu kết quả vào một danh sách.
  • Tạo embedding cho văn bản trong product_texts.

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.

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