Generating Embeddings with OpenAI
Use the text-embedding-3-small and text-embedding-3-large models to embed sentences, paragraphs, and documents, and compare their trade-offs in quality and cost.
OpenAI Embedding Models Overview
OpenAI offers two production embedding models: text-embedding-3-small and text-embedding-3-large. The small model produces 1536-dimensional vectors and is 5x cheaper per token, while the large model produces 3072-dimensional vectors with higher accuracy on benchmarks. For most RAG applications, the small model is the right starting point.
Making Your First Embedding Call
The client.embeddings.create() method takes a model name and an input string or list. It returns a response object with a data list, where each item has an embedding attribute containing the vector as a Python list of floats.
Always store your API key in an environment variable — never hardcode it in source files.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ['OPENAI_API_KEY'])
response = client.embeddings.create(
model='text-embedding-3-small',
input='What is retrieval-augmented generation?'
)
vector = response.data[0].embedding
print(f'Vector length: {len(vector)}') # 1536
print(f'Type: {type(vector[0])}') # float
print(f'Sample values: {vector[:3]}') # [-0.02, 0.01, ...]All lessons in this course
- What Are Vector Embeddings?
- Generating Embeddings with OpenAI
- Semantic Search with NumPy
- Clustering and Visualizing Embeddings