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

Connected

Exercise

Classifying review sentiment

Now that you've calculated the embeddings, it's time to compute the cosine distances and extract the most similar label.

You'll do this by defining a function called find_closest(), which can be used to compare the embeddings between one vector and multiple others, and return the nearest distance and its index. You'll then loop over the reviews and and use find_closest() to find the closest distance for each review, extracting the classified label using the index.

The class_embeddings and review_embeddings objects you created in the last exercise are available for you to use, as well as the reviews and sentiments.

Instructions

100 XP
  • Define a function called find_closest() that returns the distance and index of the most similar embedding to the query_vector.
  • Use find_closest() to find the closest distance between each review's embeddings and the class_embeddings.
  • Use the 'index' of closest to subset sentiments and extract the 'label'.