diff --git a/dotnet/src/Connectors/Connectors.Memory.AzureAISearch/AzureAISearchKernelBuilderExtensions.cs b/dotnet/src/Connectors/Connectors.Memory.AzureAISearch/AzureAISearchKernelBuilderExtensions.cs
index 5096c1486f1f..9b4c6818fb03 100644
--- a/dotnet/src/Connectors/Connectors.Memory.AzureAISearch/AzureAISearchKernelBuilderExtensions.cs
+++ b/dotnet/src/Connectors/Connectors.Memory.AzureAISearch/AzureAISearchKernelBuilderExtensions.cs
@@ -12,6 +12,7 @@ namespace Microsoft.SemanticKernel;
///
/// Extension methods to register Azure AI Search instances on the .
///
+[Obsolete("Call the corresponding method on the Services property of your IKernelBuilder instance.")]
public static class AzureAISearchKernelBuilderExtensions
{
///
@@ -23,7 +24,7 @@ public static class AzureAISearchKernelBuilderExtensions
/// The kernel builder.
public static IKernelBuilder AddAzureAISearchVectorStore(this IKernelBuilder builder, AzureAISearchVectorStoreOptions? options = default, string? serviceId = default)
{
- builder.Services.AddAzureAISearchVectorStore(options, serviceId);
+ builder.Services.AddKeyedAzureAISearchVectorStore(serviceId, options);
return builder;
}
@@ -38,7 +39,7 @@ public static IKernelBuilder AddAzureAISearchVectorStore(this IKernelBuilder bui
/// The kernel builder.
public static IKernelBuilder AddAzureAISearchVectorStore(this IKernelBuilder builder, Uri endpoint, TokenCredential tokenCredential, AzureAISearchVectorStoreOptions? options = default, string? serviceId = default)
{
- builder.Services.AddAzureAISearchVectorStore(endpoint, tokenCredential, options, serviceId);
+ builder.Services.AddKeyedAzureAISearchVectorStore(serviceId, endpoint, tokenCredential, options);
return builder;
}
@@ -53,7 +54,7 @@ public static IKernelBuilder AddAzureAISearchVectorStore(this IKernelBuilder bui
/// The kernel builder.
public static IKernelBuilder AddAzureAISearchVectorStore(this IKernelBuilder builder, Uri endpoint, AzureKeyCredential credential, AzureAISearchVectorStoreOptions? options = default, string? serviceId = default)
{
- builder.Services.AddAzureAISearchVectorStore(endpoint, credential, options, serviceId);
+ builder.Services.AddKeyedAzureAISearchVectorStore(serviceId, endpoint, credential, options);
return builder;
}
@@ -73,7 +74,7 @@ public static IKernelBuilder AddAzureAISearchVectorStoreRecordCollection? options = default,
string? serviceId = default)
{
- builder.Services.AddAzureAISearchVectorStoreRecordCollection(collectionName, options, serviceId);
+ builder.Services.AddKeyedAzureAISearchVectorStoreRecordCollection(serviceId, collectionName, options);
return builder;
}
@@ -97,7 +98,7 @@ public static IKernelBuilder AddAzureAISearchVectorStoreRecordCollection? options = default,
string? serviceId = default)
{
- builder.Services.AddAzureAISearchVectorStoreRecordCollection(collectionName, endpoint, tokenCredential, options, serviceId);
+ builder.Services.AddKeyedAzureAISearchVectorStoreRecordCollection(serviceId, collectionName, endpoint, tokenCredential, options);
return builder;
}
@@ -121,7 +122,7 @@ public static IKernelBuilder AddAzureAISearchVectorStoreRecordCollection? options = default,
string? serviceId = default)
{
- builder.Services.AddAzureAISearchVectorStoreRecordCollection(collectionName, endpoint, credential, options, serviceId);
+ builder.Services.AddKeyedAzureAISearchVectorStoreRecordCollection(serviceId, collectionName, endpoint, credential, options);
return builder;
}
}
diff --git a/dotnet/src/Connectors/Connectors.Memory.AzureAISearch/AzureAISearchServiceCollectionExtensions.cs b/dotnet/src/Connectors/Connectors.Memory.AzureAISearch/AzureAISearchServiceCollectionExtensions.cs
index 0daa73595cbd..52be400d1c92 100644
--- a/dotnet/src/Connectors/Connectors.Memory.AzureAISearch/AzureAISearchServiceCollectionExtensions.cs
+++ b/dotnet/src/Connectors/Connectors.Memory.AzureAISearch/AzureAISearchServiceCollectionExtensions.cs
@@ -20,229 +20,358 @@ namespace Microsoft.SemanticKernel;
public static class AzureAISearchServiceCollectionExtensions
{
///
- /// Register an Azure AI Search with the specified service ID and where is retrieved from the dependency injection container.
+ /// Registers an Azure AI Search in the , retrieving the from the dependency injection container.
///
- /// The to register the on.
- /// Optional options to further configure the .
- /// An optional service id to use as the service key.
+ /// The to which the vector store should be added.
+ /// Options to further configure the .
+ /// The service lifetime for the store. Defaults to .
/// The service collection.
- public static IServiceCollection AddAzureAISearchVectorStore(this IServiceCollection services, AzureAISearchVectorStoreOptions? options = default, string? serviceId = default)
- {
+ public static IServiceCollection AddAzureAISearchVectorStore(
+ this IServiceCollection serviceCollection,
+ AzureAISearchVectorStoreOptions? options = default,
// If we are not constructing the SearchIndexClient, add the IVectorStore as transient, since we
// cannot make assumptions about how SearchIndexClient is being managed.
- services.AddKeyedTransient(
- serviceId,
- (sp, obj) =>
- {
- var searchIndexClient = sp.GetRequiredService();
- var selectedOptions = options ?? sp.GetService();
-
- return new AzureAISearchVectorStore(
- searchIndexClient,
- selectedOptions);
- });
-
- return services;
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ => AddKeyedAzureAISearchVectorStore(serviceCollection, serviceKey: null, options, lifetime);
+
+ ///
+ /// Registers a keyed Azure AI Search in the , retrieving the from the dependency injection container.
+ ///
+ /// The to which the vector store should be added.
+ /// The key with which to associate the vector store.
+ /// Options to further configure the .
+ /// The service lifetime for the store. Defaults to .
+ /// The service collection.
+ public static IServiceCollection AddKeyedAzureAISearchVectorStore(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
+ AzureAISearchVectorStoreOptions? options = default,
+ // If we are not constructing the SearchIndexClient, add the IVectorStore as transient, since we
+ // cannot make assumptions about how SearchIndexClient is being managed.
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ {
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStore),
+ serviceKey,
+ (serviceProvider, _) => new AzureAISearchVectorStore(
+ serviceProvider.GetRequiredService(),
+ options ?? serviceProvider.GetService()),
+ lifetime));
+
+ return serviceCollection;
}
///
- /// Register an Azure AI Search with the provided and and the specified service ID.
+ /// Registers an Azure AI Search in the , using the provided and .
///
- /// The to register the on.
+ /// The to which the vector store should be added.
/// The service endpoint for Azure AI Search.
/// The credential to authenticate to Azure AI Search with.
- /// Optional options to further configure the .
- /// An optional service id to use as the service key.
+ /// Options to further configure the .
+ /// The service lifetime for the store. Defaults to .
/// The service collection.
- public static IServiceCollection AddAzureAISearchVectorStore(this IServiceCollection services, Uri endpoint, TokenCredential tokenCredential, AzureAISearchVectorStoreOptions? options = default, string? serviceId = default)
+ public static IServiceCollection AddAzureAISearchVectorStore(
+ this IServiceCollection serviceCollection,
+ Uri endpoint,
+ TokenCredential tokenCredential,
+ AzureAISearchVectorStoreOptions? options = default,
+ ServiceLifetime lifetime = ServiceLifetime.Singleton)
+ => AddKeyedAzureAISearchVectorStore(serviceCollection, serviceKey: null, endpoint, tokenCredential, options, lifetime);
+
+ ///
+ /// Registers a keyed Azure AI Search in the , using the provided and .
+ ///
+ /// The to which the vector store should be added.
+ /// The key with which to associate the vector store.
+ /// The service endpoint for Azure AI Search.
+ /// The credential to authenticate to Azure AI Search with.
+ /// Options to further configure the .
+ /// The service lifetime for the store. Defaults to .
+ /// The service collection.
+ public static IServiceCollection AddKeyedAzureAISearchVectorStore(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
+ Uri endpoint,
+ TokenCredential tokenCredential,
+ AzureAISearchVectorStoreOptions? options = default,
+ ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
Verify.NotNull(endpoint);
Verify.NotNull(tokenCredential);
- services.AddKeyedSingleton(
- serviceId,
- (sp, obj) =>
- {
- var selectedOptions = options ?? sp.GetService();
- var searchClientOptions = BuildSearchClientOptions(selectedOptions?.JsonSerializerOptions);
- var searchIndexClient = new SearchIndexClient(endpoint, tokenCredential, searchClientOptions);
-
- // Construct the vector store.
- return new AzureAISearchVectorStore(
- searchIndexClient,
- selectedOptions);
- });
-
- return services;
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStore),
+ serviceKey,
+ (serviceProvider, _) =>
+ {
+ options ??= serviceProvider.GetService();
+ var searchClientOptions = BuildSearchClientOptions(options?.JsonSerializerOptions);
+ var searchIndexClient = new SearchIndexClient(endpoint, tokenCredential, searchClientOptions);
+
+ // Construct the vector store.
+ return new AzureAISearchVectorStore(searchIndexClient, options);
+ },
+ lifetime));
+
+ return serviceCollection;
}
///
- /// Register an Azure AI Search with the provided and and the specified service ID.
+ /// Registers an Azure AI Search in the , using the provided and .
///
- /// The to register the on.
+ /// The to which the vector store should be added.
/// The service endpoint for Azure AI Search.
/// The credential to authenticate to Azure AI Search with.
- /// Optional options to further configure the .
- /// An optional service id to use as the service key.
+ /// Options to further configure the .
+ /// The service lifetime for the store. Defaults to .
/// The service collection.
- public static IServiceCollection AddAzureAISearchVectorStore(this IServiceCollection services, Uri endpoint, AzureKeyCredential credential, AzureAISearchVectorStoreOptions? options = default, string? serviceId = default)
+ public static IServiceCollection AddAzureAISearchVectorStore(
+ this IServiceCollection serviceCollection,
+ Uri endpoint,
+ AzureKeyCredential credential,
+ AzureAISearchVectorStoreOptions? options = default,
+ ServiceLifetime lifetime = ServiceLifetime.Singleton)
+ => AddKeyedAzureAISearchVectorStore(serviceCollection, serviceKey: null, endpoint, credential, options, lifetime);
+
+ ///
+ /// Registers a keyed Azure AI Search in the , using the provided and .
+ ///
+ /// The to which the vector store should be added.
+ /// The key with which to associate the vector store.
+ /// The service endpoint for Azure AI Search.
+ /// The credential to authenticate to Azure AI Search with.
+ /// Options to further configure the .
+ /// The service lifetime for the store. Defaults to .
+ /// The service collection.
+ public static IServiceCollection AddKeyedAzureAISearchVectorStore(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
+ Uri endpoint,
+ AzureKeyCredential credential,
+ AzureAISearchVectorStoreOptions? options = default,
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
{
Verify.NotNull(endpoint);
Verify.NotNull(credential);
- services.AddKeyedSingleton(
- serviceId,
- (sp, obj) =>
- {
- var selectedOptions = options ?? sp.GetService();
- var searchClientOptions = BuildSearchClientOptions(selectedOptions?.JsonSerializerOptions);
- var searchIndexClient = new SearchIndexClient(endpoint, credential, searchClientOptions);
-
- // Construct the vector store.
- return new AzureAISearchVectorStore(
- searchIndexClient,
- selectedOptions);
- });
-
- return services;
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStore),
+ serviceKey,
+ (serviceProvider, _) =>
+ {
+ options ??= serviceProvider.GetService();
+ var searchClientOptions = BuildSearchClientOptions(options?.JsonSerializerOptions);
+ var searchIndexClient = new SearchIndexClient(endpoint, credential, searchClientOptions);
+
+ // Construct the vector store.
+ return new AzureAISearchVectorStore(searchIndexClient, options);
+ },
+ lifetime));
+
+ return serviceCollection;
}
///
- /// Register an Azure AI Search , and with the
- /// specified service ID and where is retrieved from the dependency injection container.
+ /// Registers an Azure AI Search , and ,
+ /// retrieving the from the dependency injection container.
///
/// The type of the data model that the collection should contain.
- /// The to register the on.
+ /// The to which the vector store should be added.
/// The name of the collection that this will access.
- /// Optional configuration options to pass to the .
- /// An optional service id to use as the service key.
+ /// Configuration options to pass to the .
+ /// The service lifetime for the collection. Defaults to .
/// The service collection.
public static IServiceCollection AddAzureAISearchVectorStoreRecordCollection(
- this IServiceCollection services,
+ this IServiceCollection serviceCollection,
+ string collectionName,
+ AzureAISearchVectorStoreRecordCollectionOptions? options = default,
+ // If we are not constructing the SearchIndexClient, add the IVectorStore as transient, since we
+ // cannot make assumptions about how SearchIndexClient is being managed.
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ => AddKeyedAzureAISearchVectorStoreRecordCollection(serviceCollection, serviceKey: null, collectionName, options, lifetime);
+
+ ///
+ /// Registers a keyed Azure AI Search , and ,
+ /// retrieving the from the dependency injection container.
+ ///
+ /// The type of the data model that the collection should contain.
+ /// The to which the vector store should be added.
+ /// The key with which to associate the vector store.
+ /// The name of the collection that this will access.
+ /// Configuration options to pass to the .
+ /// The service lifetime for the collection. Defaults to .
+ /// The service collection.
+ public static IServiceCollection AddKeyedAzureAISearchVectorStoreRecordCollection(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
string collectionName,
AzureAISearchVectorStoreRecordCollectionOptions? options = default,
- string? serviceId = default)
- {
// If we are not constructing the SearchIndexClient, add the IVectorStore as transient, since we
// cannot make assumptions about how SearchIndexClient is being managed.
- services.AddKeyedTransient>(
- serviceId,
- (sp, obj) =>
- {
- var searchIndexClient = sp.GetRequiredService();
- var selectedOptions = options ?? sp.GetService>();
-
- return new AzureAISearchVectorStoreRecordCollection(
- searchIndexClient,
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ {
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStoreRecordCollection),
+ serviceKey,
+ (serviceProvider, _) => new AzureAISearchVectorStoreRecordCollection(
+ serviceProvider.GetRequiredService(),
collectionName,
- selectedOptions);
- });
+ options ?? serviceProvider.GetService>()),
+ lifetime));
- AddVectorizedSearch(services, serviceId);
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorizedSearch),
+ serviceKey,
+ static (serviceProvider, serviceKey) => serviceProvider.GetRequiredKeyedService>(serviceKey),
+ lifetime));
- return services;
+ return serviceCollection;
}
///
- /// Register an Azure AI Search , and with the
- /// provided and and the specified service ID.
+ /// Registers an Azure AI Search , and with the
+ /// provided and .
///
/// The type of the data model that the collection should contain.
- /// The to register the on.
+ /// The to which the vector store should be added.
/// The name of the collection that this will access.
/// The service endpoint for Azure AI Search.
/// The credential to authenticate to Azure AI Search with.
/// Optional configuration options to pass to the .
- /// An optional service id to use as the service key.
+ /// The service lifetime for the collection. Defaults to .
/// The service collection.
public static IServiceCollection AddAzureAISearchVectorStoreRecordCollection(
- this IServiceCollection services,
+ this IServiceCollection serviceCollection,
string collectionName,
Uri endpoint,
TokenCredential tokenCredential,
AzureAISearchVectorStoreRecordCollectionOptions? options = default,
- string? serviceId = default)
+ ServiceLifetime lifetime = ServiceLifetime.Singleton)
+ => AddKeyedAzureAISearchVectorStoreRecordCollection(serviceCollection, serviceKey: null, collectionName, endpoint, tokenCredential, options, lifetime);
+
+ ///
+ /// Registers a keyed Azure AI Search , and with the
+ /// provided and .
+ ///
+ /// The type of the data model that the collection should contain.
+ /// The to which the vector store should be added.
+ /// The key with which to associate the vector store.
+ /// The name of the collection that this will access.
+ /// The service endpoint for Azure AI Search.
+ /// The credential to authenticate to Azure AI Search with.
+ /// Optional configuration options to pass to the .
+ /// The service lifetime for the collection. Defaults to .
+ /// The service collection.
+ public static IServiceCollection AddKeyedAzureAISearchVectorStoreRecordCollection(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
+ string collectionName,
+ Uri endpoint,
+ TokenCredential tokenCredential,
+ AzureAISearchVectorStoreRecordCollectionOptions? options = default,
+ ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
Verify.NotNull(endpoint);
Verify.NotNull(tokenCredential);
- services.AddKeyedSingleton>(
- serviceId,
- (sp, obj) =>
- {
- var selectedOptions = options ?? sp.GetService>();
- var searchClientOptions = BuildSearchClientOptions(selectedOptions?.JsonSerializerOptions);
- var searchIndexClient = new SearchIndexClient(endpoint, tokenCredential, searchClientOptions);
-
- // Construct the vector store.
- return new AzureAISearchVectorStoreRecordCollection(
- searchIndexClient,
- collectionName,
- selectedOptions);
- });
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStoreRecordCollection),
+ serviceKey,
+ (serviceProvider, _) =>
+ {
+ options ??= serviceProvider.GetService>();
+ var searchClientOptions = BuildSearchClientOptions(options?.JsonSerializerOptions);
+ var searchIndexClient = new SearchIndexClient(endpoint, tokenCredential, searchClientOptions);
- AddVectorizedSearch(services, serviceId);
+ // Construct the vector store.
+ return new AzureAISearchVectorStoreRecordCollection(searchIndexClient, collectionName, options);
+ },
+ lifetime));
- return services;
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorizedSearch),
+ serviceKey,
+ static (serviceProvider, serviceKey) => serviceProvider.GetRequiredKeyedService>(serviceKey),
+ lifetime));
+
+ return serviceCollection;
}
///
- /// Register an Azure AI Search , and with the
- /// provided and and the specified service ID.
+ /// Registers an Azure AI Search , and with the
+ /// provided and .
///
/// The type of the data model that the collection should contain.
- /// The to register the on.
+ /// The to register the on.
/// The name of the collection that this will access.
/// The service endpoint for Azure AI Search.
/// The credential to authenticate to Azure AI Search with.
/// Optional configuration options to pass to the .
- /// An optional service id to use as the service key.
+ /// The service lifetime for the collection. Defaults to .
/// The service collection.
public static IServiceCollection AddAzureAISearchVectorStoreRecordCollection(
- this IServiceCollection services,
+ this IServiceCollection serviceCollection,
string collectionName,
Uri endpoint,
AzureKeyCredential credential,
AzureAISearchVectorStoreRecordCollectionOptions? options = default,
- string? serviceId = default)
+ ServiceLifetime lifetime = ServiceLifetime.Singleton)
+ => AddKeyedAzureAISearchVectorStoreRecordCollection(serviceCollection, serviceKey: null, collectionName, endpoint, credential, options, lifetime);
+
+ ///
+ /// Registers a keyed Azure AI Search , and with the
+ /// provided and .
+ ///
+ /// The type of the data model that the collection should contain.
+ /// The to register the on.
+ /// The key with which to associate the vector store.
+ /// The name of the collection that this will access.
+ /// The service endpoint for Azure AI Search.
+ /// The credential to authenticate to Azure AI Search with.
+ /// Optional configuration options to pass to the .
+ /// The service lifetime for the collection. Defaults to .
+ /// The service collection.
+ public static IServiceCollection AddKeyedAzureAISearchVectorStoreRecordCollection(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
+ string collectionName,
+ Uri endpoint,
+ AzureKeyCredential credential,
+ AzureAISearchVectorStoreRecordCollectionOptions? options = default,
+ ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
Verify.NotNull(endpoint);
Verify.NotNull(credential);
- services.AddKeyedSingleton>(
- serviceId,
- (sp, obj) =>
- {
- var selectedOptions = options ?? sp.GetService>();
- var searchClientOptions = BuildSearchClientOptions(selectedOptions?.JsonSerializerOptions);
- var searchIndexClient = new SearchIndexClient(endpoint, credential, searchClientOptions);
-
- // Construct the vector store.
- return new AzureAISearchVectorStoreRecordCollection(
- searchIndexClient,
- collectionName,
- selectedOptions);
- });
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStoreRecordCollection),
+ serviceKey,
+ (serviceProvider, _) =>
+ {
+ var selectedOptions = options ?? serviceProvider.GetService>();
+ var searchClientOptions = BuildSearchClientOptions(selectedOptions?.JsonSerializerOptions);
+ var searchIndexClient = new SearchIndexClient(endpoint, credential, searchClientOptions);
- AddVectorizedSearch(services, serviceId);
+ // Construct the vector store.
+ return new AzureAISearchVectorStoreRecordCollection(searchIndexClient, collectionName, selectedOptions);
+ },
+ lifetime));
- return services;
- }
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorizedSearch),
+ serviceKey, static (serviceProvider, serviceKey) => serviceProvider.GetRequiredKeyedService>(serviceKey),
+ lifetime));
- ///
- /// Also register the with the given as a .
- ///
- /// The type of the data model that the collection should contain.
- /// The service collection to register on.
- /// The service id that the registrations should use.
- private static void AddVectorizedSearch(IServiceCollection services, string? serviceId)
- {
- services.AddKeyedTransient>(
- serviceId,
- (sp, obj) =>
- {
- return sp.GetRequiredKeyedService>(serviceId);
- });
+ return serviceCollection;
}
///
@@ -252,8 +381,11 @@ private static void AddVectorizedSearch(IServiceCollection services, st
/// The .
private static SearchClientOptions BuildSearchClientOptions(JsonSerializerOptions? jsonSerializerOptions)
{
- var searchClientOptions = new SearchClientOptions();
- searchClientOptions.Diagnostics.ApplicationId = HttpHeaderConstant.Values.UserAgent;
+ var searchClientOptions = new SearchClientOptions
+ {
+ Diagnostics = { ApplicationId = HttpHeaderConstant.Values.UserAgent }
+ };
+
if (jsonSerializerOptions != null)
{
searchClientOptions.Serializer = new JsonObjectSerializer(jsonSerializerOptions);
diff --git a/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBMongoDB/AzureCosmosDBMongoDBKernelBuilderExtensions.cs b/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBMongoDB/AzureCosmosDBMongoDBKernelBuilderExtensions.cs
index af73629568ec..150ea19c26f9 100644
--- a/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBMongoDB/AzureCosmosDBMongoDBKernelBuilderExtensions.cs
+++ b/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBMongoDB/AzureCosmosDBMongoDBKernelBuilderExtensions.cs
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
+using System;
using Microsoft.Extensions.VectorData;
using Microsoft.SemanticKernel.Connectors.AzureCosmosDBMongoDB;
using MongoDB.Driver;
@@ -9,6 +10,7 @@ namespace Microsoft.SemanticKernel;
///
/// Extension methods to register Azure CosmosDB MongoDB instances on the .
///
+[Obsolete("Call the corresponding method on the Services property of your IKernelBuilder instance.")]
public static class AzureCosmosDBMongoDBKernelBuilderExtensions
{
///
@@ -24,7 +26,7 @@ public static IKernelBuilder AddAzureCosmosDBMongoDBVectorStore(
AzureCosmosDBMongoDBVectorStoreOptions? options = default,
string? serviceId = default)
{
- builder.Services.AddAzureCosmosDBMongoDBVectorStore(options, serviceId);
+ builder.Services.AddKeyedAzureCosmosDBMongoDBVectorStore(serviceId, options);
return builder;
}
@@ -45,7 +47,7 @@ public static IKernelBuilder AddAzureCosmosDBMongoDBVectorStore(
AzureCosmosDBMongoDBVectorStoreOptions? options = default,
string? serviceId = default)
{
- builder.Services.AddAzureCosmosDBMongoDBVectorStore(connectionString, databaseName, options, serviceId);
+ builder.Services.AddKeyedAzureCosmosDBMongoDBVectorStore(serviceId, connectionString, databaseName, options);
return builder;
}
@@ -65,7 +67,7 @@ public static IKernelBuilder AddAzureCosmosDBMongoDBVectorStoreRecordCollection<
AzureCosmosDBMongoDBVectorStoreRecordCollectionOptions? options = default,
string? serviceId = default)
{
- builder.Services.AddAzureCosmosDBMongoDBVectorStoreRecordCollection(collectionName, options, serviceId);
+ builder.Services.AddKeyedAzureCosmosDBMongoDBVectorStoreRecordCollection(serviceId, collectionName, options);
return builder;
}
@@ -89,7 +91,7 @@ public static IKernelBuilder AddAzureCosmosDBMongoDBVectorStoreRecordCollection<
AzureCosmosDBMongoDBVectorStoreRecordCollectionOptions? options = default,
string? serviceId = default)
{
- builder.Services.AddAzureCosmosDBMongoDBVectorStoreRecordCollection(collectionName, connectionString, databaseName, options, serviceId);
+ builder.Services.AddKeyedAzureCosmosDBMongoDBVectorStoreRecordCollection(serviceId, collectionName, connectionString, databaseName, options);
return builder;
}
}
diff --git a/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBMongoDB/AzureCosmosDBMongoDBServiceCollectionExtensions.cs b/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBMongoDB/AzureCosmosDBMongoDBServiceCollectionExtensions.cs
index f4f77082a271..25f8db3263b9 100644
--- a/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBMongoDB/AzureCosmosDBMongoDBServiceCollectionExtensions.cs
+++ b/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBMongoDB/AzureCosmosDBMongoDBServiceCollectionExtensions.cs
@@ -14,154 +14,231 @@ namespace Microsoft.SemanticKernel;
public static class AzureCosmosDBMongoDBServiceCollectionExtensions
{
///
- /// Register a Azure CosmosDB MongoDB with the specified service ID
- /// and where the Azure CosmosDB MongoDB is retrieved from the dependency injection container.
+ /// Registers an Azure CosmosDB MongoDB , retrieving the from the dependency injection container.
///
- /// The to register the on.
- /// Optional options to further configure the .
- /// An optional service id to use as the service key.
+ /// The to which the vector store should be added.
+ /// Options to further configure the .
+ /// The service lifetime for the store. Defaults to .
/// Service collection.
public static IServiceCollection AddAzureCosmosDBMongoDBVectorStore(
- this IServiceCollection services,
+ this IServiceCollection serviceCollection,
AzureCosmosDBMongoDBVectorStoreOptions? options = default,
- string? serviceId = default)
- {
// If we are not constructing MongoDatabase, add the IVectorStore as transient, since we
// cannot make assumptions about how MongoDatabase is being managed.
- services.AddKeyedTransient(
- serviceId,
- (sp, obj) =>
- {
- var database = sp.GetRequiredService();
- var selectedOptions = options ?? sp.GetService();
-
- return new AzureCosmosDBMongoDBVectorStore(database, options);
- });
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ => AddKeyedAzureCosmosDBMongoDBVectorStore(serviceCollection, serviceKey: null, options, lifetime);
- return services;
+ ///
+ /// Registers a keyed Azure CosmosDB MongoDB , retrieving the from the dependency injection container.
+ ///
+ /// The to which the vector store should be added.
+ /// The key with which to associate the vector store.
+ /// Options to further configure the .
+ /// The service lifetime for the store. Defaults to .
+ /// Service collection.
+ public static IServiceCollection AddKeyedAzureCosmosDBMongoDBVectorStore(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
+ AzureCosmosDBMongoDBVectorStoreOptions? options = default,
+ // If we are not constructing MongoDatabase, add the IVectorStore as transient, since we
+ // cannot make assumptions about how MongoDatabase is being managed.
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ {
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStore),
+ serviceKey,
+ (serviceProvider, _) => new AzureCosmosDBMongoDBVectorStore(
+ serviceProvider.GetRequiredService(),
+ options ?? serviceProvider.GetService()),
+ lifetime));
+
+ return serviceCollection;
}
///
- /// Register a Azure CosmosDB MongoDB with the specified service ID
- /// and where the Azure CosmosDB MongoDB is constructed using the provided and .
+ /// Registers an Azure CosmosDB MongoDB , using the provided and .
///
- /// The to register the on.
+ /// The to which the vector store should be added.
/// Connection string required to connect to Azure CosmosDB MongoDB.
/// Database name for Azure CosmosDB MongoDB.
- /// Optional options to further configure the .
- /// An optional service id to use as the service key.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
/// Service collection.
public static IServiceCollection AddAzureCosmosDBMongoDBVectorStore(
- this IServiceCollection services,
+ this IServiceCollection serviceCollection,
string connectionString,
string databaseName,
AzureCosmosDBMongoDBVectorStoreOptions? options = default,
- string? serviceId = default)
- {
// If we are constructing IMongoDatabase, add the IVectorStore as singleton, since we are managing the lifetime of it,
// and the recommendation from Mongo is to register it with a singleton lifetime.
- services.AddKeyedSingleton(
- serviceId,
- (sp, obj) =>
- {
- var settings = MongoClientSettings.FromConnectionString(connectionString);
- settings.ApplicationName = HttpHeaderConstant.Values.UserAgent;
+ ServiceLifetime lifetime = ServiceLifetime.Singleton)
+ => AddKeyedAzureCosmosDBMongoDBVectorStore(serviceCollection, serviceKey: null, connectionString, databaseName, options, lifetime);
- var mongoClient = new MongoClient(settings);
- var database = mongoClient.GetDatabase(databaseName);
+ ///
+ /// Registers a keyed Azure CosmosDB MongoDB , using the provided and .
+ ///
+ /// The to which the vector store should be added.
+ /// The key with which to associate the vector store.
+ /// Connection string required to connect to Azure CosmosDB MongoDB.
+ /// Database name for Azure CosmosDB MongoDB.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
+ /// Service collection.
+ public static IServiceCollection AddKeyedAzureCosmosDBMongoDBVectorStore(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
+ string connectionString,
+ string databaseName,
+ AzureCosmosDBMongoDBVectorStoreOptions? options = default,
+ // If we are constructing IMongoDatabase, add the IVectorStore as singleton, since we are managing the lifetime of it,
+ // and the recommendation from Mongo is to register it with a singleton lifetime.
+ ServiceLifetime lifetime = ServiceLifetime.Singleton)
+ {
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStore),
+ serviceKey,
+ (serviceProvider, _) =>
+ {
+ var settings = MongoClientSettings.FromConnectionString(connectionString);
+ settings.ApplicationName = HttpHeaderConstant.Values.UserAgent;
+
+ var mongoClient = new MongoClient(settings);
+ var database = mongoClient.GetDatabase(databaseName);
- var selectedOptions = options ?? sp.GetService();
+ options ??= serviceProvider.GetService();
- return new AzureCosmosDBMongoDBVectorStore(database, options);
- });
+ return new AzureCosmosDBMongoDBVectorStore(database, options);
+ },
+ lifetime));
- return services;
+ return serviceCollection;
}
///
- /// Register an Azure CosmosDB MongoDB and with the specified service ID
- /// and where the Azure CosmosDB MongoDB is retrieved from the dependency injection container.
+ /// Registers an Azure CosmosDB MongoDB and ,
+ /// retrieving the is retrieved from the dependency injection container.
///
/// The type of the record.
- /// The to register the on.
+ /// The to which the vector store should be added.
/// The name of the collection.
- /// Optional options to further configure the .
- /// An optional service id to use as the service key.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
/// Service collection.
public static IServiceCollection AddAzureCosmosDBMongoDBVectorStoreRecordCollection(
- this IServiceCollection services,
+ this IServiceCollection serviceCollection,
string collectionName,
AzureCosmosDBMongoDBVectorStoreRecordCollectionOptions? options = default,
- string? serviceId = default)
- {
- services.AddKeyedTransient>(
- serviceId,
- (sp, obj) =>
- {
- var database = sp.GetRequiredService();
- var selectedOptions = options ?? sp.GetService>();
-
- return new AzureCosmosDBMongoDBVectorStoreRecordCollection(database, collectionName, selectedOptions);
- });
-
- AddVectorizedSearch(services, serviceId);
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ => AddKeyedAzureCosmosDBMongoDBVectorStoreRecordCollection(serviceCollection, serviceKey: null, collectionName, options, lifetime);
- return services;
+ ///
+ /// Registers a keyed Azure CosmosDB MongoDB and ,
+ /// retrieving the is retrieved from the dependency injection container.
+ ///
+ /// The type of the record.
+ /// The to which the vector store should be added.
+ /// The key with which to associate the vector store.
+ /// The name of the collection.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
+ /// Service collection.
+ public static IServiceCollection AddKeyedAzureCosmosDBMongoDBVectorStoreRecordCollection(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
+ string collectionName,
+ AzureCosmosDBMongoDBVectorStoreRecordCollectionOptions? options = default,
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ {
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStoreRecordCollection),
+ serviceKey,
+ (serviceProvider, _) => new AzureCosmosDBMongoDBVectorStoreRecordCollection(
+ serviceProvider.GetRequiredService(),
+ collectionName,
+ options ?? serviceProvider.GetService>()),
+ lifetime));
+
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorizedSearch),
+ serviceKey,
+ static (serviceProvider, serviceKey) => serviceProvider.GetRequiredKeyedService>(serviceKey),
+ lifetime));
+
+ return serviceCollection;
}
///
- /// Register an Azure CosmosDB MongoDB and with the specified service ID
- /// and where the Azure CosmosDB MongoDB is constructed using the provided and .
+ /// Registers an Azure CosmosDB MongoDB and
+ /// using the provided and .
///
/// The type of the record.
- /// The to register the on.
+ /// The to which the vector store should be added.
/// The name of the collection.
/// Connection string required to connect to Azure CosmosDB MongoDB.
/// Database name for Azure CosmosDB MongoDB.
- /// Optional options to further configure the .
- /// An optional service id to use as the service key.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
/// Service collection.
public static IServiceCollection AddAzureCosmosDBMongoDBVectorStoreRecordCollection(
- this IServiceCollection services,
+ this IServiceCollection serviceCollection,
string collectionName,
string connectionString,
string databaseName,
AzureCosmosDBMongoDBVectorStoreRecordCollectionOptions? options = default,
- string? serviceId = default)
- {
- services.AddKeyedSingleton>(
- serviceId,
- (sp, obj) =>
- {
- var settings = MongoClientSettings.FromConnectionString(connectionString);
- settings.ApplicationName = HttpHeaderConstant.Values.UserAgent;
-
- var mongoClient = new MongoClient(settings);
- var database = mongoClient.GetDatabase(databaseName);
-
- var selectedOptions = options ?? sp.GetService>();
-
- return new AzureCosmosDBMongoDBVectorStoreRecordCollection(database, collectionName, selectedOptions);
- });
-
- AddVectorizedSearch(services, serviceId);
-
- return services;
- }
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ => AddKeyedAzureCosmosDBMongoDBVectorStoreRecordCollection(serviceCollection, serviceKey: null, collectionName, connectionString, databaseName, options, lifetime);
///
- /// Also register the with the given as a .
+ /// Registers a keyed Azure CosmosDB MongoDB and
+ /// using the provided and .
///
- /// The type of the data model that the collection should contain.
- /// The service collection to register on.
- /// The service id that the registrations should use.
- private static void AddVectorizedSearch(IServiceCollection services, string? serviceId)
+ /// The type of the record.
+ /// The to which the vector store should be added.
+ /// The key with which to associate the vector store.
+ /// The name of the collection.
+ /// Connection string required to connect to Azure CosmosDB MongoDB.
+ /// Database name for Azure CosmosDB MongoDB.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
+ /// Service collection.
+ public static IServiceCollection AddKeyedAzureCosmosDBMongoDBVectorStoreRecordCollection(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
+ string collectionName,
+ string connectionString,
+ string databaseName,
+ AzureCosmosDBMongoDBVectorStoreRecordCollectionOptions? options = default,
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
{
- services.AddKeyedTransient>(
- serviceId,
- (sp, obj) =>
- {
- return sp.GetRequiredKeyedService>(serviceId);
- });
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStoreRecordCollection),
+ serviceKey,
+ (serviceProvider, _) =>
+ {
+ var settings = MongoClientSettings.FromConnectionString(connectionString);
+ settings.ApplicationName = HttpHeaderConstant.Values.UserAgent;
+
+ var mongoClient = new MongoClient(settings);
+ var database = mongoClient.GetDatabase(databaseName);
+
+ options ??= serviceProvider.GetService>();
+
+ return new AzureCosmosDBMongoDBVectorStoreRecordCollection(database, collectionName, options);
+ },
+ lifetime));
+
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorizedSearch),
+ serviceKey,
+ static (serviceProvider, serviceKey) => serviceProvider.GetRequiredKeyedService>(serviceKey),
+ lifetime));
+
+ return serviceCollection;
}
}
diff --git a/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLKernelBuilderExtensions.cs b/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLKernelBuilderExtensions.cs
index 12f7c0118538..3143d53acda9 100644
--- a/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLKernelBuilderExtensions.cs
+++ b/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLKernelBuilderExtensions.cs
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
+using System;
using Microsoft.Azure.Cosmos;
using Microsoft.Extensions.VectorData;
using Microsoft.SemanticKernel.Connectors.AzureCosmosDBNoSQL;
@@ -9,6 +10,7 @@ namespace Microsoft.SemanticKernel;
///
/// Extension methods to register Azure CosmosDB NoSQL instances on the .
///
+[Obsolete("Call the corresponding method on the Services property of your IKernelBuilder instance.")]
public static class AzureCosmosDBNoSQLKernelBuilderExtensions
{
///
@@ -24,7 +26,7 @@ public static IKernelBuilder AddAzureCosmosDBNoSQLVectorStore(
AzureCosmosDBNoSQLVectorStoreOptions? options = default,
string? serviceId = default)
{
- builder.Services.AddAzureCosmosDBNoSQLVectorStore(options, serviceId);
+ builder.Services.AddKeyedAzureCosmosDBNoSQLVectorStore(serviceId, options);
return builder;
}
@@ -45,11 +47,11 @@ public static IKernelBuilder AddAzureCosmosDBNoSQLVectorStore(
AzureCosmosDBNoSQLVectorStoreOptions? options = default,
string? serviceId = default)
{
- builder.Services.AddAzureCosmosDBNoSQLVectorStore(
+ builder.Services.AddKeyedAzureCosmosDBNoSQLVectorStore(
+ serviceId,
connectionString,
databaseName,
- options,
- serviceId);
+ options);
return builder;
}
@@ -70,7 +72,7 @@ public static IKernelBuilder AddAzureCosmosDBNoSQLVectorStoreRecordCollection? options = default,
string? serviceId = default)
{
- builder.Services.AddAzureCosmosDBNoSQLVectorStoreRecordCollection(collectionName, options, serviceId);
+ builder.Services.AddKeyedAzureCosmosDBNoSQLVectorStoreRecordCollection(serviceId, collectionName, options);
return builder;
}
@@ -94,7 +96,7 @@ public static IKernelBuilder AddAzureCosmosDBNoSQLVectorStoreRecordCollection? options = default,
string? serviceId = default)
{
- builder.Services.AddAzureCosmosDBNoSQLVectorStoreRecordCollection(collectionName, connectionString, databaseName, options, serviceId);
+ builder.Services.AddKeyedAzureCosmosDBNoSQLVectorStoreRecordCollection(serviceId, collectionName, connectionString, databaseName, options);
return builder;
}
}
diff --git a/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLServiceCollectionExtensions.cs b/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLServiceCollectionExtensions.cs
index 1c70d360ee62..2e93943d6347 100644
--- a/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLServiceCollectionExtensions.cs
+++ b/dotnet/src/Connectors/Connectors.Memory.AzureCosmosDBNoSQL/AzureCosmosDBNoSQLServiceCollectionExtensions.cs
@@ -15,155 +15,231 @@ namespace Microsoft.SemanticKernel;
public static class AzureCosmosDBNoSQLServiceCollectionExtensions
{
///
- /// Register an Azure CosmosDB NoSQL with the specified service ID
- /// and where the Azure CosmosDB NoSQL is retrieved from the dependency injection container.
+ /// Registers an Azure CosmosDB NoSQL , retrieving the is retrieved from the dependency injection container.
///
- /// The to register the on.
- /// Optional options to further configure the .
- /// An optional service id to use as the service key.
+ /// The to which the vector store should be added.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
/// Service collection.
public static IServiceCollection AddAzureCosmosDBNoSQLVectorStore(
- this IServiceCollection services,
+ this IServiceCollection serviceCollection,
AzureCosmosDBNoSQLVectorStoreOptions? options = default,
- string? serviceId = default)
- {
// If we are not constructing Database, add the IVectorStore as transient, since we
// cannot make assumptions about how Database is being managed.
- services.AddKeyedTransient(
- serviceId,
- (sp, obj) =>
- {
- var database = sp.GetRequiredService();
- var selectedOptions = options ?? sp.GetService();
-
- return new AzureCosmosDBNoSQLVectorStore(database, options);
- });
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ => AddKeyedAzureCosmosDBNoSQLVectorStore(serviceCollection, serviceKey: null, options, lifetime);
- return services;
+ ///
+ /// Registers a keyed Azure CosmosDB NoSQL , retrieving the is retrieved from the dependency injection container.
+ ///
+ /// The to which the vector store should be added.
+ /// The key with which to associate the vector store.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
+ /// Service collection.
+ public static IServiceCollection AddKeyedAzureCosmosDBNoSQLVectorStore(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
+ AzureCosmosDBNoSQLVectorStoreOptions? options = default,
+ // If we are not constructing Database, add the IVectorStore as transient, since we
+ // cannot make assumptions about how Database is being managed.
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ {
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStore),
+ serviceKey,
+ (serviceProvider, _) => new AzureCosmosDBNoSQLVectorStore(
+ serviceProvider.GetRequiredService(),
+ options ?? serviceProvider.GetService()),
+ lifetime));
+
+ return serviceCollection;
}
///
- /// Register an Azure CosmosDB NoSQL with the specified service ID
- /// and where the Azure CosmosDB NoSQL is constructed using the provided and .
+ /// Registers an Azure CosmosDB NoSQL , using the provided and .
///
- /// The to register the on.
+ /// The to which the vector store should be added.
/// Connection string required to connect to Azure CosmosDB NoSQL.
/// Database name for Azure CosmosDB NoSQL.
- /// Optional options to further configure the .
- /// An optional service id to use as the service key.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
/// Service collection.
public static IServiceCollection AddAzureCosmosDBNoSQLVectorStore(
- this IServiceCollection services,
+ this IServiceCollection serviceCollection,
+ string connectionString,
+ string databaseName,
+ AzureCosmosDBNoSQLVectorStoreOptions? options = default,
+ // If we are constructing Database, add the IVectorStore as singleton.
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ => AddKeyedAzureCosmosDBNoSQLVectorStore(serviceCollection, serviceKey: null, connectionString, databaseName, options, lifetime);
+
+ ///
+ /// Registers a keyed Azure CosmosDB NoSQL , using the provided and .
+ ///
+ /// The to which the vector store should be added.
+ /// The key with which to associate the vector store.
+ /// Connection string required to connect to Azure CosmosDB NoSQL.
+ /// Database name for Azure CosmosDB NoSQL.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
+ /// Service collection.
+ public static IServiceCollection AddKeyedAzureCosmosDBNoSQLVectorStore(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
string connectionString,
string databaseName,
AzureCosmosDBNoSQLVectorStoreOptions? options = default,
- string? serviceId = default)
- {
// If we are constructing Database, add the IVectorStore as singleton.
- services.AddKeyedSingleton(
- serviceId,
- (sp, obj) =>
- {
- var cosmosClient = new CosmosClient(connectionString, new()
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ {
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStore),
+ serviceKey,
+ (serviceProvider, _) =>
{
- ApplicationName = HttpHeaderConstant.Values.UserAgent,
- UseSystemTextJsonSerializerWithOptions = options?.JsonSerializerOptions ?? JsonSerializerOptions.Default,
- });
+ var cosmosClient = new CosmosClient(connectionString, new()
+ {
+ ApplicationName = HttpHeaderConstant.Values.UserAgent,
+ UseSystemTextJsonSerializerWithOptions = options?.JsonSerializerOptions ?? JsonSerializerOptions.Default,
+ });
- var database = cosmosClient.GetDatabase(databaseName);
- var selectedOptions = options ?? sp.GetService();
+ var database = cosmosClient.GetDatabase(databaseName);
+ options ??= serviceProvider.GetService();
- return new AzureCosmosDBNoSQLVectorStore(database, options);
- });
+ return new AzureCosmosDBNoSQLVectorStore(database, options);
+ },
+ lifetime));
- return services;
+ return serviceCollection;
}
///
- /// Register an Azure CosmosDB NoSQL and with the specified service ID
- /// and where the Azure CosmosDB NoSQL is retrieved from the dependency injection container.
+ /// Registers an Azure CosmosDB NoSQL and ,
+ /// retrieving the from the dependency injection container.
///
/// The type of the record.
- /// The to register the on.
+ /// The to which the vector store should be added.
/// The name of the collection.
/// Optional options to further configure the .
- /// An optional service id to use as the service key.
+ /// The service lifetime for the client. Defaults to .
/// Service collection.
public static IServiceCollection AddAzureCosmosDBNoSQLVectorStoreRecordCollection(
- this IServiceCollection services,
+ this IServiceCollection serviceCollection,
string collectionName,
AzureCosmosDBNoSQLVectorStoreRecordCollectionOptions? options = default,
- string? serviceId = default)
- {
- services.AddKeyedTransient>(
- serviceId,
- (sp, obj) =>
- {
- var database = sp.GetRequiredService();
- var selectedOptions = options ?? sp.GetService>();
-
- return new AzureCosmosDBNoSQLVectorStoreRecordCollection(database, collectionName, selectedOptions);
- });
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ => AddKeyedAzureCosmosDBNoSQLVectorStoreRecordCollection(serviceCollection, serviceKey: null, collectionName, options, lifetime);
- AddVectorizedSearch(services, serviceId);
-
- return services;
+ ///
+ /// Registers a keyed Azure CosmosDB NoSQL and ,
+ /// retrieving the from the dependency injection container.
+ ///
+ /// The type of the record.
+ /// The to which the vector store should be added.
+ /// The key with which to associate the vector store.
+ /// The name of the collection.
+ /// Optional options to further configure the .
+ /// The service lifetime for the client. Defaults to .
+ /// Service collection.
+ public static IServiceCollection AddKeyedAzureCosmosDBNoSQLVectorStoreRecordCollection(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
+ string collectionName,
+ AzureCosmosDBNoSQLVectorStoreRecordCollectionOptions? options = default,
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ {
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStoreRecordCollection),
+ serviceKey,
+ (serviceProvider, _) => new AzureCosmosDBNoSQLVectorStoreRecordCollection(
+ serviceProvider.GetRequiredService(),
+ collectionName,
+ options ?? serviceProvider.GetService>()),
+ lifetime));
+
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorizedSearch),
+ serviceKey,
+ static (serviceProvider, serviceKey) => serviceProvider.GetRequiredKeyedService>(serviceKey),
+ lifetime));
+
+ return serviceCollection;
}
///
- /// Register an Azure CosmosDB NoSQL and with the specified service ID
- /// and where the Azure CosmosDB NoSQL is constructed using the provided and .
+ /// Register an Azure CosmosDB NoSQL and ,
+ /// using the provided and .
///
/// The type of the record.
- /// The to register the on.
+ /// The to which the vector store should be added.
/// The name of the collection.
/// Connection string required to connect to Azure CosmosDB NoSQL.
/// Database name for Azure CosmosDB NoSQL.
/// Optional options to further configure the .
- /// An optional service id to use as the service key.
+ /// The service lifetime for the client. Defaults to .
/// Service collection.
public static IServiceCollection AddAzureCosmosDBNoSQLVectorStoreRecordCollection(
- this IServiceCollection services,
+ this IServiceCollection serviceCollection,
string collectionName,
string connectionString,
string databaseName,
AzureCosmosDBNoSQLVectorStoreRecordCollectionOptions? options = default,
- string? serviceId = default)
- {
- services.AddKeyedSingleton>(
- serviceId,
- (sp, obj) =>
- {
- var cosmosClient = new CosmosClient(connectionString, new()
- {
- ApplicationName = HttpHeaderConstant.Values.UserAgent,
- UseSystemTextJsonSerializerWithOptions = options?.JsonSerializerOptions ?? JsonSerializerOptions.Default,
- });
-
- var database = cosmosClient.GetDatabase(databaseName);
- var selectedOptions = options ?? sp.GetService>();
-
- return new AzureCosmosDBNoSQLVectorStoreRecordCollection(database, collectionName, selectedOptions);
- });
-
- AddVectorizedSearch(services, serviceId);
-
- return services;
- }
+ ServiceLifetime lifetime = ServiceLifetime.Singleton)
+ => AddKeyedAzureCosmosDBNoSQLVectorStoreRecordCollection(serviceCollection, serviceKey: null, collectionName, connectionString, databaseName, options, lifetime);
///
- /// Also register the with the given as a .
+ /// Register a keyed Azure CosmosDB NoSQL and ,
+ /// using the provided and .
///
- /// The type of the data model that the collection should contain.
- /// The service collection to register on.
- /// The service id that the registrations should use.
- private static void AddVectorizedSearch(IServiceCollection services, string? serviceId)
+ /// The type of the record.
+ /// The to which the vector store should be added.
+ /// The key with which to associate the vector store.
+ /// The name of the collection.
+ /// Connection string required to connect to Azure CosmosDB NoSQL.
+ /// Database name for Azure CosmosDB NoSQL.
+ /// Optional options to further configure the .
+ /// The service lifetime for the client. Defaults to .
+ /// Service collection.
+ public static IServiceCollection AddKeyedAzureCosmosDBNoSQLVectorStoreRecordCollection(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
+ string collectionName,
+ string connectionString,
+ string databaseName,
+ AzureCosmosDBNoSQLVectorStoreRecordCollectionOptions? options = default,
+ ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
- services.AddKeyedTransient>(
- serviceId,
- (sp, obj) =>
- {
- return sp.GetRequiredKeyedService>(serviceId);
- });
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStoreRecordCollection),
+ serviceKey,
+ (serviceProvider, _) =>
+ {
+ var cosmosClient = new CosmosClient(connectionString, new()
+ {
+ ApplicationName = HttpHeaderConstant.Values.UserAgent,
+ UseSystemTextJsonSerializerWithOptions = options?.JsonSerializerOptions ?? JsonSerializerOptions.Default,
+ });
+
+ var database = cosmosClient.GetDatabase(databaseName);
+ options ??= serviceProvider.GetService>();
+
+ return new AzureCosmosDBNoSQLVectorStoreRecordCollection(database, collectionName, options);
+ },
+ lifetime));
+
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorizedSearch),
+ serviceKey,
+ static (serviceProvider, serviceKey) => serviceProvider.GetRequiredKeyedService>(serviceKey),
+ lifetime));
+
+ return serviceCollection;
}
}
diff --git a/dotnet/src/Connectors/Connectors.Memory.InMemory/InMemoryKernelBuilderExtensions.cs b/dotnet/src/Connectors/Connectors.Memory.InMemory/InMemoryKernelBuilderExtensions.cs
index 85311ceba4fb..2dd1bf01ca40 100644
--- a/dotnet/src/Connectors/Connectors.Memory.InMemory/InMemoryKernelBuilderExtensions.cs
+++ b/dotnet/src/Connectors/Connectors.Memory.InMemory/InMemoryKernelBuilderExtensions.cs
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
+using System;
using Microsoft.Extensions.VectorData;
using Microsoft.SemanticKernel.Connectors.InMemory;
@@ -8,6 +9,7 @@ namespace Microsoft.SemanticKernel;
///
/// Extension methods to register Data services on the .
///
+[Obsolete("Call the corresponding method on the Services property of your IKernelBuilder instance.")]
public static class InMemoryKernelBuilderExtensions
{
///
@@ -18,7 +20,7 @@ public static class InMemoryKernelBuilderExtensions
/// The kernel builder.
public static IKernelBuilder AddInMemoryVectorStore(this IKernelBuilder builder, string? serviceId = default)
{
- builder.Services.AddInMemoryVectorStore(serviceId);
+ builder.Services.AddKeyedInMemoryVectorStore(serviceId);
return builder;
}
@@ -39,7 +41,7 @@ public static IKernelBuilder AddInMemoryVectorStoreRecordCollection(collectionName, options, serviceId);
+ builder.Services.AddKeyedInMemoryVectorStoreRecordCollection(serviceId, collectionName, options);
return builder;
}
}
diff --git a/dotnet/src/Connectors/Connectors.Memory.InMemory/InMemoryServiceCollectionExtensions.cs b/dotnet/src/Connectors/Connectors.Memory.InMemory/InMemoryServiceCollectionExtensions.cs
index b541aad65b98..c98cb459275f 100644
--- a/dotnet/src/Connectors/Connectors.Memory.InMemory/InMemoryServiceCollectionExtensions.cs
+++ b/dotnet/src/Connectors/Connectors.Memory.InMemory/InMemoryServiceCollectionExtensions.cs
@@ -14,50 +14,92 @@ namespace Microsoft.SemanticKernel;
public static class InMemoryServiceCollectionExtensions
{
///
- /// Register an InMemory with the specified service ID.
+ /// Registers an InMemory .
///
- /// The to register the on.
- /// An optional service id to use as the service key.
+ /// The to which the vector store should be added.
+ /// The service lifetime for the client. Defaults to .
/// The service collection.
- public static IServiceCollection AddInMemoryVectorStore(this IServiceCollection services, string? serviceId = default)
+ public static IServiceCollection AddInMemoryVectorStore(
+ this IServiceCollection serviceCollection,
+ ServiceLifetime lifetime = ServiceLifetime.Singleton)
+ => AddKeyedInMemoryVectorStore(serviceCollection, serviceKey: null, lifetime);
+
+ ///
+ /// Registers an InMemory .
+ ///
+ /// The to which the vector store should be added.
+ /// The key with which to associate the vector store.
+ /// The service lifetime for the client. Defaults to .
+ /// The service collection.
+ public static IServiceCollection AddKeyedInMemoryVectorStore(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
+ ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
- services.AddKeyedSingleton(serviceId);
- services.AddKeyedSingleton(serviceId, (sp, obj) => sp.GetRequiredKeyedService(serviceId));
- return services;
+ serviceCollection.Add(new ServiceDescriptor(typeof(InMemoryVectorStore), serviceKey, typeof(InMemoryVectorStore), lifetime));
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStore),
+ serviceKey,
+ static (serviceProvider, serviceKey) => serviceProvider.GetRequiredKeyedService(serviceKey),
+ lifetime));
+
+ return serviceCollection;
}
///
- /// Register an InMemory and with the specified service ID.
+ /// Register an InMemory and .
///
/// The type of the key.
/// The type of the record.
- /// The to register the on.
+ /// The to which the vector store should be added.
/// The name of the collection.
- /// Optional options to further configure the .
- /// An optional service id to use as the service key.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
/// The service collection.
public static IServiceCollection AddInMemoryVectorStoreRecordCollection(
- this IServiceCollection services,
+ this IServiceCollection serviceCollection,
string collectionName,
InMemoryVectorStoreRecordCollectionOptions? options = default,
- string? serviceId = default)
+ ServiceLifetime lifetime = ServiceLifetime.Singleton)
+ where TKey : notnull
+ => AddKeyedInMemoryVectorStoreRecordCollection(serviceCollection, serviceKey: null, collectionName, options, lifetime);
+
+ ///
+ /// Register a keyed InMemory and .
+ ///
+ /// The type of the key.
+ /// The type of the record.
+ /// The to which the vector store should be added.
+ /// The key with which to associate the vector store.
+ /// The name of the collection.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
+ /// The service collection.
+ public static IServiceCollection AddKeyedInMemoryVectorStoreRecordCollection(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
+ string collectionName,
+ InMemoryVectorStoreRecordCollectionOptions? options = default,
+ ServiceLifetime lifetime = ServiceLifetime.Singleton)
where TKey : notnull
{
- services.AddKeyedSingleton>(
- serviceId,
- (sp, obj) =>
- {
- var selectedOptions = options ?? sp.GetService>();
- return (new InMemoryVectorStoreRecordCollection(collectionName, selectedOptions) as IVectorStoreRecordCollection)!;
- });
-
- services.AddKeyedSingleton>(
- serviceId,
- (sp, obj) =>
- {
- return sp.GetRequiredKeyedService>(serviceId);
- });
-
- return services;
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStoreRecordCollection),
+ serviceKey,
+ (serviceProvider, _) => new InMemoryVectorStoreRecordCollection(
+ collectionName,
+ options ?? serviceProvider.GetService>()),
+ lifetime));
+
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorizedSearch),
+ serviceKey,
+ static (serviceProvider, serviceKey) => serviceProvider.GetRequiredKeyedService>(serviceKey),
+ lifetime));
+
+ return serviceCollection;
}
}
diff --git a/dotnet/src/Connectors/Connectors.Memory.MongoDB/MongoDBServiceCollectionExtensions.cs b/dotnet/src/Connectors/Connectors.Memory.MongoDB/MongoDBServiceCollectionExtensions.cs
index b8e89aab82da..d86c92b10609 100644
--- a/dotnet/src/Connectors/Connectors.Memory.MongoDB/MongoDBServiceCollectionExtensions.cs
+++ b/dotnet/src/Connectors/Connectors.Memory.MongoDB/MongoDBServiceCollectionExtensions.cs
@@ -14,154 +14,229 @@ namespace Microsoft.SemanticKernel;
public static class MongoDBServiceCollectionExtensions
{
///
- /// Register a MongoDB with the specified service ID
- /// and where the MongoDB is retrieved from the dependency injection container.
+ /// Registers a MongoDB , retrieving the from the dependency injection container.
///
- /// The to register the on.
- /// Optional options to further configure the .
- /// An optional service id to use as the service key.
+ /// The to register the on.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
/// Service collection.
public static IServiceCollection AddMongoDBVectorStore(
- this IServiceCollection services,
+ this IServiceCollection serviceCollection,
MongoDBVectorStoreOptions? options = default,
- string? serviceId = default)
- {
// If we are not constructing MongoDatabase, add the IVectorStore as transient, since we
// cannot make assumptions about how MongoDatabase is being managed.
- services.AddKeyedTransient(
- serviceId,
- (sp, obj) =>
- {
- var database = sp.GetRequiredService();
- var selectedOptions = options ?? sp.GetService();
-
- return new MongoDBVectorStore(database, options);
- });
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ => AddKeyedMongoDBVectorStore(serviceCollection, serviceKey: null, options, lifetime);
- return services;
+ ///
+ /// Registers a keyed MongoDB , retrieving the from the dependency injection container.
+ ///
+ /// The to register the on.
+ /// The key with which to associate the vector store.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
+ /// Service collection.
+ public static IServiceCollection AddKeyedMongoDBVectorStore(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
+ MongoDBVectorStoreOptions? options = default,
+ // If we are not constructing MongoDatabase, add the IVectorStore as transient, since we
+ // cannot make assumptions about how MongoDatabase is being managed.
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ {
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStore),
+ serviceKey,
+ (serviceProvider, _) => new MongoDBVectorStore(
+ serviceProvider.GetRequiredService(),
+ options ?? serviceProvider.GetService()),
+ lifetime));
+
+ return serviceCollection;
}
///
- /// Register a MongoDB with the specified service ID
- /// and where the MongoDB is constructed using the provided and .
+ /// Registers a MongoDB , using the provided and .
///
- /// The to register the on.
+ /// The to register the on.
/// Connection string required to connect to MongoDB.
/// Database name for MongoDB.
- /// Optional options to further configure the .
- /// An optional service id to use as the service key.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
/// Service collection.
public static IServiceCollection AddMongoDBVectorStore(
- this IServiceCollection services,
+ this IServiceCollection serviceCollection,
string connectionString,
string databaseName,
MongoDBVectorStoreOptions? options = default,
- string? serviceId = default)
- {
// If we are constructing IMongoDatabase, add the IVectorStore as singleton, since we are managing the lifetime of it,
// and the recommendation from Mongo is to register it with a singleton lifetime.
- services.AddKeyedSingleton(
- serviceId,
- (sp, obj) =>
- {
- var settings = MongoClientSettings.FromConnectionString(connectionString);
- settings.ApplicationName = HttpHeaderConstant.Values.UserAgent;
-
- var mongoClient = new MongoClient(settings);
- var database = mongoClient.GetDatabase(databaseName);
-
- var selectedOptions = options ?? sp.GetService();
-
- return new MongoDBVectorStore(database, options);
- });
+ ServiceLifetime lifetime = ServiceLifetime.Singleton)
+ => AddKeyedMongoDBVectorStore(serviceCollection, serviceKey: null, connectionString, databaseName, options, lifetime);
- return services;
+ ///
+ /// Registers a keyed MongoDB , using the provided and .
+ ///
+ /// The to register the on.
+ /// The key with which to associate the vector store.
+ /// Connection string required to connect to MongoDB.
+ /// Database name for MongoDB.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
+ /// Service collection.
+ public static IServiceCollection AddKeyedMongoDBVectorStore(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
+ string connectionString,
+ string databaseName,
+ MongoDBVectorStoreOptions? options = default,
+ // If we are constructing IMongoDatabase, add the IVectorStore as singleton, since we are managing the lifetime of it,
+ // and the recommendation from Mongo is to register it with a singleton lifetime.
+ ServiceLifetime lifetime = ServiceLifetime.Singleton)
+ {
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStore),
+ serviceKey,
+ (serviceProvider, _) =>
+ {
+ var settings = MongoClientSettings.FromConnectionString(connectionString);
+ settings.ApplicationName = HttpHeaderConstant.Values.UserAgent;
+
+ var mongoClient = new MongoClient(settings);
+ var database = mongoClient.GetDatabase(databaseName);
+
+ return new MongoDBVectorStore(database, options ?? serviceProvider.GetService());
+ },
+ lifetime));
+
+ return serviceCollection;
}
///
- /// Register a MongoDB and with the specified service ID
- /// and where the MongoDB is retrieved from the dependency injection container.
+ /// Registers a MongoDB and ,
+ /// retrieving the from the dependency injection container.
///
/// The type of the record.
- /// The to register the on.
+ /// The to which the vector store should be added.
/// The name of the collection.
- /// Optional options to further configure the .
- /// An optional service id to use as the service key.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
/// Service collection.
public static IServiceCollection AddMongoDBVectorStoreRecordCollection(
- this IServiceCollection services,
+ this IServiceCollection serviceCollection,
string collectionName,
MongoDBVectorStoreRecordCollectionOptions? options = default,
- string? serviceId = default)
- {
- services.AddKeyedTransient>(
- serviceId,
- (sp, obj) =>
- {
- var database = sp.GetRequiredService();
- var selectedOptions = options ?? sp.GetService>();
-
- return new MongoDBVectorStoreRecordCollection(database, collectionName, selectedOptions);
- });
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ => AddKeyedMongoDBVectorStoreRecordCollection(serviceCollection, serviceKey: null, collectionName, options, lifetime);
- AddVectorizedSearch(services, serviceId);
-
- return services;
+ ///
+ /// Registers a keyed MongoDB and ,
+ /// retrieving the from the dependency injection container.
+ ///
+ /// The type of the record.
+ /// The to which the vector store should be added.
+ /// The key with which to associate the vector store.
+ /// The name of the collection.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
+ /// Service collection.
+ public static IServiceCollection AddKeyedMongoDBVectorStoreRecordCollection(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
+ string collectionName,
+ MongoDBVectorStoreRecordCollectionOptions? options = default,
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ {
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStoreRecordCollection),
+ serviceKey,
+ (serviceProvider, _) => new MongoDBVectorStoreRecordCollection(
+ serviceProvider.GetRequiredService(),
+ collectionName,
+ options ?? serviceProvider.GetService>()),
+ lifetime));
+
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorizedSearch),
+ serviceKey,
+ static (serviceProvider, serviceKey) => serviceProvider.GetRequiredKeyedService>(serviceKey),
+ lifetime));
+
+ return serviceCollection;
}
///
- /// Register a MongoDB and with the specified service ID
- /// and where the MongoDB is constructed using the provided and .
+ /// Registers a MongoDB and ,
+ /// using the provided and .
///
/// The type of the record.
- /// The to register the on.
+ /// The to which the vector store should be added.
/// The name of the collection.
/// Connection string required to connect to MongoDB.
/// Database name for MongoDB.
- /// Optional options to further configure the .
- /// An optional service id to use as the service key.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
/// Service collection.
public static IServiceCollection AddMongoDBVectorStoreRecordCollection(
- this IServiceCollection services,
+ this IServiceCollection serviceCollection,
string collectionName,
string connectionString,
string databaseName,
MongoDBVectorStoreRecordCollectionOptions? options = default,
- string? serviceId = default)
- {
- services.AddKeyedSingleton>(
- serviceId,
- (sp, obj) =>
- {
- var settings = MongoClientSettings.FromConnectionString(connectionString);
- settings.ApplicationName = HttpHeaderConstant.Values.UserAgent;
-
- var mongoClient = new MongoClient(settings);
- var database = mongoClient.GetDatabase(databaseName);
-
- var selectedOptions = options ?? sp.GetService>();
-
- return new MongoDBVectorStoreRecordCollection(database, collectionName, selectedOptions);
- });
-
- AddVectorizedSearch(services, serviceId);
-
- return services;
- }
+ ServiceLifetime lifetime = ServiceLifetime.Singleton)
+ => AddKeyedMongoDBVectorStoreRecordCollection(serviceCollection, serviceKey: null, collectionName, connectionString, databaseName, options, lifetime);
///
- /// Also register the with the given as a .
+ /// Registers a keyed MongoDB and ,
+ /// using the provided and .
///
- /// The type of the data model that the collection should contain.
- /// The service collection to register on.
- /// The service id that the registrations should use.
- private static void AddVectorizedSearch(IServiceCollection services, string? serviceId)
+ /// The type of the record.
+ /// The to which the vector store should be added.
+ /// The key with which to associate the vector store.
+ /// The name of the collection.
+ /// Connection string required to connect to MongoDB.
+ /// Database name for MongoDB.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
+ /// Service collection.
+ public static IServiceCollection AddKeyedMongoDBVectorStoreRecordCollection(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
+ string collectionName,
+ string connectionString,
+ string databaseName,
+ MongoDBVectorStoreRecordCollectionOptions? options = default,
+ ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
- services.AddKeyedTransient>(
- serviceId,
- (sp, obj) =>
- {
- return sp.GetRequiredKeyedService>(serviceId);
- });
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStoreRecordCollection),
+ serviceKey,
+ (serviceProvider, _) =>
+ {
+ var settings = MongoClientSettings.FromConnectionString(connectionString);
+ settings.ApplicationName = HttpHeaderConstant.Values.UserAgent;
+
+ var mongoClient = new MongoClient(settings);
+ var database = mongoClient.GetDatabase(databaseName);
+
+ var selectedOptions = options ?? serviceProvider.GetService>();
+
+ return new MongoDBVectorStoreRecordCollection(database, collectionName, selectedOptions);
+ },
+ lifetime));
+
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorizedSearch),
+ serviceKey,
+ static (serviceProvider, serviceKey) => serviceProvider.GetRequiredKeyedService>(serviceKey),
+ lifetime));
+
+ return serviceCollection;
}
}
diff --git a/dotnet/src/Connectors/Connectors.Memory.Pinecone/PineconeKernelBuilderExtensions.cs b/dotnet/src/Connectors/Connectors.Memory.Pinecone/PineconeKernelBuilderExtensions.cs
index 50048c8dfa6f..9410c1ef9c15 100644
--- a/dotnet/src/Connectors/Connectors.Memory.Pinecone/PineconeKernelBuilderExtensions.cs
+++ b/dotnet/src/Connectors/Connectors.Memory.Pinecone/PineconeKernelBuilderExtensions.cs
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
+using System;
using Microsoft.Extensions.VectorData;
using Microsoft.SemanticKernel.Connectors.Pinecone;
using Sdk = Pinecone;
@@ -9,6 +10,7 @@ namespace Microsoft.SemanticKernel;
///
/// Extension methods to register Pinecone instances on the .
///
+[Obsolete("Call the corresponding method on the Services property of your IKernelBuilder instance.")]
public static class PineconeKernelBuilderExtensions
{
///
@@ -20,7 +22,7 @@ public static class PineconeKernelBuilderExtensions
/// The kernel builder.
public static IKernelBuilder AddPineconeVectorStore(this IKernelBuilder builder, PineconeVectorStoreOptions? options = default, string? serviceId = default)
{
- builder.Services.AddPineconeVectorStore(options, serviceId);
+ builder.Services.AddKeyedPineconeVectorStore(serviceId, options);
return builder;
}
@@ -34,7 +36,7 @@ public static IKernelBuilder AddPineconeVectorStore(this IKernelBuilder builder,
/// The kernel builder.
public static IKernelBuilder AddPineconeVectorStore(this IKernelBuilder builder, string apiKey, PineconeVectorStoreOptions? options = default, string? serviceId = default)
{
- builder.Services.AddPineconeVectorStore(apiKey, options, serviceId);
+ builder.Services.AddKeyedPineconeVectorStore(serviceId, apiKey, options);
return builder;
}
@@ -54,7 +56,7 @@ public static IKernelBuilder AddPineconeVectorStoreRecordCollection(
PineconeVectorStoreRecordCollectionOptions? options = default,
string? serviceId = default)
{
- builder.Services.AddPineconeVectorStoreRecordCollection(collectionName, options, serviceId);
+ builder.Services.AddKeyedPineconeVectorStoreRecordCollection(serviceId, collectionName, options);
return builder;
}
@@ -76,7 +78,7 @@ public static IKernelBuilder AddPineconeVectorStoreRecordCollection(
PineconeVectorStoreRecordCollectionOptions? options = default,
string? serviceId = default)
{
- builder.Services.AddPineconeVectorStoreRecordCollection(collectionName, apiKey, options, serviceId);
+ builder.Services.AddKeyedPineconeVectorStoreRecordCollection(serviceId, collectionName, apiKey, options);
return builder;
}
}
diff --git a/dotnet/src/Connectors/Connectors.Memory.Pinecone/PineconeServiceCollectionExtensions.cs b/dotnet/src/Connectors/Connectors.Memory.Pinecone/PineconeServiceCollectionExtensions.cs
index 5e7658eb923f..123522892291 100644
--- a/dotnet/src/Connectors/Connectors.Memory.Pinecone/PineconeServiceCollectionExtensions.cs
+++ b/dotnet/src/Connectors/Connectors.Memory.Pinecone/PineconeServiceCollectionExtensions.cs
@@ -13,141 +13,206 @@ namespace Microsoft.SemanticKernel;
public static class PineconeServiceCollectionExtensions
{
///
- /// Register a Pinecone with the specified service ID and where is retrieved from the dependency injection container.
+ /// Registers a Pinecone , retrieving from the dependency injection container.
///
- /// The to register the on.
- /// Optional options to further configure the .
- /// An optional service id to use as the service key.
+ /// The to which the vector store should be added.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
/// The service collection.
- public static IServiceCollection AddPineconeVectorStore(this IServiceCollection services, PineconeVectorStoreOptions? options = default, string? serviceId = default)
- {
+ public static IServiceCollection AddPineconeVectorStore(
+ this IServiceCollection serviceCollection,
+ PineconeVectorStoreOptions? options = default,
// If we are not constructing the PineconeClient, add the IVectorStore as transient, since we
// cannot make assumptions about how PineconeClient is being managed.
- services.AddKeyedTransient(
- serviceId,
- (sp, obj) =>
- {
- var pineconeClient = sp.GetRequiredService();
- var selectedOptions = options ?? sp.GetService();
-
- return new PineconeVectorStore(
- pineconeClient,
- selectedOptions);
- });
-
- return services;
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ => AddKeyedPineconeVectorStore(serviceCollection, serviceKey: null, options, lifetime);
+
+ ///
+ /// Registers a keyed Pinecone , retrieving from the dependency injection container.
+ ///
+ /// The to which the vector store should be added.
+ /// The key with which to associate the vector store.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
+ /// The service collection.
+ public static IServiceCollection AddKeyedPineconeVectorStore(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
+ PineconeVectorStoreOptions? options = default,
+ // If we are not constructing the PineconeClient, add the IVectorStore as transient, since we
+ // cannot make assumptions about how PineconeClient is being managed.
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ {
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStore),
+ serviceKey,
+ (serviceProvider, _) => new PineconeVectorStore(
+ serviceProvider.GetRequiredService(),
+ options ?? serviceProvider.GetService()),
+ lifetime));
+
+ return serviceCollection;
}
///
- /// Register a Pinecone with the specified service ID and where is constructed using the provided apikey.
+ /// Registers a Pinecone using the provided apikey.
///
- /// The to register the on.
+ /// The to which the vector store should be added.
/// The api key for Pinecone.
- /// Optional options to further configure the .
- /// An optional service id to use as the service key.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
/// The service collection.
- public static IServiceCollection AddPineconeVectorStore(this IServiceCollection services, string apiKey, PineconeVectorStoreOptions? options = default, string? serviceId = default)
+ public static IServiceCollection AddPineconeVectorStore(
+ this IServiceCollection serviceCollection,
+ string apiKey,
+ PineconeVectorStoreOptions? options = default,
+ ServiceLifetime lifetime = ServiceLifetime.Singleton)
+ => AddKeyedPineconeVectorStore(serviceCollection, serviceKey: null, apiKey, options, lifetime);
+
+ ///
+ /// Registers a keyed Pinecone using the provided apikey.
+ ///
+ /// The to which the vector store should be added.
+ /// The key with which to associate the vector store.
+ /// The api key for Pinecone.
+ /// Options to further configure the .
+ /// The service lifetime for the client. Defaults to .
+ /// The service collection.
+ public static IServiceCollection AddKeyedPineconeVectorStore(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
+ string apiKey,
+ PineconeVectorStoreOptions? options = default,
+ ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
- services.AddKeyedSingleton(
- serviceId,
- (sp, obj) =>
- {
- var pineconeClient = new Sdk.PineconeClient(apiKey);
- var selectedOptions = options ?? sp.GetService();
-
- return new PineconeVectorStore(
- pineconeClient,
- selectedOptions);
- });
-
- return services;
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStore),
+ serviceKey,
+ (serviceProvider, _) => new PineconeVectorStore(
+ new Sdk.PineconeClient(apiKey),
+ options ?? serviceProvider.GetService()),
+ lifetime));
+
+ return serviceCollection;
}
///
- /// Register a Pinecone and with the
- /// specified service ID and where is retrieved from the dependency injection container.
+ /// Registers a Pinecone and ,
+ /// retrieving from the dependency injection container.
///
/// The type of the data model that the collection should contain.
- /// The to register the on.
+ /// The to which the vector store should be added.
/// The name of the collection that this will access.
- /// Optional configuration options to pass to the .
- /// An optional service id to use as the service key.
+ /// Configuration options to pass to the .
+ /// The service lifetime for the client. Defaults to .
/// The service collection.
public static IServiceCollection AddPineconeVectorStoreRecordCollection(
- this IServiceCollection services,
+ this IServiceCollection serviceCollection,
string collectionName,
PineconeVectorStoreRecordCollectionOptions? options = default,
- string? serviceId = default)
- {
// If we are not constructing the PineconeClient, add the IVectorStore as transient, since we
// cannot make assumptions about how PineconeClient is being managed.
- services.AddKeyedTransient>(
- serviceId,
- (sp, obj) =>
- {
- var pineconeClient = sp.GetRequiredService();
- var selectedOptions = options ?? sp.GetService>();
-
- return new PineconeVectorStoreRecordCollection(
- pineconeClient,
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ => AddKeyedPineconeVectorStoreRecordCollection(serviceCollection, serviceKey: null, collectionName, options, lifetime);
+
+ ///
+ /// Registers a keyed Pinecone and ,
+ /// retrieving from the dependency injection container.
+ ///
+ /// The type of the data model that the collection should contain.
+ /// The to which the vector store should be added.
+ /// The key with which to associate the vector store.
+ /// The name of the collection that this will access.
+ /// Configuration options to pass to the .
+ /// The service lifetime for the client. Defaults to .
+ /// The service collection.
+ public static IServiceCollection AddKeyedPineconeVectorStoreRecordCollection(
+ this IServiceCollection serviceCollection,
+ object? serviceKey,
+ string collectionName,
+ PineconeVectorStoreRecordCollectionOptions? options = default,
+ // If we are not constructing the PineconeClient, add the IVectorStore as transient, since we
+ // cannot make assumptions about how PineconeClient is being managed.
+ ServiceLifetime lifetime = ServiceLifetime.Transient)
+ {
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorStoreRecordCollection),
+ serviceKey,
+ (serviceProvider, _) => new PineconeVectorStoreRecordCollection(
+ serviceProvider.GetRequiredService(),
collectionName,
- selectedOptions);
- });
+ options ?? serviceProvider.GetService>()),
+ lifetime));
- AddVectorizedSearch(services, serviceId);
+ serviceCollection.Add(
+ new ServiceDescriptor(
+ typeof(IVectorizedSearch),
+ serviceKey,
+ static (serviceProvider, serviceKey) => serviceProvider.GetRequiredKeyedService>(serviceKey),
+ lifetime));
- return services;
+ return serviceCollection;
}
///
- /// Register a Pinecone and with the
- /// provided and the specified service ID.
+ /// Registers a Pinecone and ,
+ /// using the provided apikey.
///
/// The type of the data model that the collection should contain.
- /// The to register the on.
+ /// The to which the vector store should be added.
/// The name of the collection that this will access.
/// The api key for Pinecone.
- /// Optional configuration options to pass to the .
- /// An optional service id to use as the service key.
+ /// Configuration options to pass to the .
+ /// The service lifetime for the client. Defaults to .
/// The service collection.
public static IServiceCollection AddPineconeVectorStoreRecordCollection(
- this IServiceCollection services,
+ this IServiceCollection serviceCollection,
string collectionName,
string apiKey,
PineconeVectorStoreRecordCollectionOptions? options = default,
- string? serviceId = default)
- {
- services.AddKeyedSingleton