Get startedGet started for free

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.

This exercise is part of the course

Introduction to Embeddings with the OpenAI API

View Course

Exercise instructions

  • 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.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 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(____)
Edit and Run Code