使用 Postgres 矢量存储库连接器(预览版)
警告
语义内核向量存储功能处于预览状态,在发布前的某些有限情况下,可能仍会进行需要重大更改的改进。
概述
Postgres Vector Store 连接器可用于访问和管理 Postgres 中的数据。 连接器具有以下特征。
功能区域 | 支持 |
---|---|
集合映射到 | Postgres 表 |
支持的键属性类型 |
|
支持的数据属性类型 |
|
支持的向量属性类型 | 只读内存(ReadOnlyMemory)<浮点数(float)> |
支持的索引类型 | Hnsw |
支持的距离函数 |
|
支持的筛选器子句 |
|
支持记录中的多个向量 | 是的 |
是否支持Filterable? | 不 |
是否支持FullTextSearchable? | 不 |
支持 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 矢量存储实例。
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。