E5Embedding.Net
3.0.0
dotnet add package E5Embedding.Net --version 3.0.0
NuGet\Install-Package E5Embedding.Net -Version 3.0.0
<PackageReference Include="E5Embedding.Net" Version="3.0.0" />
<PackageVersion Include="E5Embedding.Net" Version="3.0.0" />
<PackageReference Include="E5Embedding.Net" />
paket add E5Embedding.Net --version 3.0.0
#r "nuget: E5Embedding.Net, 3.0.0"
#:package E5Embedding.Net@3.0.0
#addin nuget:?package=E5Embedding.Net&version=3.0.0
#tool nuget:?package=E5Embedding.Net&version=3.0.0
E5Embedding.Net
High-performance .NET library for generating text embeddings using E5 models with ONNX Runtime, supporting CUDA, DirectML, and automatic CPU fallback.
E5Embedding.Net provides a simple and production-ready API for integrating modern embedding models into .NET applications.
Designed for:
- Semantic Search
- Retrieval Augmented Generation (RAG)
- Vector Databases
- Document Similarity
- Recommendation Systems
- AI-powered Search
Features
🚀 High Performance
- Optimized ONNX Runtime inference
- Efficient batch processing
- Async embedding generation
🎯 E5 Model Support
- Built specifically for E5 embedding models
- Supports retrieval-style embeddings (
query:/passage:)
💻 GPU Acceleration
- NVIDIA CUDA support
- Windows DirectML support
- Automatic CPU fallback
🔧 Flexible Tokenization
- SentencePiece tokenizer support
- BERT WordPiece tokenizer support
📦 Easy Integration
- Simple API
- Dependency Injection support
- Microsoft.Extensions.Logging integration
🛡️ Production Ready
- Resource management
- Validation
- Error handling
- Logging support
Installation
Install via .NET CLI:
dotnet add package E5Embedding.Net
Or via Package Manager Console:
Install-Package E5Embedding.Net
Architecture
The processing flow of the pipeline:
Text Input
|
v
Tokenizer
|
v
Token IDs + Attention Mask
|
v
ONNX Runtime
|
v
Embedding Vector
Quick Start
using E5Embedding.Net;
var config = new E5EmbeddingConfiguration
{
OnnxModelPath = "./E5/model.onnx",
SentencePieceModelFile = "./E5/sentencepiece.bpe.model",
TokenizerConfigFile = "./E5/tokenizer_config.json",
TokenizerJsonFile = "./E5/tokenizer.json",
MaxSequenceLength = 512,
Dimension = 1024,
BatchSize = 16
};
using var embeddingService = new OnnxEmbeddingService(config);
var embedding = await embeddingService.EmbedAsync("This is a sample text.");
Console.WriteLine($"Embedding size: {embedding.Length}");
Batch Embeddings
For multiple documents, use batch processing:
var documents = new[]
{
"Document one",
"Document two",
"Document three"
};
var embeddings = await embeddingService.EmbedBatchAsync(documents);
Note: Batch processing improves throughput by reducing inference overhead.
Retrieval Example
E5 models are optimized for retrieval scenarios using prefixes:
- Query:
query: <your search query> - Passage:
passage: <your document content>
var queryEmbedding = await service.EmbedAsync(
"query: What is machine learning?"
);
var passageEmbedding = await service.EmbedAsync(
"passage: Machine learning is a branch of AI..."
);
Dependency Injection
Example registration:
services.AddSingleton<E5EmbeddingConfiguration>(sp =>
{
return new E5EmbeddingConfiguration
{
OnnxModelPath = "./model.onnx",
MaxSequenceLength = 512,
Dimension = 1024,
BatchSize = 16
};
});
services.AddSingleton<IEmbeddingService>(sp =>
{
var config = sp.GetRequiredService<E5EmbeddingConfiguration>();
var logger = sp.GetService<ILogger<OnnxEmbeddingService>>();
return new OnnxEmbeddingService(config, logger);
});
Configuration
E5EmbeddingConfiguration
| Property | Type | Description | Default |
|---|---|---|---|
OnnxModelPath |
string |
ONNX model location | Required |
SentencePieceModelFile |
string |
SentencePiece model file | sentencepiece.bpe.model |
TokenizerConfigFile |
string |
Tokenizer configuration | tokenizer_config.json |
TokenizerJsonFile |
string |
Tokenizer metadata | tokenizer.json |
MaxSequenceLength |
int |
Maximum tokens | Required |
Dimension |
int |
Embedding dimension | 1024 |
BatchSize |
int |
Batch processing size | 16 |
GPU Acceleration
E5Embedding.Net automatically selects the best available execution provider in the following order:
- CUDA
- DirectML
- CPU
No additional configuration is required. The selected provider is reported through logging.
Tokenizers
SentencePieceTokenizer
Recommended for E5 models.
var tokenizer = new SentencePieceTokenizer(
"sentencepiece.bpe.model",
"tokenizer_config.json",
"tokenizer.json",
512
);
var encoding = tokenizer.Encode("Hello world");
BertTokenizer
Supports BERT-style WordPiece tokenization.
var tokenizer = new BertTokenizer(
"tokenizer_config.json",
"tokenizer.json",
512
);
var result = tokenizer.Encode("Example text");
Supported Models
Currently tested with intfloat/multilingual-e5-large.
Supported ONNX variants:
model.onnxmodel.onnx_datamodel_O4.onnxmodel_qint8_avx512_vnni.onnx
Model Files
Required files:
model.onnxmodel.onnx_datasentencepiece.bpe.modeltokenizer.jsontokenizer_config.json
Requirements
- .NET 8.0+
- ONNX Runtime
- E5 ONNX model files & Tokenizer files
Supported Platforms:
- Windows / Linux
- GPU: NVIDIA CUDA / DirectML compatible GPUs
Performance Tips
- Reuse the Service:
Create one instance and reuse it as a singleton. The ONNX session is expensive to initialize.
services.AddSingleton<IEmbeddingService, OnnxEmbeddingService>(); - Use Batch Processing:
Prefer
EmbedBatchAsync()for multiple texts. - Dispose Resources:
Always dispose of the service when finished:
using var service = new OnnxEmbeddingService(config);
Error Handling
Common exceptions:
| Exception | Description |
|---|---|
ArgumentNullException |
Missing required arguments |
FileNotFoundException |
Model or tokenizer files missing |
InvalidOperationException |
Invalid configuration |
AggregateException |
GPU and CPU initialization failure |
Roadmap
- More E5 model variants
- Native AOT support
- Memory pooling optimization
- Additional quantized models
- Streaming embedding API
- Built-in similarity utilities
Contributing
Contributions are welcome! Feel free to open issues or submit pull requests.
License
MIT License. See LICENSE for details.
Support
For issues, discussions, and contributions, visit the GitHub Repository.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.8)
- Microsoft.ML.OnnxRuntime.Gpu (>= 1.26.0)
- Microsoft.ML.Tokenizers (>= 2.0.0)
-
net8.0
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.8)
- Microsoft.ML.OnnxRuntime.Gpu (>= 1.26.0)
- Microsoft.ML.Tokenizers (>= 2.0.0)
-
net9.0
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.8)
- Microsoft.ML.OnnxRuntime.Gpu (>= 1.26.0)
- Microsoft.ML.Tokenizers (>= 2.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial stable release of E5Embedding.Net with E5 ONNX embedding support,
GPU acceleration and tokenizer support.