1. Learn
  2. /
  3. Courses
  4. /
  5. Introduction to Embeddings with the OpenAI API

Exercise

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.

Instructions

100 XP
  • Copy your API key and provide it to the OpenAI client.
  • 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.