警告
Azure CosmosDB MongoDB (vCore) 向量存储功能处于预览状态,在发布之前,在有限情况下仍可能发生需要重大更改的改进。
警告
语义内核向量存储功能处于预览状态,可能需要重大变更的改进仍可能在发布前的有限情况下发生。
警告
语义内核向量存储功能处于预览状态,可能需要重大变更的改进仍可能在发布前的有限情况下发生。
概述
Azure CosmosDB MongoDB 矢量存储连接器可用于访问和管理 Azure CosmosDB MongoDB(vCore)中的数据。 连接器具有以下特征。
功能区域 | 支持 |
---|---|
集合映射到 | Azure Cosmos DB MongoDB (vCore) 集合 + 索引 |
支持的键属性类型 | 字符串 |
支持的数据属性类型 |
|
支持的向量属性类型 |
|
支持的索引类型 |
|
支持的距离函数 |
|
支持的过滤器条件 |
|
支持记录中的多个向量 | 是 |
是否支持 IsIndexed? | 是 |
是否支持FullTextIndexed? | 否 |
StorageName支持吗? | 否,请改用 BsonElementAttribute。 有关详细信息,请参阅此处。 |
支持 HybridSearch? | 否 |
功能区域 | 支持 |
---|---|
集合映射到 | Azure Cosmos DB MongoDB (vCore) 集合 + 索引 |
支持的键属性类型 | 字符串 |
支持的数据属性类型 |
|
支持的向量属性类型 |
|
支持的索引类型 |
|
支持的距离函数 |
|
支持的过滤器条件 |
|
支持记录中的多个向量 | 是 |
是否支持Filterable? | 是 |
是否支持FullTextSearchable? | 否 |
更多信息即将推出。
局限性
此连接器与 Azure Cosmos DB MongoDB(vCore)兼容,但不适用于 Azure Cosmos DB MongoDB(RU)。
入门
将 Azure CosmosDB MongoDB Vector Store 连接器 NuGet 包添加到项目。
dotnet add package Microsoft.SemanticKernel.Connectors.CosmosMongoDB --prerelease
可以使用语义内核提供的扩展方法将向量存储添加到可用的 KernelBuilder
依赖项注入容器或 IServiceCollection
依赖项注入容器。
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
// Using Kernel Builder.
var kernelBuilder = Kernel
.CreateBuilder();
kernelBuilder.Services
.AddCosmosMongoVectorStore(connectionString, databaseName);
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
// Using IServiceCollection with ASP.NET Core.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCosmosMongoVectorStore(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.Services.AddCosmosMongoVectorStore();
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.AddCosmosMongoVectorStore();
可以直接构造 Azure CosmosDB MongoDB 矢量存储实例。
using Microsoft.SemanticKernel.Connectors.CosmosMongoDB;
using MongoDB.Driver;
var mongoClient = new MongoClient(connectionString);
var database = mongoClient.GetDatabase(databaseName);
var vectorStore = new CosmosMongoVectorStore(database);
可以构造对命名集合的直接引用。
using Microsoft.SemanticKernel.Connectors.CosmosMongoDB;
using MongoDB.Driver;
var mongoClient = new MongoClient(connectionString);
var database = mongoClient.GetDatabase(databaseName);
var collection = new CosmosMongoCollection<ulong, Hotel>(
database,
"skhotels");
数据映射
将数据从数据模型映射到存储时,Azure CosmosDB MongoDB Vector Store 连接器提供默认映射器。
此映射器将数据模型中的属性列表直接转换为 Azure CosmosDB MongoDB 的字段,并使用 MongoDB.Bson.Serialization
进行存储架构的转换。 这意味着,如果需要与数据模型属性名称不同的存储名称,则支持使用该 MongoDB.Bson.Serialization.Attributes.BsonElement
名称。 唯一的例外是记录的键,它映射到名为 _id
的数据库字段,因为所有 CosmosDB MongoDB 记录的 ID 都必须使用此名称。
属性名称重写
对于数据属性和向量属性,可以提供替代字段名称,以便在存储中使用的字段名称与数据模型中的属性名称不同。 密钥不支持此操作,因为密钥在 MongoDB 中具有固定名称。
属性名称重写是通过对数据模型属性设置 BsonElement
属性来完成的。
下面是具有 BsonElement
集的数据模型的示例。
using Microsoft.Extensions.VectorData;
public class Hotel
{
[VectorStoreKey]
public ulong HotelId { get; set; }
[BsonElement("hotel_name")]
[VectorStoreData(IsIndexed = true)]
public string HotelName { get; set; }
[BsonElement("hotel_description")]
[VectorStoreData(IsFullTextIndexed = true)]
public string Description { get; set; }
[BsonElement("hotel_description_embedding")]
[VectorStoreVector(4, DistanceFunction = DistanceFunction.CosineDistance, IndexKind = IndexKind.Hnsw)]
public ReadOnlyMemory<float>? DescriptionEmbedding { get; set; }
}
入门
将 Azure CosmosDB MongoDB 矢量存储依赖项添加到环境。 由于 Azure CosmosDB MongoDB 连接器基于 MongoDB Atlas 连接器构建,并且使用与该连接器相同的客户端,因此需要安装以下附加组件:
pip install semantic-kernel[azure, mongo]
然后,可以创建向量存储。
from semantic_kernel.connectors.memory.azure_cosmos_db import AzureCosmosDBforMongoDBStore
# If the right environment settings are set, namely AZURE_COSMOS_DB_MONGODB_CONNECTION_STRING and optionally AZURE_COSMOS_DB_MONGODB_DATABASE_NAME, this is enough to create the Store:
store = AzureCosmosDBforMongoDBStore()
另外,你也可以传入你自己的 mongodb 客户端,以便对客户端构造有更好的控制。
from pymongo import AsyncMongoClient
from semantic_kernel.connectors.memory.azure_cosmos_db import AzureCosmosDBforMongoDBStore
client = AsyncMongoClient(...)
store = AzureCosmosDBforMongoDBStore(mongo_client=client)
传入客户端时,语义内核不会为你关闭连接,因此需要确保将其关闭,例如使用 async with
语句。
您还可以在没有商店的情况下直接创建集合。
from semantic_kernel.connectors.memory.azure_cosmos_db import AzureCosmosDBforMongoDBCollection
# `hotel` is a class created with the @vectorstoremodel decorator
collection = AzureCosmosDBforMongoDBCollection(
collection_name="my_collection",
data_model_type=hotel
)
序列化
由于 Azure CosmosDB for MongoDB 连接器需要一个简单的字典,字段与索引对应作为输入,序列化非常简单,它只使用预先确定的键 _id
,因此,如果数据模型的键尚不是 _id
,我们将用该键替换数据模型的键。
有关此概念的更多详细信息,请参阅 序列化文档。
即将推出
更多信息即将推出。