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

Connected

Exercise

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.

Instructions

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