상품 추천 시스템
이 연습 문제에서는 다양한 상품을 판매하는 온라인 소매업체를 위한 추천 시스템을 만들어 볼 거예요. 이 시스템은 사용자가 어떤 상품 페이지를 방문했지만 구매하지 않은 경우, 마지막으로 방문한 상품을 기준으로 유사한 상품 3개를 추천합니다.
아래는 사이트에서 판매 중인 상품 정보를 담은 딕셔너리 리스트입니다.
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",
...
"Fast wireless charging"
]
},
...
]
또한 사용자가 마지막으로 방문한 상품 정보가 last_product 딕셔너리에 저장되어 있습니다.
이 코스에서 앞서 정의해 둔 다음 사용자 정의 함수들도 사용할 수 있어요:
create_embeddings(texts)→texts의 각 텍스트에 대한 임베딩 리스트를 반환합니다.create_product_text(product)→product의 특징들을 임베딩용 단일 문자열로 결합합니다.find_n_closest(query_vector, embeddings, n=3)→ 코사인 거리 기준으로query_vector와embeddings간의 가장 가까운n개의 거리와 해당 인덱스를 반환합니다.
이 연습은 강의의 일부입니다
OpenAI API로 시작하는 임베딩 Introduction
연습 안내
create_product_text()을 사용해last_product와products의 각 상품에서 텍스트 특징을 결합하세요.create_embeddings()로last_product_text와product_texts를 임베딩하세요. 이때last_product_embeddings는 단일 리스트가 되도록 하세요.find_n_closest()를 사용해 가장 작은 코사인 거리 3개와 그 인덱스를 찾으세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Combine the features for last_product and each product in products
last_product_text = ____
product_texts = ____
# Embed last_product_text and product_texts
last_product_embeddings = ____
product_embeddings = ____
# Find the three smallest cosine distances and their indexes
hits = ____(____, product_embeddings)
for hit in hits:
product = products[hit['index']]
print(product['title'])