aiEmbed
Generate vector embeddings for text using AI providers. Embeddings are numerical representations that capture semantic meaning, enabling semantic search, similarity comparison, clustering, and recommendations.
Syntax
aiEmbed(input, params, options)Parameters
input
any
Yes
-
The text or array of texts to generate embeddings for
params
struct
No
{}
Request parameters for the AI provider (e.g., { model: "text-embedding-3-large" })
options
struct
No
{}
Request options (e.g., { provider: "openai", returnFormat: "embeddings" })
Options Structure
provider
string
(config)
The AI provider to use (openai, cohere, voyage, ollama)
apiKey
string
(config/env)
API key for the provider
returnFormat
string
"raw"
Response format: "raw", "embeddings", "first"
timeout
numeric
30
Request timeout in seconds
Return Formats
"raw" (default): Full API response with metadata
"embeddings": Array of embedding vectors only
"first": Single embedding vector (first item if batch)
Returns
Returns embedding data based on returnFormat:
"raw": Complete response with
data,model,usagemetadata"embeddings": Array of embedding vectors (arrays of floats)
"first": Single embedding vector (array of floats)
Examples
Basic Single Text Embedding
Get Just the Vector
Batch Embeddings
Array of Vectors
Specific Provider and Model
Semantic Search
Document Chunking with Embeddings
Voyage AI Embeddings
Ollama Local Embeddings
Cohere Input Types
Multilingual Embeddings
Caching Embeddings
RAG (Retrieval Augmented Generation)
Notes
📐 Dimensions: Most models output 1024-3072 dimensional vectors
💰 Cost: Embedding models are typically much cheaper than chat models
🚀 Performance: Batch requests (arrays) are more efficient than individual calls
🔍 Use Cases: Semantic search, similarity, clustering, recommendations, anomaly detection
🌍 Multilingual: Modern embedding models support 100+ languages
💾 Storage: Vectors are large - consider compression for scale
🎯 Events: Fires
beforeAIEmbedandafterAIEmbedevents
Related Functions
aiChunk()- Chunk large documents before embeddingaiTokens()- Estimate token usageaiMemory()- Store and search embeddings with vector memoryaiService()- Get embedding service providers
Best Practices
✅ Batch when possible - Send arrays of texts for better performance
✅ Cache embeddings - Embeddings are deterministic, cache for reuse
✅ Chunk long documents - Most models have token limits (e.g., 8192)
✅ Use appropriate models - Larger models (3-large) for critical search, smaller for scale
✅ Normalize vectors - Some similarity calculations require unit vectors
✅ Store metadata - Keep original text with embeddings for retrieval
❌ Don't embed everything - Embeddings cost money and storage, be selective
❌ Don't forget rate limits - Batch and throttle large embedding jobs
❌ Don't mix models - Use same model for queries and documents for consistency
Last updated