Skip to main content

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:
  1. LLM Context Limits: Language models have token limits (~8K-32K tokens)
  2. Semantic Precision: Smaller chunks provide more precise context
  3. Better Retrieval: Focused chunks improve search relevance

Chunking Strategy in OpenRAG

Example:
Why Overlap?
  • 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:
  1. Sentence Detection: Uses spaCy to detect sentence boundaries
  2. Smart Splitting: Tries to break at sentence boundaries, not mid-sentence
  3. 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
Alternatives (configurable in settings):

Embedding Process

Example Vector Output:

Service Architecture

Docker Service Configuration

API Endpoints

The embedding service exposes a FastAPI server: Port: 8002 (internal only) Endpoints:
  1. POST /embed
    Response:
  2. POST /chunk
    Response:
  3. GET /health

Performance Metrics

Chunking Performance

Test Setup: 31 PDF documents, total 456 pages, ~2.3 MB text content

Embedding 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:
  1. Update EMBEDDING_MODEL in Docker Compose
  2. Restart the embedding service:
  3. Re-index existing documents (they must be embedded with the same model):
Changing models requires re-indexing all documents. Vectors from different models are not compatible.

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:
Expected output:

Troubleshooting

Slow Embedding

Symptom: Embedding takes >10 seconds per document Solutions:
  1. Increase batch size: EMBEDDING_BATCH_SIZE=64
  2. Use smaller model: all-MiniLM-L6-v2 instead of all-mpnet-base-v2
  3. Add GPU support for 5x speedup

Out of Memory

Symptom: Service crashes with CUDA out of memory or Killed Solutions:
  1. Reduce batch size: EMBEDDING_BATCH_SIZE=16
  2. Reduce max sequence length: MAX_SEQUENCE_LENGTH=256
  3. Use smaller model
  4. Increase Docker RAM allocation

Next Steps

Qdrant Vector DB

Learn how embeddings are stored and searched

Ollama LLM

Understand LLM response generation