SentenceTransformersCSharp 1.0.2
dotnet add package SentenceTransformersCSharp --version 1.0.2
NuGet\Install-Package SentenceTransformersCSharp -Version 1.0.2
<PackageReference Include="SentenceTransformersCSharp" Version="1.0.2" />
<PackageVersion Include="SentenceTransformersCSharp" Version="1.0.2" />
<PackageReference Include="SentenceTransformersCSharp" />
paket add SentenceTransformersCSharp --version 1.0.2
#r "nuget: SentenceTransformersCSharp, 1.0.2"
#:package SentenceTransformersCSharp@1.0.2
#addin nuget:?package=SentenceTransformersCSharp&version=1.0.2
#tool nuget:?package=SentenceTransformersCSharp&version=1.0.2
SentenceTransformersCSharp
A .NET Framework 4.6.1 compatible implementation of Sentence Transformers All-MiniLM-L6-v2, supporting .NET Framework 4.6.1+, .NET Standard 2.0, and .NET Standard 2.1.
About This Project
This library provides a C# implementation of the All-MiniLM-L6-v2 sentence transformer model with full support for legacy .NET Framework environments. It includes comprehensive helper utilities and documentation for resolving common ONNX Runtime issues in .NET Framework 4.6.1+ applications.
Inspiration
This project is inspired by the original AllMiniLML6v2Sharp by ksanman, but has been completely rewritten and enhanced for .NET Framework 4.6.1 compatibility while maintaining API compatibility.
Key Features
- ✅ Full .NET Framework 4.6.1 compatibility
- ✅ .NET Standard 2.0 and 2.1 support
- ✅ Includes tokenizer and ONNX model
- ✅ ONNX Runtime initialization helpers for legacy environments
- ✅ Comprehensive documentation for resolving common issues
- ✅ Backward compatible with original API
Target Frameworks
- .NET Framework 4.6.1+
- .NET Standard 2.0
- .NET Standard 2.1
- .NET Core 2.0+
- .NET 5.0+
NuGet Package
Package: SentenceTransformersCSharp
Original Package: AllMiniLML6v2Sharp
The Nuget does not include the onnx model or the vocab.txt. These can be found on Hugging Face (See tested models below).
The Embedder looks for the default model.onnx and vocab.txt files in the .\model
folder.
You may use a custom onnx model or custom vocab as well.
How to use
- Single Sentence
var sentence = "This is an example sentence";
using var embedder = new AllMiniLmL6V2Embedder();
var embedding = embedder.GenerateEmbedding(sentence);
- Multiple Sentences
string[] sentences = ["This is an example sentence", "Here is another"];
using var embedder = new AllMiniLmL6V2Embedder();
var embeddings = model.GenerateEmbeddings(sentences);
- Custom All-MiniLM-L6-v2 onnx model
var sentence = "This is an example sentence";
using var embedder = new AllMiniLmL6V2Embedder(modelPath: "path/to/model.onnx");
var embedding = embedder.GenerateEmbedding(sentence);
- Custom vocab
var sentence = "This is an example sentence";
BertTokenizer tokenizer = new("path/to/vocab.txt");
using var embedder = new AllMiniLmL6V2Embedder(tokenizer: tokenizer);
var embedding = embedder.GenerateEmbedding(sentence);
- Custom Tokenizer
var sentence = "This is an example sentence";
ITokenizer tokenizer = new CustomTokenizer();
using var embedder = new AllMiniLmL6V2Embedder(tokenizer: tokenizer);
var embedding = embedder.GenerateEmbedding(sentence);
Tested Models
.NET Framework 4.6.1 Specific Information
ONNX Runtime Initialization
For .NET Framework 4.6.1 projects, you may encounter ONNX Runtime initialization issues. This library includes helper classes to resolve these:
// Option 1: Use SafeAllMiniLmL6V2Embedder (recommended)
using (var embedder = new SafeAllMiniLmL6V2Embedder())
{
var embedding = embedder.GenerateEmbedding("Your text");
}
// Option 2: Manual initialization
OnnxRuntimeInitializer.EnsureInitialized();
using (var embedder = new AllMiniLmL6V2Embedder())
{
var embedding = embedder.GenerateEmbedding("Your text");
}
Troubleshooting
If you encounter issues with .NET Framework 4.6.1:
- GPU Conflicts: Run the included
FIX_GPU_CONFLICT.ps1
PowerShell script - ONNX Runtime Issues: See
INITIALIZE_ONNX_RUNTIME.md
for detailed solutions - Package Conflicts: Refer to
ONNX_RUNTIME_FIX.md
for resolution steps
License
This project maintains the original MIT License. See LICENSE file for details.
Acknowledgments
- Original project by ksanman
- Based on Sentence Transformers
- Uses ONNX Runtime for model inference
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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 was computed. 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 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.1
- libtorch-cpu-win-x64 (>= 2.1.0.1)
- Microsoft.ML (>= 3.0.0)
- Microsoft.ML.OnnxRuntime (>= 1.12.1)
- Microsoft.ML.OnnxRuntime.Managed (>= 1.12.1)
-
.NETStandard 2.0
- libtorch-cpu-win-x64 (>= 2.1.0.1)
- Microsoft.ML (>= 3.0.0)
- Microsoft.ML.OnnxRuntime (>= 1.12.1)
- Microsoft.ML.OnnxRuntime.Managed (>= 1.12.1)
-
.NETStandard 2.1
- libtorch-cpu-win-x64 (>= 2.1.0.1)
- Microsoft.ML (>= 3.0.0)
- Microsoft.ML.OnnxRuntime (>= 1.12.1)
- Microsoft.ML.OnnxRuntime.Managed (>= 1.12.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version 1.0.2 - Updated library namespace to match package name. Changes include: Renamed namespace from AllMiniLmL6V2Sharp to SentenceTransformersCSharp, updated assembly name to SentenceTransformersCSharp, updated all source files to use the new namespace, and improved consistency between package name and library name. This is a breaking change for existing code - update your using statements from 'using AllMiniLmL6V2Sharp' to 'using SentenceTransformersCSharp'.