Contoso.AI.TextEmbedder.MiniLML6
0.1.6-beta
dotnet add package Contoso.AI.TextEmbedder.MiniLML6 --version 0.1.6-beta
NuGet\Install-Package Contoso.AI.TextEmbedder.MiniLML6 -Version 0.1.6-beta
<PackageReference Include="Contoso.AI.TextEmbedder.MiniLML6" Version="0.1.6-beta" />
<PackageVersion Include="Contoso.AI.TextEmbedder.MiniLML6" Version="0.1.6-beta" />
<PackageReference Include="Contoso.AI.TextEmbedder.MiniLML6" />
paket add Contoso.AI.TextEmbedder.MiniLML6 --version 0.1.6-beta
#r "nuget: Contoso.AI.TextEmbedder.MiniLML6, 0.1.6-beta"
#:package Contoso.AI.TextEmbedder.MiniLML6@0.1.6-beta
#addin nuget:?package=Contoso.AI.TextEmbedder.MiniLML6&version=0.1.6-beta&prerelease
#tool nuget:?package=Contoso.AI.TextEmbedder.MiniLML6&version=0.1.6-beta&prerelease
Contoso.AI.TextEmbedder.MiniLML6
AI-powered text embedding library using the all-MiniLM-L6-v2 ONNX model from sentence-transformers. Converts text into 384-dimensional embeddings for semantic search, similarity tasks, clustering, and more.
โจ Features
- 384-Dimensional Embeddings - Rich semantic representations for text
- Automatic Model Download - Model downloads automatically at build time
- GPU Acceleration - Leverages DirectML for faster inference when available
- Easy-to-Use API - Simple async factory pattern with
CreateAsync() - Cosine Similarity - Built-in similarity calculation between embeddings
- Semantic Search - Find semantically similar texts
- NuGet Package - Easy integration into any .NET project
๐ Requirements
- Windows 10 SDK 19041 or later
- .NET 8.0 or later
- GPU (optional) - DirectML-compatible GPU for hardware acceleration
- Falls back to CPU execution if GPU is not available
๐ Quick Start
Installation
dotnet add package Contoso.AI.TextEmbedder.MiniLML6
Or via Package Manager Console:
Install-Package Contoso.AI.TextEmbedder.MiniLML6
Basic Usage
using Contoso.AI;
// Check if the feature is ready
var readyState = TextEmbedderMiniLML6.GetReadyState();
if (readyState != AIFeatureReadyState.Ready)
{
// Prepare the feature (downloads model if needed)
var readyResult = await TextEmbedderMiniLML6.EnsureReadyAsync();
if (readyResult.Status != AIFeatureReadyResultState.Success)
{
Console.WriteLine($"Failed to initialize: {readyResult.ExtendedError?.Message}");
return;
}
}
// Create embedder instance (returns ITextEmbedder interface)
using ITextEmbedder embedder = await TextEmbedderMiniLML6.CreateAsync();
// Generate embeddings
var texts = new[]
{
"The quick brown fox jumps over the lazy dog",
"Machine learning is transforming technology"
};
var embeddings = embedder.GenerateEmbeddings(texts);
// Calculate similarity
var similarity = embeddings[0].CosineSimilarity(embeddings[1]);
Console.WriteLine($"Similarity: {similarity:F3}");
Semantic Search Example
using Contoso.AI;
using ITextEmbedder embedder = await TextEmbedderMiniLML6.CreateAsync();
// Index some documents
var documents = new[]
{
"Python is a programming language",
"Cats make great pets",
"Machine learning uses neural networks",
"Dogs are loyal companions"
};
var docEmbeddings = embedder.GenerateEmbeddings(documents);
// Search with a query
var query = "Domestic animals";
var queryEmbedding = embedder.GenerateEmbeddings(query)[0];
// Find most similar documents
var results = docEmbeddings
.Select((emb, idx) => new {
Document = documents[idx],
Score = queryEmbedding.CosineSimilarity(emb)
})
.OrderByDescending(x => x.Score)
.ToList();
foreach (var result in results)
{
Console.WriteLine($"{result.Score:F3} - {result.Document}");
}
๐ API Reference
TextEmbedderMiniLML6 Class
| Method | Description |
|---|---|
GetReadyState() |
Static. Returns AIFeatureReadyState indicating if the feature can be used |
EnsureReadyAsync() |
Static. Checks for required dependencies |
CreateAsync() |
Static factory. Creates and initializes a new ITextEmbedder instance |
ITextEmbedder Interface
| Method | Description |
|---|---|
GenerateEmbeddings(params string[]) |
Synchronously generates embeddings for texts |
GenerateEmbeddingsAsync(IEnumerable<string>, CancellationToken) |
Asynchronously generates embeddings for texts |
Embedding Class
| Property/Method | Description |
|---|---|
Vector |
Gets the embedding as a float array |
Dimensions |
Gets the dimensionality (384 for MiniLM-L6-v2) |
CosineSimilarity(Embedding) |
Calculates cosine similarity with another embedding (returns -1 to 1) |
๐๏ธ Development
Building from Source
# Clone the repository
git clone https://github.com/contoso/Contoso.AI.git
cd Contoso.AI/TextEmbedder
# Restore and build (model downloads automatically)
dotnet restore
dotnet build
# Run the console test
dotnet run --project Contoso.AI.TextEmbedder.MiniLML6.ConsoleTest
Model Information
This project uses the all-MiniLM-L6-v2 model from sentence-transformers:
- Source: sentence-transformers/all-MiniLM-L6-v2 on Hugging Face
- Commit: d83dd3760b5bfe921f2fe125446b17bf0b7eda8c
- Format: ONNX
- Output Dimensions: 384
- License: Apache 2.0
The model is automatically downloaded during the first build and cached in the obj/Models directory.
๐งช Use Cases
- Semantic Search - Find documents similar to a query
- Text Clustering - Group similar texts together
- Duplicate Detection - Identify similar or duplicate content
- Recommendation Systems - Recommend similar items based on text descriptions
- Question Answering - Find answers similar to questions
- Content Classification - Classify text by similarity to categories
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- sentence-transformers for the all-MiniLM-L6-v2 model
- ONNX Runtime for the inference engine
- Microsoft AI Dev Gallery for embedding generation code samples
Made with โค๏ธ by Contoso
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0-windows10.0.19041 is compatible. net9.0-windows was computed. net10.0-windows was computed. |
-
net8.0-windows10.0.19041
- Contoso.AI.AIFeatureCore (>= 0.0.1-beta)
- Contoso.AI.TextEmbedder (>= 0.1.6-beta)
- Microsoft.ML.OnnxRuntimeGenAI (>= 0.7.0-rc1)
- Microsoft.ML.Tokenizers (>= 1.0.0)
- Microsoft.WindowsAppSDK (>= 1.8.251106002)
- System.Numerics.Tensors (>= 9.0.9)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.1.6-beta | 34 | 2/19/2026 |