Creates an embedding vector representing the input text. Embeddings are useful for semantic search, clustering, and similarity comparison.
Request Body
The embedding model to use (e.g., text-embedding-3-small, text-embedding-3-large, text-embedding-ada-002).
The text to embed. Can be a single string or an array of strings for batch processing.
The format to return embeddings in. Options: float or base64.
The number of dimensions for the output embeddings. Only supported by some models.
Response
Array of embedding objects. The index of the embedding in the input array.
The embedding vector (array of floats).
The model used to generate embeddings.
Token usage statistics. Number of tokens in the input.
Examples
Single Text Embedding
from openai import OpenAI
client = OpenAI(
api_key = "sk-voidai-your_key_here" ,
base_url = "https://api.voidai.app/v1"
)
response = client.embeddings.create(
model = "text-embedding-3-small" ,
input = "The quick brown fox jumps over the lazy dog."
)
embedding = response.data[ 0 ].embedding
print ( f "Embedding dimension: { len (embedding) } " )
print ( f "First 5 values: { embedding[: 5 ] } " )
Batch Embeddings
texts = [
"How do I reset my password?" ,
"What are your pricing plans?" ,
"How can I contact support?"
]
response = client.embeddings.create(
model = "text-embedding-3-small" ,
input = texts
)
for i, data in enumerate (response.data):
print ( f "Text { i } : { len (data.embedding) } dimensions" )
Custom Dimensions
# Use smaller dimension for efficiency
response = client.embeddings.create(
model = "text-embedding-3-large" ,
input = "Your text here" ,
dimensions = 256
)
embedding = response.data[ 0 ].embedding
print ( f "Reduced dimension: { len (embedding) } " ) # 256
Response Example
{
"object" : "list" ,
"data" : [
{
"object" : "embedding" ,
"index" : 0 ,
"embedding" : [ 0.0023 , -0.0095 , 0.0152 , ... ]
}
],
"model" : "text-embedding-3-small" ,
"usage" : {
"prompt_tokens" : 10 ,
"total_tokens" : 10
}
}
Use Cases
Semantic Search Find similar content by comparing embedding distances.
Clustering Group similar documents together based on embeddings.
Classification Use embeddings as features for ML classifiers.
Recommendations Find similar items for recommendation systems.
Similarity Search Example
import numpy as np
from openai import OpenAI
client = OpenAI(
api_key = "sk-voidai-your_key_here" ,
base_url = "https://api.voidai.app/v1"
)
def cosine_similarity ( a , b ):
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
# Create embeddings for documents
documents = [
"Python is a programming language" ,
"JavaScript runs in the browser" ,
"Machine learning uses neural networks"
]
doc_embeddings = client.embeddings.create(
model = "text-embedding-3-small" ,
input = documents
).data
# Search query
query = "How do I code in Python?"
query_embedding = client.embeddings.create(
model = "text-embedding-3-small" ,
input = query
).data[ 0 ].embedding
# Find most similar document
similarities = [
cosine_similarity(query_embedding, doc.embedding)
for doc in doc_embeddings
]
best_match_idx = np.argmax(similarities)
print ( f "Best match: { documents[best_match_idx] } " )
print ( f "Similarity: { similarities[best_match_idx] :.4f} " )
Model Comparison
Model Dimensions Best For text-embedding-3-small1536 Cost-effective, general use text-embedding-3-large3072 Highest quality, customizable dimensions text-embedding-ada-0021536 Legacy, broad compatibility