Embedding Generation
Overview
Generate embeddings from text, images, and multimodal data with intelligent caching.
Text Embeddings
Generate embeddings from text using the embed_text function. NeuronDB supports multiple embedding models and automatically caches results for performance.
Basic Text Embedding
Generate text embedding
-- Basic text embedding (uses default model)
SELECT
'Hello, world!' AS text,
embed_text('Hello, world!') AS embedding,
vector_dims(embed_text('Hello, world!')) AS dimensions;
-- Verify embedding is not null
SELECT
embed_text('Hello, world!') IS NOT NULL AS not_null,
vector_dims(embed_text('Hello, world!')) AS dims;Function Signature:
embed_text( text TEXT, -- Input text model_name TEXT DEFAULT NULL -- Optional model name ) RETURNS VECTOR -- Returns embedding vectorSpecify Embedding Model
Generate with specific model
-- Use specific embedding model
SELECT
embed_text('Test text', 'sentence-transformers/all-MiniLM-L6-v2') AS embedding,
vector_dims(embed_text('Test text', 'sentence-transformers/all-MiniLM-L6-v2')) AS dims;
-- Common models:
-- 'all-MiniLM-L6-v2' (default, 384 dimensions)
-- 'sentence-transformers/all-MiniLM-L6-v2' (384 dimensions)
-- 'all-mpnet-base-v2' (768 dimensions)
-- 'text-embedding-ada-002' (1536 dimensions, requires API key)Embedding Consistency
Same text produces the same embedding (cached):
Vector consistency
-- Same text produces same embedding
WITH embeddings AS (
SELECT
embed_text('Consistency test') AS vec1,
embed_text('Consistency test') AS vec2
)
SELECT
vector_dims(vec1) = vector_dims(vec2) AS dims_match,
vec1 <-> vec2 AS distance -- Should be 0 or very close
FROM embeddings;Unicode and Special Characters
Unicode support
-- Unicode and special characters are supported
SELECT
vector_dims(embed_text('Hello δΈη π')) AS unicode_dims,
vector_dims(embed_text('Text with "quotes" and ''apostrophes''')) AS special_chars_dims;Long Text
Long text embedding
-- Long text is automatically handled
SELECT
vector_dims(embed_text(repeat('This is a long text. ', 100))) AS long_text_dims;Batch Generation
Generate embeddings for multiple texts efficiently using batch functions. Batch processing is faster and more efficient than individual calls.
Batch embedding generation
-- Batch embedding generation
-- Returns: array of vectors
SELECT embed_text_batch(
ARRAY['First text', 'Second text', 'Third text']
) AS embeddings;
-- Use with table data
WITH texts AS (
SELECT ARRAY_AGG(content) AS text_array
FROM documents
LIMIT 100
)
SELECT
embed_text_batch(text_array) AS batch_embeddings
FROM texts;
-- Process embeddings from batch
SELECT
unnest(embed_text_batch(ARRAY['text1', 'text2', 'text3'])) AS embedding;Function Signature:
embed_text_batch( texts TEXT[], -- Array of text strings model_name TEXT DEFAULT NULL ) RETURNS VECTOR[] -- Returns array of vectorsPerformance: Batch generation is significantly faster than individual embed_text() calls, especially when GPU acceleration is enabled.
Caching
Embeddings are automatically cached to improve performance. The cache stores embeddings by text content and model name, avoiding redundant computation.
View cache statistics
-- View embedding cache statistics
SELECT * FROM neurondb.embedding_cache_stats;
-- View cached embeddings
SELECT
cache_key,
model_name,
vector_dims(embedding) AS dims,
created_at
FROM neurondb.embedding_cache
ORDER BY created_at DESC
LIMIT 10;
-- Check cache hit rate
SELECT
cache_hits,
cache_misses,
ROUND(100.0 * cache_hits / NULLIF(cache_hits + cache_misses, 0), 2) AS hit_rate_percent
FROM neurondb.embedding_cache_stats;Cache Configuration:
- Cache is enabled by default
- Cache key is based on text content and model name
- Cache size is configurable via
neurondb.embedding_cache_size - Cache automatically evicts least recently used entries when full
Configuration
Configure embedding generation via PostgreSQL GUC settings:
Embedding configuration
-- Set default embedding model
SET neurondb.default_embedding_model = 'all-MiniLM-L6-v2';
-- Configure LLM API key for external models (Hugging Face)
SET neurondb.llm_api_key = 'your-api-key';
-- Enable GPU acceleration for embeddings
SET neurondb.gpu_enabled = true;
-- Configure cache size (number of entries)
SET neurondb.embedding_cache_size = 10000;Note: If LLM is not configured, embed_text() gracefully returns zero vectors as fallback. Configure API keys or enable GPU for real embeddings.
Learn More
For detailed documentation on embedding models, providers, caching strategies, and multimodal embeddings, visit: Embedding Generation Documentation
Related Topics
- Model Management - Manage embedding models
- Vector Search - Index and search embeddings