Get startedGet started for free

More repeatable embeddings

As you continue to work with embeddings, you'll find yourself making repeated calls to OpenAI's embedding model. To make these calls in a more repeatable and modular way, it would be better to define a custom function called create_embeddings() that would output embeddings for any number of text inputs. In this exercise, you'll do just that!

This exercise is part of the course

Introduction to Embeddings with the OpenAI API

View Course

Exercise instructions

  • Define a create_embeddings() function that sends an input, texts, to the embedding model, and returns a list containing the embeddings for each input text.
  • Embed short_description using create_embeddings(), and extract and print the embeddings in a single list.
  • Embed list_of_descriptions using create_embeddings() and print.

Hands-on interactive exercise

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

# Define a create_embeddings function
def create_embeddings(texts):
  response = client.____(
    model="text-embedding-3-small",
    input=____
  )
  response_dict = response.model_dump()
  
  return [data['____'] for data in ____['data']]

# Embed short_description and print
print(____)

# Embed list_of_descriptions and print
print(____)
Edit and Run Code