Postgres Vector Store コネクタの使用 (プレビュー)
警告
セマンティック カーネル ベクター ストア機能はプレビュー段階であり、破壊的変更を必要とする機能強化は、リリース前の限られた状況で引き続き発生する可能性があります。
概要
Postgres ベクター ストア コネクタを使用して、Postgres のデータにアクセスして管理できます。 コネクタには次の特性があります。
機能領域 | サポート |
---|---|
コレクションのマップ | Postgres テーブル |
サポートされているキー プロパティの種類 |
|
サポートされているデータ プロパティ型 |
|
サポートされているベクター プロパティ型 | ReadOnlyMemory<浮動小数点数> |
サポートされているインデックスの種類 | Hnsw |
サポートされている距離関数 |
|
サポートされているフィルター句 |
|
レコード内の複数のベクターをサポートします | はい |
IsFilterable がサポートされていますか? | いいえ |
IsFullTextSearchable がサポートされていますか? | いいえ |
StoragePropertyName がサポートされていますか? | はい |
開始する
Postgres Vector Store コネクタ NuGet パッケージをプロジェクトに追加します。
dotnet add package Microsoft.SemanticKernel.Connectors.Postgres --prerelease
セマンティック カーネルによって提供される拡張メソッドを使用して、IServiceCollection
依存関係挿入コンテナーにベクター ストアを追加できます。
この場合、ベクター機能が有効になっている Npgsql.NpgsqlDataSource
クラスのインスタンスもコンテナーに登録されます。
using Microsoft.SemanticKernel;
// Using IServiceCollection with ASP.NET Core.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddPostgresVectorStore("Host=localhost;Port=5432;Username=postgres;Password=example;Database=postgres;");
パラメーターを受け取たない拡張メソッドも提供されます。 これらには、Npgsql.NpgsqlDataSource
クラスのインスタンスを依存関係挿入コンテナーに個別に登録する必要があります。
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Npgsql;
// Using IServiceCollection with ASP.NET Core.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<NpgsqlDataSource>(sp =>
{
NpgsqlDataSourceBuilder dataSourceBuilder = new("Host=localhost;Port=5432;Username=postgres;Password=example;Database=postgres;");
dataSourceBuilder.UseVector();
return dataSourceBuilder.Build();
});
builder.Services.AddPostgresVectorStore();
Postgres Vector Store インスタンスを直接構築できます。
using Microsoft.SemanticKernel.Connectors.Postgres;
using Npgsql;
NpgsqlDataSourceBuilder dataSourceBuilder = new("Host=localhost;Port=5432;Username=postgres;Password=example;Database=postgres;");
dataSourceBuilder.UseVector();
var dataSource = dataSourceBuilder.Build();
var connection = new PostgresVectorStore(dataSource);
名前付きコレクションへの直接参照を構築できます。
using Microsoft.SemanticKernel.Connectors.Postgres;
using Npgsql;
NpgsqlDataSourceBuilder dataSourceBuilder = new("Host=localhost;Port=5432;Username=postgres;Password=example;Database=postgres;");
dataSourceBuilder.UseVector();
var dataSource = dataSourceBuilder.Build();
var collection = new PostgresVectorStoreRecordCollection<string, Hotel>(dataSource, "skhotels");
データ マッピング
Postgres Vector Store コネクタは、データ モデルからストレージへのマッピング時に既定のマッパーを提供します。 このマッパーは、データ モデルのプロパティの一覧を Postgres の列に直接変換します。
PostgresVectorStoreRecordCollectionOptions<TRecord>.DictionaryCustomMapper
プロパティを使用してカスタム マッパーを指定することで、既定のマッパー動作をオーバーライドすることもできます。
プロパティ名のオーバーライド
データ モデルのプロパティ名とは異なるストレージで使用するプロパティ名をオーバーライドできます。
プロパティ名のオーバーライドは、データ モデルのプロパティ属性またはレコード定義を使用して StoragePropertyName
オプションを設定することによって行われます。
属性に StoragePropertyName
が設定されたデータ モデルの例と、Postgres クエリでの表現方法を次に示します。
using Microsoft.Extensions.VectorData;
public class Hotel
{
[VectorStoreRecordKey]
public ulong HotelId { get; set; }
[VectorStoreRecordData(StoragePropertyName = "hotel_name")]
public string? HotelName { get; set; }
[VectorStoreRecordData(StoragePropertyName = "hotel_description")]
public string? Description { get; set; }
[VectorStoreRecordVector(Dimensions: 4, DistanceFunction.CosineDistance)]
public ReadOnlyMemory<float>? DescriptionEmbedding { get; set; }
}
CREATE TABLE public."Hotels" (
"HotelId" INTEGER NOT NULL,
"hotel_name" TEXT ,
"hotel_description" TEXT ,
"DescriptionEmbedding" VECTOR(4) ,
PRIMARY KEY ("HotelId")
);
もうすぐです
詳細については、近日公開予定です。
JDBC
JDBC コネクタを使用して Postgres に接続できます。