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
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
usingcreate_embeddings()
, and extract and print the embeddings in a single list. - Embed
list_of_descriptions
usingcreate_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(____)