使用 SQLite 向量存放區連接器 (預覽)
警告
語意核心向量存放區功能處於預覽狀態,且需要重大變更的改善可能仍會在發行前有限的情況下發生。
概觀
SQLite 向量存放區連接器可用來存取和管理 SQLite 中的數據。 連接器具有下列特性。
功能區域 | 支援 |
---|---|
集合對應至 | SQLite 數據表 |
支援的鍵屬性類型 |
|
支援的數據類型 |
|
支援的向量屬性類型 | ReadOnlyMemory<float> |
支援的索引類型 | N/A |
支援的距離函數 |
|
支援的篩選子句 |
|
支援記錄中的多個向量 | Yes |
是否支援IsFilterable? | 不 |
是否支援 IsFullTextSearchable 功能? | 不 |
StoragePropertyName 是否支援? | Yes |
支援 HybridSearch 嗎? | 不 |
限制
SQLite 不支援開箱即用的向量搜尋。 應先載入 SQLite 延伸模組,以啟用向量搜尋功能。 SQLite 連接器的目前實作與 sqlite-vec 向量搜尋延伸模組相容。
若要安裝擴充功能,請使用其中 一個版本 搭配您選擇的特定擴充功能版本。 可以使用install.sh
腳本取得預先編譯的版本。 此文稿會產生 vec0.dll
,其必須位於與執行中的應用程式位於相同的資料夾中。 這可讓應用程式呼叫 方法, SqliteConnection.LoadExtension("vec0")
並載入向量搜尋延伸模組。
開始使用
將 SQLite Vector Store 連接器 NuGet 套件新增至您的專案。
dotnet add package Microsoft.SemanticKernel.Connectors.Sqlite --prerelease
您可以使用 Semantic Kernel 所提供的擴充方法,將向量存放區新增至 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 向量存儲區連接器會提供預設對應程式。 此映射器會將資料模型上的屬性清單直接映射成 SQLite 中的資料行。
您也可以透過SqliteVectorStoreRecordCollectionOptions<TRecord>.DictionaryCustomMapper
屬性提供自訂的映射器,以覆寫預設的映射行為。
使用向量搜尋延伸模組時,向量會儲存在虛擬數據表中,與索引鍵和數據屬性分開。
根據預設,具有向量的虛擬數據表會使用與具有索引鍵和數據屬性之數據表相同的名稱,但具有 vec_
前置詞。 例如,如果中的 SqliteVectorStoreRecordCollection
集合名稱是 skhotels
,則具有向量之虛擬資料表的名稱會是 vec_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。