Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Warning
The MongoDB Vector Store functionality is in preview, and improvements that require breaking changes may still occur in limited circumstances before release.
Warning
The Semantic Kernel Vector Store functionality is in preview, and improvements that require breaking changes may still occur in limited circumstances before release.
Warning
The Semantic Kernel Vector Store functionality is in preview, and improvements that require breaking changes may still occur in limited circumstances before release.
Overview
The MongoDB Vector Store connector can be used to access and manage data in MongoDB. The connector has the following characteristics.
Feature Area | Support |
---|---|
Collection maps to | MongoDB Collection + Index |
Supported key property types | string |
Supported data property types |
|
Supported vector property types |
|
Supported index types | N/A |
Supported distance functions |
|
Supported filter clauses |
|
Supports multiple vectors in a record | Yes |
IsIndexed supported? | Yes |
IsFullTextIndexed supported? | No |
StorageName supported? | No, use BsonElementAttribute instead. See here for more info. |
HybridSearch supported? | Yes |
Feature Area | Support |
---|---|
Collection maps to | MongoDB Collection + Index |
Supported key property types | string |
Supported data property types |
|
Supported vector property types |
|
Supported index types |
|
Supported distance functions |
|
Supported filter clauses |
|
Supports multiple vectors in a record | Yes |
IsFilterable supported? | Yes |
IsFullTextSearchable supported? | No |
More info coming soon.
Getting started
Add the MongoDB Vector Store connector NuGet package to your project.
dotnet add package Microsoft.SemanticKernel.Connectors.MongoDB --prerelease
You can add the vector store to the IServiceCollection
dependency injection container using extension methods provided by Semantic Kernel.
using Microsoft.Extensions.DependencyInjection;
// Using IServiceCollection with ASP.NET Core.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMongoVectorStore(connectionString, databaseName);
Extension methods that take no parameters are also provided. These require an instance of MongoDB.Driver.IMongoDatabase
to be separately registered with the dependency injection container.
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();
You can construct a MongoDB Vector Store instance directly.
using Microsoft.SemanticKernel.Connectors.MongoDB;
using MongoDB.Driver;
var mongoClient = new MongoClient(connectionString);
var database = mongoClient.GetDatabase(databaseName);
var vectorStore = new MongoVectorStore(database);
It is possible to construct a direct reference to a named collection.
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");
Data mapping
The MongoDB Vector Store connector provides a default mapper when mapping data from the data model to storage.
This mapper does a direct conversion of the list of properties on the data model to the fields in MongoDB and uses MongoDB.Bson.Serialization
to convert to the storage schema. This means that usage of the MongoDB.Bson.Serialization.Attributes.BsonElement
is supported if a different storage name to the
data model property name is required. The only exception is the key of the record which is mapped to a database field named _id
, since all MongoDB
records must use this name for ids.
Property name override
For data properties and vector properties, you can provide override field names to use in storage that is different to the property names on the data model. This is not supported for keys, since a key has a fixed name in MongoDB.
The property name override is done by setting the BsonElement
attribute on the data model properties.
Here is an example of a data model with BsonElement
set.
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; }
}
Getting started
Add the MongoDB Atlas Vector Store dependencies to your environment. It needs the pymongo
package which is included in the mongo extra: , you need to install with these extras:
pip install semantic-kernel[mongo]
You can then create the vector store.
from semantic_kernel.connectors.mongodb 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()
Alternatively, you can also pass in your own mongodb client if you want to have more control over the client construction:
from pymongo import AsyncMongoClient
from semantic_kernel.connectors.mongodb import MongoDBAtlasStore
client = AsyncMongoClient(...)
store = MongoDBAtlasStore(mongo_client=client)
When a client is passed in, Semantic Kernel will not close the connection for you, so you need to ensure to close it, for instance with a async with
statement.
You can also create a collection directly, without the store.
from semantic_kernel.connectors.mongodb import MongoDBAtlasCollection
# `hotel` is a class created with the @vectorstoremodel decorator
collection = MongoDBAtlasCollection(
record_type=hotel,
collection_name="my_collection",
)
Serialization
Since the MongoDB Atlas connector needs a simple dict with the fields corresponding to the index as the input, the serialization is quite easy, it only uses a predetermined key _id
, so we replace the key of the data model with that if it is not already _id
.
For more details on this concept see the serialization documentation.
Coming soon
More info coming soon.