BaşlayınÜcretsiz Başlayın

Semantic search for products

Time to put your find_n_closest() function to use! You'll test out your semantic product search on a test query, computing a sorted list of the five most semantically similar products, based on the enriched data you gave the model.

Here's a reminder of the find_n_closest() function you created in the previous exercise:

def find_n_closest(query_vector, embeddings, n=3):
    distances = []
    for index, embedding in enumerate(embeddings):
        distance = spatial.distance.cosine(query_vector, embedding)
        distances.append({"distance": distance, "index": index})
    distances_sorted = sorted(distances, key=lambda x: x["distance"])
    return distances_sorted[0:n]

The create_embeddings() function you created earlier is also available. Recall that it takes some text, and returns a list of lists containing the embeddings for each text. The products dictionary and the product_embeddings you created previously have also been loaded.

Bu egzersiz

Introduction to Embeddings with the OpenAI API

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • Create the query vector from query_text.
  • Find the five closest distances and their corresponding indexes using the find_n_closest() function.
  • Loop over hits and extract the product at each 'index' in the hits list.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

# Create the query vector from query_text
query_text = "computer"
query_vector = ____[0]

# Find the five closest distances
hits = ____

print(f'Search results for "{query_text}"')
for hit in hits:
  # Extract the product at each index in hits
  product = products[____]
  print(product["title"])
Kodu Düzenle ve Çalıştır