DotnetPrompt.Npgsql.PgVector 1.0.0-alpha.1

This is a prerelease version of DotnetPrompt.Npgsql.PgVector.
dotnet add package DotnetPrompt.Npgsql.PgVector --version 1.0.0-alpha.1
NuGet\Install-Package DotnetPrompt.Npgsql.PgVector -Version 1.0.0-alpha.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="DotnetPrompt.Npgsql.PgVector" Version="1.0.0-alpha.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DotnetPrompt.Npgsql.PgVector --version 1.0.0-alpha.1
#r "nuget: DotnetPrompt.Npgsql.PgVector, 1.0.0-alpha.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 DotnetPrompt.Npgsql.PgVector as a Cake Addin
#addin nuget:?package=DotnetPrompt.Npgsql.PgVector&version=1.0.0-alpha.1&prerelease

// Install DotnetPrompt.Npgsql.PgVector as a Cake Tool
#tool nuget:?package=DotnetPrompt.Npgsql.PgVector&version=1.0.0-alpha.1&prerelease

Npgsql.PgVector - dotnet mapping for PostgreSQL pg_vector extension.

What is Npgsql.PgVector?

Npgsql.PgVector gives you convinient way to work with pg_vector extension to store and work with vectors like Embedding vectors for LLM and more.

Read more about pg_vector extension, how to install and work with it here.

Setup

To use the mapping, first, ensure that you installed and loaded pg_vector extension and set it up like this:

var dataSourceBuilder = new NpgsqlDataSourceBuilder(...);
dataSourceBuilder.UsePgVector();
await using var dataSource = dataSourceBuilder.Build();

Once the mapping is set up, you can transparently read and write the Vector object.

Getting Started

Create a vector column with 3 dimensions

await using (var cmd = dataSource.CreateCommand("CREATE TABLE IF NOT EXISTS items (embedding vector(3))"))
{
    await cmd.ExecuteNonQueryAsync();
}

Insert values

await using var connection = await dataSource.OpenConnectionAsync();
await using (var cmd = new NpgsqlCommand("INSERT INTO items VALUES ($1), ($2)", connection))
{
    cmd.Parameters.AddWithValue(new Vector(1f, 2f, 3f));
    cmd.Parameters.AddWithValue(new Vector(4f, 5f, 6f));
    await cmd.ExecuteNonQueryAsync();
}

Get the nearest neighbor by L2 distance

await using (var cmd = dataSource.CreateCommand("SELECT * FROM items ORDER BY embedding <-> ($1) LIMIT 1"))
{
    cmd.Parameters.AddWithValue(new Vector(1f, 2f, 3f));

    var value = await cmd.ExecuteScalarAsync();
    Console.WriteLine(value);
}
> [1,2,3]

Also supports inner product (<#>) and cosine distance (<=>)

Note: <#> returns the negative inner product since Postgres only supports ASC order index scans on operators

Querying

Use a SELECT clause to get the distance

await using (var cmd = dataSource.CreateCommand("SELECT embedding <-> ($1) AS distance FROM items"))
{
    cmd.Parameters.AddWithValue(new Vector(3f, 2f, 1f));

    await using (var reader = await cmd.ExecuteReaderAsync())
    {
        while (await reader.ReadAsync())
        {
            Console.WriteLine(reader.GetDouble(0));
        }
    }
}
> 2.8284271247461903
> 5.916079783099616

Use a WHERE clause to get rows within a certain distance

await using (var cmd = dataSource.CreateCommand("SELECT * FROM items WHERE embedding <-> ($1) < ($2)"))
{
    cmd.Parameters.AddWithValue(new Vector(3f, 1f, 2f));
    cmd.Parameters.AddWithValue(5);

    await using (var reader = await cmd.ExecuteReaderAsync())
    {
        while (await reader.ReadAsync())
        {
            Console.WriteLine(reader.GetVector(0));
        }
    }
}
> [1,2,3]

Note: Combine with ORDER BY and LIMIT to use an index

Get the average of vectors

await using (var cmd = dataSource.CreateCommand("SELECT AVG(embedding) FROM items"))
{
    var value = await cmd.ExecuteScalarAsync();
    Console.WriteLine(value);
}
> [2.5,3.5,4.5]

Learn more about queries, indexes and other stuff on official pg_vector repository.

Product 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. 
.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 was computed. 
.NET Framework net461 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

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
1.0.0-alpha.1 218 3/8/2023