Introduction to Embeddings with the OpenAI API
Emmanuel Pire
Senior Software Engineer, DataCamp
Classification tasks:
Example:
from openai import OpenAI
client = OpenAI(api_key="<OPENAI_API_KEY>")
response = client.embeddings.create(
model="text-embedding-3-small",
input="Embeddings are a numerical representation of text that can be used to measure the relatedness between two pieces of text."
)
response_dict = response.model_dump() print(response_dict)
{'object': 'list',
'data': [
{
"embedding": [0.0023064255, ..., -0.0028842222],
"index": 0,
"object": "embedding"
}
],
'model': 'text-embedding-3-small',
'usage': {
"prompt_tokens": 24,
"total_tokens": 24
}
}
print(response_dict['data'][0]['embedding'])
[0.0023064255, ...., -0.0028842222]
Introduction to Embeddings with the OpenAI API