Aan de slagGa gratis aan de slag

Finding the most similar product

Being able to compute similarity between embeddings is a key step within embeddings applications. In this exercise, you'll return to the products list of dictionaries that you worked with previously, which contains the embedded short descriptions you also created earlier.

You'll compare a piece of text to these embedded descriptions to identify the most similar description.

numpy has been imported as np, and distance is available from scipy.spatial. A create_embeddings() function has already been defined for you and is available to use for creating embeddings from a single input.

Deze oefening maakt deel uit van de cursus

Introduction to Embeddings with the OpenAI API

Cursus bekijken

Oefeninstructies

  • Embed the text, "soap", using your create_embeddings() custom function and extract a single list of embeddings.
  • Compute the cosine distance between the query_embedding and the embeddings in product.
  • Find and print the 'short_description' of the most similar product to the search text using the cosine distances in distances.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Embed the search text
search_text = "soap"
search_embedding = ____

distances = []
for product in products:
  # Compute the cosine distance for each product description
  dist = ____(search_embedding, ____)
  distances.append(dist)

# Find and print the most similar product short_description    
min_dist_ind = ____
print(____)
Code bewerken en uitvoeren