Embedding & Chunking Service
The Embedding service is responsible for transforming document text into vector representations that can be searched semantically. It handles two critical tasks: chunking (splitting documents into manageable pieces) and embedding (converting text to vectors).What is Chunking?
Chunking is the process of splitting large documents into smaller, overlapping segments of text. This is essential for RAG systems because:- LLM Context Limits: Language models have token limits (~8K-32K tokens)
- Semantic Precision: Smaller chunks provide more precise context
- Better Retrieval: Focused chunks improve search relevance
Chunking Strategy in OpenRAG
- Ensures important information isn’t split mid-sentence
- Provides context continuity between chunks
- Improves retrieval accuracy by avoiding boundary effects
Chunking Method
OpenRAG uses recursive character splitting with sentence awareness:- Sentence Detection: Uses spaCy to detect sentence boundaries
- Smart Splitting: Tries to break at sentence boundaries, not mid-sentence
- Metadata Preservation: Each chunk retains document metadata (filename, page, position)
What is Embedding?
Embedding is the process of converting text into a numerical vector representation. Similar texts produce similar vectors, enabling semantic search.Model Used: sentence-transformers/all-MiniLM-L6-v2
Characteristics:- Dimensions: 384 (each text becomes a 384-dimensional vector)
- Model Size: 80 MB
- Speed: ~1000 sentences/second on CPU
- Quality: Good balance between speed and accuracy
- Language: Optimized for English, works reasonably on other languages
Embedding Process
Example Vector Output:Service Architecture
Docker Service Configuration
API Endpoints
The embedding service exposes a FastAPI server: Port: 8002 (internal only) Endpoints:-
POST /embed
Response:
-
POST /chunk
Response:
-
GET /health
Performance Metrics
Chunking Performance
Test Setup: 31 PDF documents, total 456 pages, ~2.3 MB text contentEmbedding Performance
Hardware: CPU-only mode (no GPU)
With GPU (NVIDIA RTX 3060):
Configuration Options
Environment Variables
Changing the Embedding Model
To use a different model:-
Update
EMBEDDING_MODELin Docker Compose -
Restart the embedding service:
-
Re-index existing documents (they must be embedded with the same model):
Advanced: Custom Chunking Strategies
For specialized use cases, you can implement custom chunking:Semantic Chunking
Split based on semantic similarity rather than fixed size:Hierarchical Chunking
Create parent-child chunk relationships:Monitoring
View embedding service logs:Troubleshooting
Slow Embedding
Symptom: Embedding takes >10 seconds per document Solutions:- Increase batch size:
EMBEDDING_BATCH_SIZE=64 - Use smaller model:
all-MiniLM-L6-v2instead ofall-mpnet-base-v2 - Add GPU support for 5x speedup
Out of Memory
Symptom: Service crashes withCUDA out of memory or Killed
Solutions:
- Reduce batch size:
EMBEDDING_BATCH_SIZE=16 - Reduce max sequence length:
MAX_SEQUENCE_LENGTH=256 - Use smaller model
- Increase Docker RAM allocation
Next Steps
Qdrant Vector DB
Learn how embeddings are stored and searched
Ollama LLM
Understand LLM response generation

