Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/efcore/EFCore.SqlServer.VectorSearch
https://github.com/efcore/EFCore.SqlServer.VectorSearch
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/efcore/EFCore.SqlServer.VectorSearch
- Owner: efcore
- License: mit
- Created: 2024-05-24T17:52:37.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-11-08T17:08:55.000Z (2 months ago)
- Last Synced: 2024-11-08T18:20:26.458Z (2 months ago)
- Language: C#
- Size: 35.2 KB
- Stars: 11
- Watchers: 2
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-semantickernel - EFCore.SqlServer.VectorSearch
README
# EFCore.SqlServer.VectorSearch
> [!IMPORTANT]
> This plugin is in prerelease status, and the APIs described below are likely to change before the final release.
> Vector Functions are in Public Preview. Learn the details about vectors in Azure SQL here: https://aka.ms/azure-sql-vector-public-previewThis 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](https://www.nuget.org/packages/EFCore.SqlServer.VectorSearch) nuget package, and enable the plugin by adding `UseVectorSearch()` to your `UseSqlServer()` config as follows:
```c#
builder.Services.AddDbContext(options =>
options.UseSqlServer("", o => o.UseVectorSearch()));
```Once the plugin has been enabled, add an ordinary `float[]` property to the .NET type being mapped with EF:
```c#
public class Product
{
public int Id { get; set; }
public float[] Embedding { get; set; }
}
```Finally, configure the property to be mapped as a vector by letting EF Core know using the `HasColumnType` method. Use the `vector` type and specify the number of dimension that your vector will have:
```c#
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity().Property(p => p.Embedding).HasColumnType("vector(3)");
}
```That's it - you can now perform similarity search in LINQ queries! For example, to get the top 5 most similar products:
```c#
var someVector = new[] { 1f, 2f, 3f };
var products = await context.Products
.OrderBy(p => EF.Functions.VectorDistance("cosine", p.Embedding, vector))
.Take(5)
.ToArrayAsync();
```A full sample using EF Core and vectors is available here:
https://github.com/Azure-Samples/azure-sql-db-vector-search/tree/main/EF-Core
Ideas? Issues? Let us know on the [issues page](https://github.com/efcore/EFCore.SqlServer.VectorSearch/issues).