使用 Azure CosmosDB MongoDB 向量存放區連接器 (預覽)
警告
語意核心向量存放區功能處於預覽狀態,且需要重大變更的改善可能仍會在發行前有限的情況下發生。
概觀
Azure CosmosDB MongoDB 向量存放區連接器可用來存取和管理 Azure CosmosDB MongoDB 中的數據。 連接器具有下列特性。
功能區域 | 支援 |
---|---|
集合對應至 | Azure Cosmos DB MongoDB 集合 + 索引 |
支援的索引鍵屬性類型 | 字串 |
支援的數據類型 |
|
支援的向量屬性類型 |
|
支援的索引類型 |
|
支援的距離函式 |
|
支援的篩選子句 |
|
支援記錄中的多個向量 | Yes |
是否支援IsFilterable? | Yes |
是否支援IsFullTextSearchable? | No |
支援的 StoragePropertyName? | 否,請改用 BsonElementAttribute。 如需詳細資訊,請參閱這裡。 |
開始使用
將 Azure CosmosDB MongoDB MongoDB 向量存放區連接器 NuGet 套件新增至您的專案。
dotnet add package Microsoft.SemanticKernel.Connectors.AzureCosmosDBMongoDB --prerelease
您可以使用 Semantic Kernel 所提供的擴充方法,將向量存放區新增至 KernelBuilder
上IServiceCollection
可用的相依性插入容器或相依性插入容器。
using Microsoft.SemanticKernel;
// Using Kernel Builder.
var kernelBuilder = Kernel
.CreateBuilder()
.AddAzureCosmosDBMongoDBVectorStore(connectionString, databaseName);
using Microsoft.SemanticKernel;
// Using IServiceCollection with ASP.NET Core.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAzureCosmosDBMongoDBVectorStore(connectionString, databaseName);
也提供不採用任何參數的擴充方法。 這些實例需要 MongoDB.Driver.IMongoDatabase
個別向相依性插入容器註冊。
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using MongoDB.Driver;
// Using Kernel Builder.
var kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.Services.AddSingleton<IMongoDatabase>(
sp =>
{
var mongoClient = new MongoClient(connectionString);
return mongoClient.GetDatabase(databaseName);
});
kernelBuilder.AddAzureCosmosDBMongoDBVectorStore();
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using MongoDB.Driver;
// Using IServiceCollection with ASP.NET Core.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<IMongoDatabase>(
sp =>
{
var mongoClient = new MongoClient(connectionString);
return mongoClient.GetDatabase(databaseName);
});
builder.Services.AddAzureCosmosDBMongoDBVectorStore();
您可以直接建構 Azure CosmosDB MongoDB 向量存放區實例。
using Microsoft.SemanticKernel.Connectors.AzureCosmosDBMongoDB;
using MongoDB.Driver;
var mongoClient = new MongoClient(connectionString);
var database = mongoClient.GetDatabase(databaseName);
var vectorStore = new AzureCosmosDBMongoDBVectorStore(database);
可以建構具名集合的直接參考。
using Microsoft.SemanticKernel.Connectors.AzureCosmosDBMongoDB;
using MongoDB.Driver;
var mongoClient = new MongoClient(connectionString);
var database = mongoClient.GetDatabase(databaseName);
var collection = new AzureCosmosDBMongoDBVectorStoreRecordCollection<Hotel>(
database,
"skhotels");
資料對應
當將數據從數據模型對應至記憶體時,Azure CosmosDB MongoDB 向量存放區連接器會提供預設對應程式。
此對應程式會將數據模型上的屬性清單直接轉換成 Azure CosmosDB MongoDB 中的欄位,並使用 MongoDB.Bson.Serialization
轉換為記憶體架構。 這表示如果需要與資料模型屬性名稱不同的記憶體名稱,則支援 使用 MongoDB.Bson.Serialization.Attributes.BsonElement
。 唯一的例外狀況是對應至名為 _id
的資料庫欄位之記錄索引鍵,因為所有 CosmosDB MongoDB 記錄都必須將此名稱用於標識碼。
屬性名稱覆寫
針對數據屬性和向量屬性,您可以提供覆寫功能變數名稱,以用於與數據模型上屬性名稱不同的記憶體中。 密鑰不支援此項目,因為金鑰在 MongoDB 中有固定名稱。
屬性名稱覆寫是藉由在數據模型屬性上設定 BsonElement
屬性來完成。
以下是具有 BsonElement
set 的數據模型範例。
using Microsoft.Extensions.VectorData;
public class Hotel
{
[VectorStoreRecordKey]
public ulong HotelId { get; set; }
[BsonElement("hotel_name")]
[VectorStoreRecordData(IsFilterable = true)]
public string HotelName { get; set; }
[BsonElement("hotel_description")]
[VectorStoreRecordData(IsFullTextSearchable = true)]
public string Description { get; set; }
[BsonElement("hotel_description_embedding")]
[VectorStoreRecordVector(4, DistanceFunction.CosineDistance, IndexKind.Hnsw)]
public ReadOnlyMemory<float>? DescriptionEmbedding { get; set; }
}
即將推出
更多信息即將推出。
即將推出
更多信息即將推出。