使用 SQLite Vector Store 连接器 (预览版)
警告
语义内核向量存储功能处于预览状态,需要中断性变更的改进可能仍发生在发布前的有限情况下。
概述
SQLite Vector Store 连接器可用于访问和管理 SQLite 中的数据。 连接器具有以下特征。
功能区域 | 支持 |
---|---|
集合映射到 | SQLite 表 |
支持的键属性类型 |
|
支持的数据属性类型 |
|
支持的向量属性类型 | ReadOnlyMemory<float> |
支持的索引类型 | 空值 |
支持的距离函数 |
|
支持的过滤器子句 |
|
支持记录中的多个向量 | 是 |
是否支持Filterable? | 否 |
是否支持FullTextSearchable? | 否 |
支持 StoragePropertyName? | 是 |
限制
SQLite 不支持现装的矢量搜索。 首先应加载 SQLite 扩展以启用矢量搜索功能。 SQLite 连接器的当前实现与 sqlite-vec 矢量搜索扩展兼容。
若要安装扩展,请将其中 一个版本 与所选的特定扩展版本一起使用。 可以使用脚本获取预编译的版本 install.sh
。 此脚本将生成 vec0.dll
,该脚本必须与正在运行的应用程序位于同一文件夹中。 这将允许应用程序调用 SqliteConnection.LoadExtension("vec0")
该方法并加载矢量搜索扩展。
入门
将 SQLite Vector Store 连接器 NuGet 包添加到项目。
dotnet add package Microsoft.SemanticKernel.Connectors.Sqlite --prerelease
可以使用语义内核提供的扩展方法将向量存储 IServiceCollection
添加到依赖项注入容器。
在这种情况下,将初始化类的 Microsoft.Data.Sqlite.SqliteConnection
实例,将打开连接,并加载矢量搜索扩展。 默认矢量搜索扩展名称是 vec0
,但可以使用该 SqliteVectorStoreOptions.VectorSearchExtensionName
属性重写它。
using Microsoft.SemanticKernel;
// Using IServiceCollection with ASP.NET Core.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSqliteVectorStore("Data Source=:memory:");
还提供不带参数的扩展方法。 这些要求将类的 Microsoft.Data.Sqlite.SqliteConnection
实例单独注册到依赖项注入容器。
在这种情况下,只有在之前未打开连接时,连接才会打开,扩展方法将假定已为已注册 Microsoft.Data.Sqlite.SqliteConnection
实例加载矢量搜索扩展。
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
// Using IServiceCollection with ASP.NET Core.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<SqliteConnection>(sp =>
{
var connection = new SqliteConnection("Data Source=:memory:");
connection.LoadExtension("vector-search-extension-name");
return connection;
});
builder.Services.AddSqliteVectorStore();
可以直接构造 SQLite 矢量存储实例。
using Microsoft.Data.Sqlite;
using Microsoft.SemanticKernel.Connectors.Sqlite;
var connection = new SqliteConnection("Data Source=:memory:");
connection.LoadExtension("vector-search-extension-name");
var vectorStore = new SqliteVectorStore(connection);
可以构造对命名集合的直接引用。
using Microsoft.Data.Sqlite;
using Microsoft.SemanticKernel.Connectors.Sqlite;
var connection = new SqliteConnection("Data Source=:memory:");
connection.LoadExtension("vector-search-extension-name");
var collection = new SqliteVectorStoreRecordCollection<Hotel>(connection, "skhotels");
数据映射
从数据模型映射到存储时,SQLite Vector Store 连接器提供默认映射器。 此映射器将数据模型中的属性列表直接转换为 SQLite 中的列。
还可以通过属性提供自定义映射器来替代默认映射器 SqliteVectorStoreRecordCollectionOptions<TRecord>.DictionaryCustomMapper
行为。
借助矢量搜索扩展,矢量存储在虚拟表中,独立于键和数据属性。
默认情况下,具有矢量的虚拟表将使用与具有键和数据属性的表相同的名称,但具有 vec_
前缀。 例如,如果集合名称为 SqliteVectorStoreRecordCollection
<skhotels
。 可以使用或SqliteVectorStoreOptions.VectorVirtualTableName
属性替代虚拟表名称SqliteVectorStoreRecordCollectionOptions<TRecord>.VectorVirtualTableName
。
属性名称重写
可以重写属性名称,以在存储中使用的属性名称与数据模型上的属性名称不同。
属性名称重写是通过数据模型属性属性或记录定义设置 StoragePropertyName
选项来完成的。
下面是在其 StoragePropertyName
属性上设置的数据模型示例,以及如何在 SQLite 查询中表示数据模型。
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 Hotels (
HotelId INTEGER PRIMARY KEY,
hotel_name TEXT,
hotel_description TEXT
);
CREATE VIRTUAL TABLE vec_Hotels (
HotelId INTEGER PRIMARY KEY,
DescriptionEmbedding FLOAT[4] distance_metric=cosine
);
即将推出
更多信息即将推出。
JDBC
JDBC 连接器可用于连接到 SQLite。