Matryoshka Embeddings
In this cookbook, we build an Encoderfile that serves Matryoshka sentence embeddings using the nomic-ai/nomic-embed-text-v1.5 model. You’ll package the model into a single, self-contained binary that runs fully offline and can be deployed as a REST API, gRPC service, or CLI.
Along the way, we show how to apply the model’s recommended Matryoshka post-processing and select a fixed embedding dimensionality at build time, making it easier to balance retrieval quality, latency, and memory footprint in production.
Check out the full code in GitHub.
What are Matryoshka Embeddings?
Matryoshka embeddings are embeddings that remain semantically meaningful even when truncated. A single model can produce embeddings at multiple dimensionalities by taking prefixes of the output vector, making it easy to balance retrieval quality against storage and performance constraints in downstream systems.
This Encoderfile is useful when you want to standardize on a fixed embedding size while still benefiting from a Matryoshka-trained model’s training regime. By selecting the embedding dimensionality at build time, you can tailor the binary to your storage, indexing, and memory constraints—then deploy it as a stable, reproducible artifact.
This is a good fit for production search and retrieval systems, offline indexing pipelines, and environments with strict operational or compliance requirements, where embedding shape must be fixed and predictable, and runtime configuration is intentionally limited.
Building the Encoderfile
This is the easiest and most reproducible path. All dependencies are pinned and handled for you.
Step 1: Build the Encoderfile
Run:
This step:
downloads the model artifacts
applies the Matryoshka post-processing configuration
builds the final Encoderfile binary
Step 2: Run the Encoderfile
Run:
The container runs the Encoderfile directly and starts an embedding server. This exposes both an HTTP (port 8080) and a gRPC endpoint (port 50051). To see more options, run:
Use this path if you want full control over the build environment or to inspect each step.
Step 1: Install Prerequisites
Ensure the encoderfile CLI is installed and available in your PATH. For instructions on how to install the encoderfile CLI, check out our Getting Started guide.
To install Huggingface CLI (for downloading model artifacts):
Step 2: Download Model
Run the following:
This script downloads the nomic-ai/nomic-embed-text-v1.5 model files (config.json, tokenizer.json, tokenizer_config.json, and onnx/model.onnx) expected by the Encoderfile build configuration.
Step 3: Build the Encoderfile
Run the following:
This produces a single executable binary, named nomic-embed-text-v1_5.encoderfile. All configuration-model weights, embedding dimensionality, and post-processing logic-is compiled into this file.
Step 4: Run the Encoderfile
To serve the model as a server:
If you get a permission error
Running Inference
You can verify that the server is running by running in a separate terminal:
You should get back the following:
The following Python snippet shows how to extract sentence embeddings:
Last updated