![]() |
VOOZH | about |
dotnet add package Pgvector --version 0.3.2
NuGet\Install-Package Pgvector -Version 0.3.2
<PackageReference Include="Pgvector" Version="0.3.2" />
<PackageVersion Include="Pgvector" Version="0.3.2" />Directory.Packages.props
<PackageReference Include="Pgvector" />Project file
paket add Pgvector --version 0.3.2
#r "nuget: Pgvector, 0.3.2"
#:package Pgvector@0.3.2
#addin nuget:?package=Pgvector&version=0.3.2Install as a Cake Addin
#tool nuget:?package=Pgvector&version=0.3.2Install as a Cake Tool
pgvector support for .NET (C#, F#, and Visual Basic)
Supports Npgsql, Dapper, Entity Framework Core, and Npgsql.FSharp
Follow the instructions for your database library:
Or check out some examples:
COPYRun
dotnet add package Pgvector
Create a connection
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connString);
dataSourceBuilder.UseVector();
await using var dataSource = dataSourceBuilder.Build();
var conn = dataSource.OpenConnection();
Enable the extension
await using (var cmd = new NpgsqlCommand("CREATE EXTENSION IF NOT EXISTS vector", conn))
{
await cmd.ExecuteNonQueryAsync();
}
conn.ReloadTypes();
Create a table
await using (var cmd = new NpgsqlCommand("CREATE TABLE items (id serial PRIMARY KEY, embedding vector(3))", conn))
{
await cmd.ExecuteNonQueryAsync();
}
Insert a vector
await using (var cmd = new NpgsqlCommand("INSERT INTO items (embedding) VALUES ($1)", conn))
{
var embedding = new Vector(new float[] { 1, 1, 1 });
cmd.Parameters.AddWithValue(embedding);
await cmd.ExecuteNonQueryAsync();
}
Get the nearest neighbors
await using (var cmd = new NpgsqlCommand("SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5", conn))
{
var embedding = new Vector(new float[] { 1, 1, 1 });
cmd.Parameters.AddWithValue(embedding);
await using (var reader = await cmd.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
Console.WriteLine(reader.GetValue(0));
}
}
}
Add an approximate index
await using (var cmd = new NpgsqlCommand("CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)", conn))
{
await cmd.ExecuteNonQueryAsync();
}
Use vector_ip_ops for inner product and vector_cosine_ops for cosine distance
See a full example
Run
dotnet add package Pgvector.Dapper
Import the library
using Pgvector.Dapper;
Create a connection
SqlMapper.AddTypeHandler(new VectorTypeHandler());
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connString);
dataSourceBuilder.UseVector();
await using var dataSource = dataSourceBuilder.Build();
var conn = dataSource.OpenConnection();
Enable the extension
conn.Execute("CREATE EXTENSION IF NOT EXISTS vector");
conn.ReloadTypes();
Define a class
public class Item
{
public int Id { get; set; }
public Vector? Embedding { get; set; }
}
Also supports HalfVector and SparseVector
Create a table
conn.Execute("CREATE TABLE items (id serial PRIMARY KEY, embedding vector(3))");
Insert a vector
var embedding = new Vector(new float[] { 1, 1, 1 });
conn.Execute(@"INSERT INTO items (embedding) VALUES (@embedding)", new { embedding });
Get the nearest neighbors
var embedding = new Vector(new float[] { 1, 1, 1 });
var items = conn.Query<Item>("SELECT * FROM items ORDER BY embedding <-> @embedding LIMIT 5", new { embedding });
foreach (Item item in items)
{
Console.WriteLine(item.Embedding);
}
Add an approximate index
conn.Execute("CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)");
// or
conn.Execute("CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)");
Use vector_ip_ops for inner product and vector_cosine_ops for cosine distance
See a full example
Run
dotnet add package Pgvector.EntityFrameworkCore
The latest version works with .NET 8 and 9. For .NET 6 and 7, use version 0.1.2 and this readme.
Import the library
using Pgvector.EntityFrameworkCore;
Enable the extension
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasPostgresExtension("vector");
}
Configure the connection
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql("connString", o => o.UseVector());
}
Define a model
public class Item
{
public int Id { get; set; }
[Column(TypeName = "vector(3)")]
public Vector? Embedding { get; set; }
}
Also supports HalfVector and SparseVector
Insert a vector
ctx.Items.Add(new Item { Embedding = new Vector(new float[] { 1, 1, 1 }) });
ctx.SaveChanges();
Get the nearest neighbors
var embedding = new Vector(new float[] { 1, 1, 1 });
var items = await ctx.Items
.OrderBy(x => x.Embedding!.L2Distance(embedding))
.Take(5)
.ToListAsync();
foreach (Item item in items)
{
if (item.Embedding != null)
{
Console.WriteLine(item.Embedding);
}
}
Also supports MaxInnerProduct, CosineDistance, L1Distance, HammingDistance, and JaccardDistance
Get the distance
var items = await ctx.Items
.Select(x => new { Entity = x, Distance = x.Embedding!.L2Distance(embedding) })
.ToListAsync();
Get items within a certain distance
var items = await ctx.Items
.Where(x => x.Embedding!.L2Distance(embedding) < 5)
.ToListAsync();
Add an approximate index
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Item>()
.HasIndex(i => i.Embedding)
.HasMethod("hnsw")
.HasOperators("vector_l2_ops")
.HasStorageParameter("m", 16)
.HasStorageParameter("ef_construction", 64);
// or
modelBuilder.Entity<Item>()
.HasIndex(i => i.Embedding)
.HasMethod("ivfflat")
.HasOperators("vector_l2_ops")
.HasStorageParameter("lists", 100);
}
Use vector_ip_ops for inner product and vector_cosine_ops for cosine distance
See a full example
Run
dotnet add package Pgvector
Import the library
open Pgvector
Create a connection
let dataSourceBuilder = new NpgsqlDataSourceBuilder(connString)
dataSourceBuilder.UseVector()
use dataSource = dataSourceBuilder.Build()
Enable the extension
dataSource
|> Sql.fromDataSource
|> Sql.query "CREATE EXTENSION IF NOT EXISTS vector"
|> Sql.executeNonQuery
Create a table
dataSource
|> Sql.fromDataSource
|> Sql.query "CREATE TABLE items (id serial PRIMARY KEY, embedding vector(3))"
|> Sql.executeNonQuery
Insert a vector
let embedding = new Vector([| 1f; 1f; 1f |])
let parameter = new NpgsqlParameter("", embedding)
dataSource
|> Sql.fromDataSource
|> Sql.query "INSERT INTO items (embedding) VALUES (@embedding)"
|> Sql.parameters [ "embedding", Sql.parameter parameter ]
|> Sql.executeNonQuery
Get the nearest neighbors
type Item = {
Id: int
Embedding: Vector
}
dataSource
|> Sql.fromDataSource
|> Sql.query "SELECT * FROM items ORDER BY embedding <-> @embedding LIMIT 5"
|> Sql.parameters [ "embedding", Sql.parameter parameter ]
|> Sql.execute (fun read ->
{
Id = read.int "id"
Embedding = read.fieldValue<Vector> "embedding"
})
|> printfn "%A"
Add an approximate index
dataSource
|> Sql.fromDataSource
|> Sql.query "CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)"
|> Sql.executeNonQuery
Use vector_ip_ops for inner product and vector_cosine_ops for cosine distance
See a full example
Run
dotnet add package Pgvector
Create a connection
Dim dataSourceBuilder As New NpgsqlDataSourceBuilder(connString)
dataSourceBuilder.UseVector()
Dim dataSource = dataSourceBuilder.Build()
Dim conn = dataSource.OpenConnection()
Enable the extension
Using cmd As New NpgsqlCommand("CREATE EXTENSION IF NOT EXISTS vector", conn)
cmd.ExecuteNonQuery()
End Using
conn.ReloadTypes()
Create a table
Using cmd As New NpgsqlCommand("CREATE TABLE items (id serial PRIMARY KEY, embedding vector(3))", conn)
cmd.ExecuteNonQuery()
End Using
Insert a vector
Using cmd As New NpgsqlCommand("INSERT INTO items (embedding) VALUES ($1)", conn)
Dim embedding As New Vector(New Single() {1, 1, 1})
cmd.Parameters.AddWithValue(embedding)
cmd.ExecuteNonQuery()
End Using
Get the nearest neighbors
Using cmd As New NpgsqlCommand("SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5", conn)
Dim embedding As New Vector(New Single() {1, 1, 1})
cmd.Parameters.AddWithValue(embedding)
Using reader As NpgsqlDataReader = cmd.ExecuteReader()
While reader.Read()
Console.WriteLine(reader.GetValue(0))
End While
End Using
End Using
Add an approximate index
Using cmd As New NpgsqlCommand("CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)", conn)
cmd.ExecuteNonQuery()
End Using
Use vector_ip_ops for inner product and vector_cosine_ops for cosine distance
See a full example
Create a vector from an array
var vec = new Vector(new float[] { 1, 2, 3 });
Get an array
var arr = vec.ToArray();
Create a half vector from an array
var vec = new HalfVector(new Half[] { (Half)1, (Half)2, (Half)3 });
Get an array
var arr = vec.ToArray();
Create a sparse vector from an array
var vec = new SparseVector(new float[] { 1, 0, 2, 0, 3, 0 });
Or a dictionary of non-zero elements
var dictionary = new Dictionary<int, float>();
dictionary.Add(0, 1);
dictionary.Add(2, 2);
dictionary.Add(4, 3);
var vec = new SparseVector(dictionary, 6);
Note: Indices start at 0
Get the number of dimensions
var dim = vec.Dimensions;
Get the indices of non-zero elements
var indices = vec.Indices;
Get the values of non-zero elements
var values = vec.Values;
Get an array
var arr = vec.ToArray();
Everyone is encouraged to help improve this project. Here are a few ways you can help:
To get started with development:
git clone https://github.com/pgvector/pgvector-dotnet.git
cd pgvector-dotnet
createdb pgvector_dotnet_test
dotnet test
To run an example:
cd examples/Loading
createdb pgvector_example
dotnet run
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 is compatible. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on Pgvector:
| Package | Downloads |
|---|---|
|
Pgvector.EntityFrameworkCore
pgvector support for Entity Framework Core |
|
|
Microsoft.KernelMemory.MemoryDb.Postgres
Postgres(with pgvector extension) connector for Microsoft Kernel Memory, to store and search memory using Postgres vector indexing and Postgres features. |
|
|
Microsoft.SemanticKernel.Connectors.PgVector
Postgres (with pgvector extension) provider for Microsoft.Extensions.VectorData by Semantic Kernel |
|
|
Microsoft.SemanticKernel.Connectors.Postgres
Postgres(with pgvector extension) connector for Semantic Kernel plugins and semantic memory |
|
|
Pgvector.Dapper
pgvector support for Dapper |
Showing the top 8 popular GitHub repositories that depend on Pgvector:
| Repository | Stars |
|---|---|
|
microsoft/semantic-kernel
Integrate cutting-edge LLM technology quickly and easily into your apps
|
|
|
dotnet/eShop
A reference .NET application implementing an eCommerce site
|
|
|
microsoft/kernel-memory
Research project. A Memory solution for users, teams, and applications.
|
|
|
thangchung/practical-dotnet-aspire
The practical .NET Aspire builds on the coffeeshop app business domain
|
|
|
Azure-Samples/eShopOnAzure
A variant of https://github.com/dotnet/eShop that uses Azure services
|
|
|
signumsoftware/framework
Open Source framework for writing data-centric applications using the latest versions of .Net Core, C# (not-nullable), ASP.NET Web API, Typescript (strict), React, D3 and Sql Server or PostgreeSQL
|
|
|
microsoft/Document-Knowledge-Mining-Solution-Accelerator
Solution accelerator built on Azure OpenAI Service and Azure AI Document Intelligence to process and extract summaries, entities, and metadata from unstructured, multi-modal documents and enable searching and chatting over this data.
|
|
|
petabridge/memorizer
Vector-search powered agent memory MCP server
|
| Version | Downloads | Last Updated |
|---|---|---|
| 0.3.2 | 1,755,744 | 5/20/2025 |
| 0.3.1 | 1,301,167 | 3/22/2025 |
| 0.3.0 | 1,160,171 | 6/26/2024 |
| 0.2.0 | 1,060,392 | 11/24/2023 |
| 0.2.0-rc.2 | 7,085 | 10/26/2023 |
| 0.2.0-rc.1 | 1,589 | 10/14/2023 |
| 0.1.4 | 121,271 | 9/25/2023 |
| 0.1.3 | 163,500 | 5/20/2023 |
| 0.1.2 | 5,862 | 4/25/2023 |
| 0.1.1 | 78,697 | 3/12/2023 |
| 0.1.0 | 9,109 | 3/10/2023 |