Embeddings
Understand how embeddings work and configure embedding models for your knowledge base.
Overview
Embeddings are numerical representations of text that capture semantic meaning. They enable your AI agent to find relevant information based on meaning, not just keyword matching.
How Embeddings Work
┌─────────────────────────────────────────────────────────┐
│ Text Input │
│ "How do I reset my password?" │
└─────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ Embedding Model │
│ (Transforms text into numerical vector) │
└─────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ Vector (1536 dimensions) │
│ [0.023, -0.041, 0.089, ..., 0.012] │
└─────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ Similarity Search │
│ Find chunks with similar vectors │
└─────────────────────────────────────────────────────────┘
Supported Embedding Models
OpenAI Models
| Model | Dimensions | Max Tokens | Cost | Quality |
|---|---|---|---|---|
| text-embedding-3-large | 3072 | 8191 | $$$ | Best |
| text-embedding-3-small | 1536 | 8191 | $ | Good |
| text-embedding-ada-002 | 1536 | 8191 | $ | Good |
Configuration:
json{ "embeddings": { "provider": "openai", "model": "text-embedding-3-small", "dimensions": 1536 } }
Anthropic/Voyage Models
| Model | Dimensions | Max Tokens | Cost | Quality |
|---|---|---|---|---|
| voyage-large-2 | 1536 | 16000 | $$ | Excellent |
| voyage-code-2 | 1536 | 16000 | $$ | Best for code |
| voyage-lite-02 | 1024 | 4000 | $ | Good |
Configuration:
json{ "embeddings": { "provider": "voyage", "model": "voyage-large-2", "dimensions": 1536 } }
Cohere Models
| Model | Dimensions | Max Tokens | Cost | Quality |
|---|---|---|---|---|
| embed-english-v3.0 | 1024 | 512 | $$ | Excellent |
| embed-multilingual-v3.0 | 1024 | 512 | $$ | Best multilingual |
| embed-english-light-v3.0 | 384 | 512 | $ | Good |
Configuration:
json{ "embeddings": { "provider": "cohere", "model": "embed-english-v3.0", "dimensions": 1024 } }
Local Models (Ollama)
| Model | Dimensions | Quality | Notes |
|---|---|---|---|
| nomic-embed-text | 768 | Good | Fast, lightweight |
| mxbai-embed-large | 1024 | Excellent | High quality |
| all-minilm | 384 | Moderate | Very fast |
Configuration:
json{ "embeddings": { "provider": "ollama", "model": "nomic-embed-text", "base_url": "http://localhost:11434", "dimensions": 768 } }
Model Selection Guide
By Use Case
| Use Case | Recommended Model | Why |
|---|---|---|
| General knowledge | text-embedding-3-small | Good balance of cost/quality |
| Technical docs | voyage-code-2 | Optimized for code/technical |
| Multilingual | embed-multilingual-v3.0 | Best cross-language support |
| High accuracy | text-embedding-3-large | Highest quality |
| Cost-sensitive | all-minilm (local) | No API costs |
| Privacy-focused | nomic-embed-text (local) | Data stays on-premise |
By Budget
| Budget | Model | Monthly Cost (1M tokens) |
|---|---|---|
| Free | Local models | $0 (compute only) |
| Low | text-embedding-3-small | ~$2 |
| Medium | text-embedding-3-large | ~$13 |
| Enterprise | voyage-large-2 | ~$12 |
Configuring Embeddings
Via UI
- Go to Agents → Select agent → Knowledge Base
- Click Settings → Embedding Model
- Select provider and model
- Click Save (existing documents will be re-embedded)
Via API
bashcurl -X PATCH "https://api.arcanflows.com/api/v1/agents/{agent_id}/knowledge/settings" \ -H "X-API-Key: your_api_key" \ -H "Content-Type: application/json" \ -d '{ "embeddings": { "provider": "openai", "model": "text-embedding-3-small", "dimensions": 1536, "batch_size": 100 } }'
Vector Storage
Supported Vector Databases
Arcanflows supports multiple vector storage backends:
| Database | Type | Best For |
|---|---|---|
| pgvector | PostgreSQL extension | Default, integrated |
| Pinecone | Managed service | Large scale, production |
| Qdrant | Self-hosted/managed | High performance |
| Weaviate | Self-hosted/managed | Hybrid search |
| ChromaDB | Embedded | Development, small scale |
Default Configuration (pgvector)
json{ "vector_store": { "type": "pgvector", "index_type": "ivfflat", "lists": 100, "probes": 10 } }
Index Types
| Index | Speed | Recall | Memory | Best For |
|---|---|---|---|---|
| Flat | Slow | 100% | High | Small datasets |
| IVFFlat | Fast | ~95% | Medium | General use |
| HNSW | Very fast | ~99% | High | Large datasets |
Similarity Search
Distance Metrics
| Metric | Formula | Use Case |
|---|---|---|
| Cosine | 1 - cos(θ) | Most common, normalized |
| Euclidean | L2 distance | Raw distances |
| Dot Product | a · b | When magnitudes matter |
Default: Cosine similarity
Search Configuration
json{ "search": { "metric": "cosine", "top_k": 5, "score_threshold": 0.7, "rerank": true, "rerank_model": "cross-encoder" } }
| Parameter | Description | Default |
|---|---|---|
top_k | Number of results to return | 5 |
score_threshold | Minimum similarity score | 0.7 |
rerank | Use reranking model | false |
Retrieval Strategies
Basic Similarity Search
python# Pseudocode results = vector_store.similarity_search( query_embedding, k=5, threshold=0.7 )
Hybrid Search (Vector + Keyword)
Combines semantic and keyword search:
json{ "search": { "type": "hybrid", "vector_weight": 0.7, "keyword_weight": 0.3, "keyword_method": "bm25" } }
Benefits:
- Better for exact matches (names, codes)
- Handles both semantic and keyword queries
- More robust retrieval
Reranking
Apply a second-stage model to improve results:
json{ "search": { "rerank": true, "rerank_model": "cohere-rerank-english-v2.0", "rerank_top_n": 10, "final_top_k": 3 } }
How it works:
- Retrieve top 10 candidates via vector search
- Rerank using cross-encoder model
- Return top 3 reranked results
Maximal Marginal Relevance (MMR)
Diversifies results to reduce redundancy:
json{ "search": { "type": "mmr", "lambda": 0.5, "fetch_k": 20, "final_k": 5 } }
| Parameter | Description |
|---|---|
lambda | Balance relevance (1.0) vs diversity (0.0) |
fetch_k | Candidates to fetch |
final_k | Final results after MMR |
Embedding Best Practices
1. Consistent Models
Use the same embedding model for documents and queries:
Documents: text-embedding-3-small → vectors
Queries: text-embedding-3-small → vectors ✓
Documents: text-embedding-3-small → vectors
Queries: text-embedding-ada-002 → vectors ✗
2. Preprocessing
Clean text before embedding:
pythondef preprocess(text): # Remove extra whitespace text = ' '.join(text.split()) # Remove special characters (optional) text = text.replace('\n', ' ') # Truncate to model max length text = text[:8000] return text
3. Query Enhancement
Improve query embeddings:
json{ "query_enhancement": { "expand_query": true, "add_context": true, "hypothetical_answer": false } }
HyDE (Hypothetical Document Embedding):
- Generate hypothetical answer to query
- Embed the hypothetical answer
- Search for similar real documents
4. Monitoring
Track embedding performance:
| Metric | Description | Target |
|---|---|---|
| Avg similarity score | Query-result similarity | > 0.75 |
| Result diversity | Unique topics in results | > 0.5 |
| Latency | Search time | < 200ms |
| Recall@k | Relevant in top k | > 0.9 |
API Reference
Generate Embedding
bashcurl -X POST "https://api.arcanflows.com/api/v1/embeddings" \ -H "X-API-Key: your_api_key" \ -H "Content-Type: application/json" \ -d '{ "input": "How do I reset my password?", "model": "text-embedding-3-small" }'
Response:
json{ "embedding": [0.023, -0.041, 0.089, ...], "dimensions": 1536, "tokens_used": 8 }
Search Knowledge Base
bashcurl -X POST "https://api.arcanflows.com/api/v1/agents/{agent_id}/knowledge/search" \ -H "X-API-Key: your_api_key" \ -H "Content-Type: application/json" \ -d '{ "query": "password reset process", "top_k": 5, "threshold": 0.7, "include_metadata": true }'
Troubleshooting
Low similarity scores
- Check embedding model consistency
- Improve chunk quality
- Try hybrid search
Irrelevant results
- Lower score threshold
- Enable reranking
- Add keyword search component
Slow search
- Add vector index (HNSW)
- Reduce top_k
- Optimize chunk sizes
High costs
- Switch to smaller embedding model
- Batch embedding requests
- Use local models for development