EFCore.SqlServer.VectorSearch 0.1.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package EFCore.SqlServer.VectorSearch --version 0.1.1                
NuGet\Install-Package EFCore.SqlServer.VectorSearch -Version 0.1.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EFCore.SqlServer.VectorSearch" Version="0.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EFCore.SqlServer.VectorSearch --version 0.1.1                
#r "nuget: EFCore.SqlServer.VectorSearch, 0.1.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EFCore.SqlServer.VectorSearch as a Cake Addin
#addin nuget:?package=EFCore.SqlServer.VectorSearch&version=0.1.1

// Install EFCore.SqlServer.VectorSearch as a Cake Tool
#tool nuget:?package=EFCore.SqlServer.VectorSearch&version=0.1.1                

EFCore.SqlServer.VectorSearch

[!IMPORTANT]
This plugin is in prerelease status, and the APIs described below are likely to change before the final release. Usage of this plugin requires the vector support feature in Azure SQL Database, currently in EAP. See this blog post for more details.

This Entity Framework Core plugin provides integration between EF and Vector Support in Azure SQL Database, allowing LINQ to be used to perform vector similarity search, and seamless insertion/retrieval of vector data.

To use the plugin, reference the EFCore.SqlServer.VectorSearch nuget package, and enable the plugin by adding UseVectorSearch() to your UseSqlServer() config as follows:

builder.Services.AddDbContext<ProductContext>(options =>
  options.UseSqlServer("<connection string>", o => o.UseVectorSearch()));

Once the plugin has been enable, add an ordinary float[] property to the .NET type being mapped with EF:

public class Product
{
    public int Id { get; set; }
    public float[] Embedding { get; set; }
}

Finally, configure the property to be mapped as a vector by applying IsVector():

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Product>().Property(p => p.Embedding).IsVector();
}

That's it - you can now perform similarity search in LINQ queries! For example, to get the top 5 most similar products:

var someVector = new[] { 1f, 2f, 3f };
var products = await context.Products
    .OrderBy(p => EF.Functions.VectorDistance("cosine", p.Embedding, vector))
    .Take(5)
    .ToArrayAsync();

To get the number of dimensions of a vector, use EF.Functions.VectorDimensions():

var dimensions = await context.Products
    .Where(p => p.Id == 1)
    .Select(p => EF.Functions.VectorDimensions(p.Embedding))
    .SingleAsync();

Finally, the JsonArrayToVector() and VectorToJsonArray() functions allow you to convert a varchar value containing a JSON array to a vector, and vice versa. These functions generally shouldn't be needed, as you can work with float[] directly, and the EF plugin will perform the conversions for you.

Ideas? Issues? Let us know on the github repo.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.2.0 189 10/30/2024
0.1.1 489 5/30/2024
0.1.0 123 5/24/2024