使用 MongoDB Vector Store 连接器 (预览版)

警告

MongoDB 矢量存储功能处于预览状态,需要重大更改的改进可能仍发生在发布前的有限情况下。

警告

语义内核向量存储功能处于预览状态,可能仍需要在发布前的一些有限情况下进行重大变更以改进功能。

警告

语义内核向量存储功能处于预览状态,可能仍需要在发布前的一些有限情况下进行重大变更以改进功能。

概述

MongoDB 矢量存储连接器可用于访问和管理 MongoDB 中的数据。 连接器具有以下特征。

功能区域 支持
集合映射到 MongoDB 集合 + 索引
支持的键属性类型 字符串
支持的数据属性类型
  • 字符串
  • 整数 (int)
  • 双倍
  • 漂浮
  • 十进制
  • 布尔
  • 日期时间
  • 以及这些类型的可枚举对象
支持的向量属性类型
  • 只读内存<float>
  • <嵌入浮点>
  • float[]
支持的索引类型 不适用
支持的距离函数
  • 余弦相似度
  • 点积相似性 (DotProductSimilarity)
  • EuclideanDistance
支持的筛选器子句
  • EqualTo
支持记录中的多个向量
是否支持 IsIndexed?
是否支持FullTextIndexed?
StorageName支持吗? 否,请改用 BsonElementAttribute。 有关详细信息,请参阅此处。
支持 HybridSearch?
功能区域 支持
集合映射到 MongoDB 集合 + 索引
支持的键属性类型 字符串
支持的数据属性类型
  • 字符串
  • 整数 (int)
  • 双倍
  • 漂浮
  • 十进制
  • 布尔
  • 日期时间
  • 和这些类型中的每一种的可迭代对象
支持的向量属性类型
  • list[float]
  • 列表[整数]
  • ndarray
支持的索引类型
  • Hnsw
  • IvfFlat
支持的距离函数
  • CosineDistance
  • 点积相似性 (DotProductSimilarity)
  • EuclideanDistance
支持的筛选器子句
  • EqualTo
  • AnyTagsEqualTo
支持记录中的多个向量
是否支持Filterable?
是否支持FullTextSearchable?

更多信息即将推出。

入门

将 MongoDB Vector Store 连接器 NuGet 包添加到项目。

dotnet add package Microsoft.SemanticKernel.Connectors.MongoDB --prerelease

可以使用语义内核提供的扩展方法将向量存储 IServiceCollection 添加到依赖项注入容器。

using Microsoft.Extensions.DependencyInjection;

// Using IServiceCollection with ASP.NET Core.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMongoVectorStore(connectionString, databaseName);

还提供不带参数的扩展方法。 这些要求将实例 MongoDB.Driver.IMongoDatabase 单独注册到依赖项注入容器。

using Microsoft.Extensions.DependencyInjection;
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.AddMongoVectorStore();

可以直接构造 MongoDB 矢量存储实例。

using Microsoft.SemanticKernel.Connectors.MongoDB;
using MongoDB.Driver;

var mongoClient = new MongoClient(connectionString);
var database = mongoClient.GetDatabase(databaseName);
var vectorStore = new MongoVectorStore(database);

可以构造对命名集合的直接引用。

using Microsoft.SemanticKernel.Connectors.MongoDB;
using MongoDB.Driver;

var mongoClient = new MongoClient(connectionString);
var database = mongoClient.GetDatabase(databaseName);
var collection = new MongoCollection<string, Hotel>(
    database,
    "skhotels");

数据映射

当将数据从数据模型映射到存储时,MongoDB Vector Store 连接器提供默认映射器。

此映射器直接将数据模型中的属性列表转换为 MongoDB 中的字段,并使用 MongoDB.Bson.Serialization 转换为存储架构。 这意味着,如果需要与数据模型属性名称不同的存储名称,则支持使用该 MongoDB.Bson.Serialization.Attributes.BsonElement 名称。 唯一的例外是映射到名为 _id的数据库字段的记录的键,因为所有 MongoDB 记录都必须将此名称用于 ID。

属性名称重写

对于数据属性和向量属性,可以提供替代字段名称,以便在存储中使用的字段名称与数据模型中的属性名称不同。 密钥不支持此操作,因为密钥在 MongoDB 中具有固定名称。

属性名称重写是通过对数据模型属性设置 BsonElement 属性来完成的。

下面是具有 BsonElement 集的数据模型的示例。

using Microsoft.Extensions.VectorData;
using MongoDB.Bson.Serialization.Attributes;

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.CosineSimilarity)]
    public ReadOnlyMemory<float>? DescriptionEmbedding { get; set; }
}

入门

将 MongoDB Atlas Vector Store 依赖项添加到环境。 它需要 mongo extra 中包含的 pymongo 包:需要安装以下附加组件:

pip install semantic-kernel[mongo]

然后,可以创建向量存储。

from semantic_kernel.connectors.memory.mongodb_atlas import MongoDBAtlasStore

# If the right environment settings are set, namely MONGODB_ATLAS_CONNECTION_STRING and optionally MONGODB_ATLAS_DATABASE_NAME and MONGODB_ATLAS_INDEX_NAME, this is enough to create the Store:
store = MongoDBAtlasStore()

另外,你也可以传入你自己的 mongodb 客户端,以便对客户端构造有更好的控制。

from pymongo import AsyncMongoClient
from semantic_kernel.connectors.memory.mongodb_atlas import MongoDBAtlasStore

client = AsyncMongoClient(...)
store = MongoDBAtlasStore(mongo_client=client)

当客户端被传入时,语义内核不会为你关闭连接,因此你需要确保将连接关闭,比如使用 async with 语句。

还可以直接创建集合,而无需通过商店。

from semantic_kernel.connectors.memory.mongodb_atlas import MongoDBAtlasCollection

# `hotel` is a class created with the @vectorstoremodel decorator
collection = MongoDBAtlasCollection(
    collection_name="my_collection",
    data_model_type=hotel
)

序列化

由于 MongoDB Atlas 连接器需要一个与索引字段对应的简单字典作为输入,因此序列化非常容易,它只使用一个预先确定的键 _id,所以如果数据模型的键还不是 _id,我们将把数据模型的键替换为 _id

有关此概念的更多详细信息,请参阅 序列化文档

即将推出

更多信息即将推出。