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

警告

语义内核向量存储功能处于预览状态,需要中断性变更的改进可能仍发生在发布前的有限情况下。

概述

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

功能区域 支持
集合映射到 MongoDB 集合 + 索引
支持的键属性类型 string
支持的数据属性类型
  • string
  • int
  • long
  • double
  • float
  • 十进制
  • 布尔
  • DateTime
  • 和枚举每种类型
支持的向量属性类型
  • ReadOnlyMemory<float>
  • ReadOnlyMemory<double>
支持的索引类型 空值
支持的距离函数
  • CosineSimilarity
  • DotProductSimilarity
  • EuclideanDistance
支持的筛选器子句
  • EqualTo
支持记录中的多个向量
是否支持Filterable?
是否支持FullTextSearchable?
支持 StoragePropertyName? 否,请改用 BsonElementAttribute。 有关详细信息,请参阅此处。

入门

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

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

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

using Microsoft.SemanticKernel;

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

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

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.AddMongoDBVectorStore();

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

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

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

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

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

var mongoClient = new MongoClient(connectionString);
var database = mongoClient.GetDatabase(databaseName);
var collection = new MongoDBVectorStoreRecordCollection<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;

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

即将推出

更多信息即将推出。

即将推出

更多信息即将推出。