Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net: Remove class constraint for VectorStore records. #9251

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions dotnet/samples/Concepts/Memory/VectorStoreExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ internal static class VectorStoreExtensions
/// </summary>
/// <typeparam name="TKey">Type of the record key.</typeparam>
/// <typeparam name="TRecord">Type of the record.</typeparam>
internal delegate TRecord CreateRecordFromString<TKey, TRecord>(string text, ReadOnlyMemory<float> vector) where TKey : notnull where TRecord : class;
internal delegate TRecord CreateRecordFromString<TKey, TRecord>(string text, ReadOnlyMemory<float> vector) where TKey : notnull;

/// <summary>
/// Delegate to create a record from a <see cref="TextSearchResult"/>.
/// </summary>
/// <typeparam name="TKey">Type of the record key.</typeparam>
/// <typeparam name="TRecord">Type of the record.</typeparam>
internal delegate TRecord CreateRecordFromTextSearchResult<TKey, TRecord>(TextSearchResult searchResult, ReadOnlyMemory<float> vector) where TKey : notnull where TRecord : class;
internal delegate TRecord CreateRecordFromTextSearchResult<TKey, TRecord>(TextSearchResult searchResult, ReadOnlyMemory<float> vector) where TKey : notnull;

/// <summary>
/// Create a <see cref="IVectorStoreRecordCollection{TKey, TRecord}"/> from a list of strings by:
Expand All @@ -45,7 +45,6 @@ internal static async Task<IVectorStoreRecordCollection<TKey, TRecord>> CreateCo
ITextEmbeddingGenerationService embeddingGenerationService,
CreateRecordFromString<TKey, TRecord> createRecord)
where TKey : notnull
where TRecord : class
{
// Get and create collection if it doesn't exist.
var collection = vectorStore.GetCollection<TKey, TRecord>(collectionName);
Expand Down Expand Up @@ -81,7 +80,6 @@ internal static async Task<IVectorStoreRecordCollection<TKey, TRecord>> CreateCo
ITextEmbeddingGenerationService embeddingGenerationService,
CreateRecordFromTextSearchResult<TKey, TRecord> createRecord)
where TKey : notnull
where TRecord : class
{
// Get and create collection if it doesn't exist.
var collection = vectorStore.GetCollection<TKey, TRecord>(collectionName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System.Text.Json;
using System.Text.Json.Nodes;
using Azure.Identity;
using Memory.VectorStoreFixtures;
using Microsoft.Extensions.VectorData;
using Microsoft.SemanticKernel.Connectors.AzureOpenAI;
Expand Down Expand Up @@ -58,7 +59,7 @@ public async Task ExampleAsync()
var textEmbeddingGenerationService = new AzureOpenAITextEmbeddingGenerationService(
TestConfiguration.AzureOpenAIEmbeddings.DeploymentName,
TestConfiguration.AzureOpenAIEmbeddings.Endpoint,
TestConfiguration.AzureOpenAIEmbeddings.ApiKey);
new AzureCliCredential());

// Initiate the docker container and construct the vector store using the custom factory for creating collections.
await redisFixture.ManualInitializeAsync();
Expand Down Expand Up @@ -135,7 +136,6 @@ private sealed class Factory : IRedisVectorStoreRecordCollectionFactory
{
public IVectorStoreRecordCollection<TKey, TRecord> CreateVectorStoreRecordCollection<TKey, TRecord>(IDatabase database, string name, VectorStoreRecordDefinition? vectorStoreRecordDefinition)
where TKey : notnull
where TRecord : class
{
// If the record definition is the glossary definition and the record type is the generic data model, inject the custom mapper into the collection options.
if (vectorStoreRecordDefinition == s_glossaryDefinition && typeof(TRecord) == typeof(GenericDataModel))
Expand Down
4 changes: 1 addition & 3 deletions dotnet/samples/Concepts/Search/VectorStore_TextSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private async Task ExecuteSearchesAsync(VectorStoreTextSearch<DataModel> textSea
/// </summary>
/// <typeparam name="TKey">Type of the record key.</typeparam>
/// <typeparam name="TRecord">Type of the record.</typeparam>
internal delegate TRecord CreateRecord<TKey, TRecord>(string text, ReadOnlyMemory<float> vector) where TKey : notnull where TRecord : class;
internal delegate TRecord CreateRecord<TKey, TRecord>(string text, ReadOnlyMemory<float> vector) where TKey : notnull;

/// <summary>
/// Create a <see cref="IVectorStoreRecordCollection{TKey, TRecord}"/> from a list of strings by:
Expand All @@ -122,7 +122,6 @@ internal static async Task<IVectorStoreRecordCollection<TKey, TRecord>> CreateCo
ITextEmbeddingGenerationService embeddingGenerationService,
CreateRecord<TKey, TRecord> createRecord)
where TKey : notnull
where TRecord : class
{
// Get and create collection if it doesn't exist.
var collection = vectorStore.GetCollection<TKey, TRecord>(collectionName);
Expand All @@ -143,7 +142,6 @@ internal static async Task<IVectorStoreRecordCollection<TKey, TRecord>> CreateCo
/// Decorator for a <see cref="IVectorizedSearch{TRecord}"/> that generates embeddings for text search queries.
/// </summary>
private sealed class VectorizedSearchWrapper<TRecord>(IVectorizedSearch<TRecord> vectorizedSearch, ITextEmbeddingGenerationService textEmbeddingGeneration) : IVectorizableTextSearch<TRecord>
where TRecord : class
{
/// <inheritdoc/>
public async Task<VectorSearchResults<TRecord>> VectorizableTextSearchAsync(string searchText, VectorSearchOptions? options = null, CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static DataModel CreateRecord(int index, string text, ReadOnlyMemory<float> embe
/// </summary>
/// <typeparam name="TKey">Type of the record key.</typeparam>
/// <typeparam name="TRecord">Type of the record.</typeparam>
internal delegate TRecord CreateRecord<TKey, TRecord>(int index, string text, ReadOnlyMemory<float> vector) where TKey : notnull where TRecord : class;
internal delegate TRecord CreateRecord<TKey, TRecord>(int index, string text, ReadOnlyMemory<float> vector) where TKey : notnull;

/// <summary>
/// Create a <see cref="IVectorStoreRecordCollection{TKey, TRecord}"/> from a list of strings by:
Expand All @@ -113,7 +113,6 @@ private async Task<IVectorStoreRecordCollection<TKey, TRecord>> CreateCollection
string[] entries,
CreateRecord<TKey, TRecord> createRecord)
where TKey : notnull
where TRecord : class
{
// Get and create collection if it doesn't exist.
var collection = this.InMemoryVectorStore.GetCollection<TKey, TRecord>(this.CollectionName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public static IKernelBuilder AddAzureAISearchVectorStoreRecordCollection<TRecord
string collectionName,
AzureAISearchVectorStoreRecordCollectionOptions<TRecord>? options = default,
string? serviceId = default)
where TRecord : class
{
builder.Services.AddAzureAISearchVectorStoreRecordCollection<TRecord>(collectionName, options, serviceId);
return builder;
Expand All @@ -97,7 +96,6 @@ public static IKernelBuilder AddAzureAISearchVectorStoreRecordCollection<TRecord
TokenCredential tokenCredential,
AzureAISearchVectorStoreRecordCollectionOptions<TRecord>? options = default,
string? serviceId = default)
where TRecord : class
{
builder.Services.AddAzureAISearchVectorStoreRecordCollection<TRecord>(collectionName, endpoint, tokenCredential, options, serviceId);
return builder;
Expand All @@ -122,7 +120,6 @@ public static IKernelBuilder AddAzureAISearchVectorStoreRecordCollection<TRecord
AzureKeyCredential credential,
AzureAISearchVectorStoreRecordCollectionOptions<TRecord>? options = default,
string? serviceId = default)
where TRecord : class
{
builder.Services.AddAzureAISearchVectorStoreRecordCollection<TRecord>(collectionName, endpoint, credential, options, serviceId);
return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ public static IServiceCollection AddAzureAISearchVectorStoreRecordCollection<TRe
string collectionName,
AzureAISearchVectorStoreRecordCollectionOptions<TRecord>? options = default,
string? serviceId = default)
where TRecord : class
{
// If we are not constructing the SearchIndexClient, add the IVectorStore as transient, since we
// cannot make assumptions about how SearchIndexClient is being managed.
Expand Down Expand Up @@ -163,7 +162,6 @@ public static IServiceCollection AddAzureAISearchVectorStoreRecordCollection<TRe
TokenCredential tokenCredential,
AzureAISearchVectorStoreRecordCollectionOptions<TRecord>? options = default,
string? serviceId = default)
where TRecord : class
{
Verify.NotNull(endpoint);
Verify.NotNull(tokenCredential);
Expand Down Expand Up @@ -207,7 +205,6 @@ public static IServiceCollection AddAzureAISearchVectorStoreRecordCollection<TRe
AzureKeyCredential credential,
AzureAISearchVectorStoreRecordCollectionOptions<TRecord>? options = default,
string? serviceId = default)
where TRecord : class
{
Verify.NotNull(endpoint);
Verify.NotNull(credential);
Expand Down Expand Up @@ -239,7 +236,6 @@ public static IServiceCollection AddAzureAISearchVectorStoreRecordCollection<TRe
/// <param name="services">The service collection to register on.</param>
/// <param name="serviceId">The service id that the registrations should use.</param>
private static void AddVectorizedSearch<TRecord>(IServiceCollection services, string? serviceId)
where TRecord : class
{
services.AddKeyedTransient<IVectorizedSearch<TRecord>>(
serviceId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public AzureAISearchVectorStore(SearchIndexClient searchIndexClient, AzureAISear
/// <inheritdoc />
public IVectorStoreRecordCollection<TKey, TRecord> GetCollection<TKey, TRecord>(string name, VectorStoreRecordDefinition? vectorStoreRecordDefinition = null)
where TKey : notnull
where TRecord : class
{
if (typeof(TKey) != typeof(string))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ namespace Microsoft.SemanticKernel.Connectors.AzureAISearch;
#pragma warning disable CA1711 // Identifiers should not have incorrect suffix
public sealed class AzureAISearchVectorStoreRecordCollection<TRecord> : IVectorStoreRecordCollection<string, TRecord>, IVectorizableTextSearch<TRecord>
#pragma warning restore CA1711 // Identifiers should not have incorrect suffix
where TRecord : class
{
/// <summary>The name of this database for telemetry purposes.</summary>
private const string DatabaseName = "AzureAISearch";
Expand Down Expand Up @@ -424,7 +423,7 @@ public Task<VectorSearchResults<TRecord>> VectorizableTextSearchAsync(string sea

if (jsonObject is null)
{
return null;
return default;
}

return VectorStoreErrorHandler.RunModelConversion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace Microsoft.SemanticKernel.Connectors.AzureAISearch;
/// Options when creating a <see cref="AzureAISearchVectorStoreRecordCollection{TRecord}"/>.
/// </summary>
public sealed class AzureAISearchVectorStoreRecordCollectionOptions<TRecord>
where TRecord : class
{
/// <summary>
/// Gets or sets an optional custom mapper to use when converting between the data model and the Azure AI Search record.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ public interface IAzureAISearchVectorStoreRecordCollectionFactory
/// <param name="vectorStoreRecordDefinition">An optional record definition that defines the schema of the record type. If not present, attributes on <typeparamref name="TRecord"/> will be used.</param>
/// <returns>The new instance of <see cref="IVectorStoreRecordCollection{TKey, TRecord}"/>.</returns>
IVectorStoreRecordCollection<TKey, TRecord> CreateVectorStoreRecordCollection<TKey, TRecord>(SearchIndexClient searchIndexClient, string name, VectorStoreRecordDefinition? vectorStoreRecordDefinition)
where TKey : notnull
where TRecord : class;
where TKey : notnull;
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public static IKernelBuilder AddAzureCosmosDBMongoDBVectorStoreRecordCollection<
string collectionName,
AzureCosmosDBMongoDBVectorStoreRecordCollectionOptions<TRecord>? options = default,
string? serviceId = default)
where TRecord : class
{
builder.Services.AddAzureCosmosDBMongoDBVectorStoreRecordCollection<TRecord>(collectionName, options, serviceId);
return builder;
Expand All @@ -89,7 +88,6 @@ public static IKernelBuilder AddAzureCosmosDBMongoDBVectorStoreRecordCollection<
string databaseName,
AzureCosmosDBMongoDBVectorStoreRecordCollectionOptions<TRecord>? options = default,
string? serviceId = default)
where TRecord : class
{
builder.Services.AddAzureCosmosDBMongoDBVectorStoreRecordCollection<TRecord>(collectionName, connectionString, databaseName, options, serviceId);
return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ public static IServiceCollection AddAzureCosmosDBMongoDBVectorStoreRecordCollect
string collectionName,
AzureCosmosDBMongoDBVectorStoreRecordCollectionOptions<TRecord>? options = default,
string? serviceId = default)
where TRecord : class
{
services.AddKeyedTransient<IVectorStoreRecordCollection<string, TRecord>>(
serviceId,
Expand Down Expand Up @@ -129,7 +128,6 @@ public static IServiceCollection AddAzureCosmosDBMongoDBVectorStoreRecordCollect
string databaseName,
AzureCosmosDBMongoDBVectorStoreRecordCollectionOptions<TRecord>? options = default,
string? serviceId = default)
where TRecord : class
{
services.AddKeyedSingleton<IVectorStoreRecordCollection<string, TRecord>>(
serviceId,
Expand Down Expand Up @@ -158,7 +156,6 @@ public static IServiceCollection AddAzureCosmosDBMongoDBVectorStoreRecordCollect
/// <param name="services">The service collection to register on.</param>
/// <param name="serviceId">The service id that the registrations should use.</param>
private static void AddVectorizedSearch<TRecord>(IServiceCollection services, string? serviceId)
where TRecord : class
{
services.AddKeyedTransient<IVectorizedSearch<TRecord>>(
serviceId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public AzureCosmosDBMongoDBVectorStore(IMongoDatabase mongoDatabase, AzureCosmos
/// <inheritdoc />
public IVectorStoreRecordCollection<TKey, TRecord> GetCollection<TKey, TRecord>(string name, VectorStoreRecordDefinition? vectorStoreRecordDefinition = null)
where TKey : notnull
where TRecord : class
{
if (typeof(TKey) != typeof(string))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Microsoft.SemanticKernel.Connectors.AzureCosmosDBMongoDB;
/// </summary>
/// <typeparam name="TRecord">The data model to use for adding, updating and retrieving data from storage.</typeparam>
#pragma warning disable CA1711 // Identifiers should not have incorrect suffix
public sealed class AzureCosmosDBMongoDBVectorStoreRecordCollection<TRecord> : IVectorStoreRecordCollection<string, TRecord> where TRecord : class
public sealed class AzureCosmosDBMongoDBVectorStoreRecordCollection<TRecord> : IVectorStoreRecordCollection<string, TRecord>
#pragma warning restore CA1711 // Identifiers should not have incorrect suffix
{
/// <summary>The name of this database for telemetry purposes.</summary>
Expand Down Expand Up @@ -157,7 +157,7 @@ public Task DeleteCollectionAsync(CancellationToken cancellationToken = default)

if (record is null)
{
return null;
return default;
}

return VectorStoreErrorHandler.RunModelConversion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Microsoft.SemanticKernel.Connectors.AzureCosmosDBMongoDB;
/// <summary>
/// Options when creating a <see cref="AzureCosmosDBMongoDBVectorStoreRecordCollection{TRecord}"/>.
/// </summary>
public sealed class AzureCosmosDBMongoDBVectorStoreRecordCollectionOptions<TRecord> where TRecord : class
public sealed class AzureCosmosDBMongoDBVectorStoreRecordCollectionOptions<TRecord>
{
/// <summary>
/// Gets or sets an optional custom mapper to use when converting between the data model and the Azure CosmosDB MongoDB BSON object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
namespace Microsoft.SemanticKernel.Connectors.AzureCosmosDBMongoDB;

internal sealed class AzureCosmosDBMongoDBVectorStoreRecordMapper<TRecord> : IVectorStoreRecordMapper<TRecord, BsonDocument>
where TRecord : class
{
/// <summary>A key property info of the data model.</summary>
private readonly PropertyInfo _keyProperty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ public interface IAzureCosmosDBMongoDBVectorStoreRecordCollectionFactory
/// <param name="vectorStoreRecordDefinition">An optional record definition that defines the schema of the record type. If not present, attributes on <typeparamref name="TRecord"/> will be used.</param>
/// <returns>The new instance of <see cref="IVectorStoreRecordCollection{TKey, TRecord}"/>.</returns>
IVectorStoreRecordCollection<TKey, TRecord> CreateVectorStoreRecordCollection<TKey, TRecord>(IMongoDatabase mongoDatabase, string name, VectorStoreRecordDefinition? vectorStoreRecordDefinition)
where TKey : notnull
where TRecord : class;
where TKey : notnull;
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public static IKernelBuilder AddAzureCosmosDBNoSQLVectorStoreRecordCollection<TR
string collectionName,
AzureCosmosDBNoSQLVectorStoreRecordCollectionOptions<TRecord>? options = default,
string? serviceId = default)
where TRecord : class
{
builder.Services.AddAzureCosmosDBNoSQLVectorStoreRecordCollection<TRecord>(collectionName, options, serviceId);
return builder;
Expand All @@ -94,7 +93,6 @@ public static IKernelBuilder AddAzureCosmosDBNoSQLVectorStoreRecordCollection<TR
string databaseName,
AzureCosmosDBNoSQLVectorStoreRecordCollectionOptions<TRecord>? options = default,
string? serviceId = default)
where TRecord : class
{
builder.Services.AddAzureCosmosDBNoSQLVectorStoreRecordCollection<TRecord>(collectionName, connectionString, databaseName, options, serviceId);
return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public static IServiceCollection AddAzureCosmosDBNoSQLVectorStoreRecordCollectio
string collectionName,
AzureCosmosDBNoSQLVectorStoreRecordCollectionOptions<TRecord>? options = default,
string? serviceId = default)
where TRecord : class
{
services.AddKeyedTransient<IVectorStoreRecordCollection<string, TRecord>>(
serviceId,
Expand Down Expand Up @@ -130,7 +129,6 @@ public static IServiceCollection AddAzureCosmosDBNoSQLVectorStoreRecordCollectio
string databaseName,
AzureCosmosDBNoSQLVectorStoreRecordCollectionOptions<TRecord>? options = default,
string? serviceId = default)
where TRecord : class
{
services.AddKeyedSingleton<IVectorStoreRecordCollection<string, TRecord>>(
serviceId,
Expand Down Expand Up @@ -160,7 +158,6 @@ public static IServiceCollection AddAzureCosmosDBNoSQLVectorStoreRecordCollectio
/// <param name="services">The service collection to register on.</param>
/// <param name="serviceId">The service id that the registrations should use.</param>
private static void AddVectorizedSearch<TRecord>(IServiceCollection services, string? serviceId)
where TRecord : class
{
services.AddKeyedTransient<IVectorizedSearch<TRecord>>(
serviceId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public AzureCosmosDBNoSQLVectorStore(Database database, AzureCosmosDBNoSQLVector
/// <inheritdoc />
public IVectorStoreRecordCollection<TKey, TRecord> GetCollection<TKey, TRecord>(string name, VectorStoreRecordDefinition? vectorStoreRecordDefinition = null)
where TKey : notnull
where TRecord : class
{
if (typeof(TKey) != typeof(string) && typeof(TKey) != typeof(AzureCosmosDBNoSQLCompositeKey))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ namespace Microsoft.SemanticKernel.Connectors.AzureCosmosDBNoSQL;
public sealed class AzureCosmosDBNoSQLVectorStoreRecordCollection<TRecord> :
IVectorStoreRecordCollection<string, TRecord>,
IVectorStoreRecordCollection<AzureCosmosDBNoSQLCompositeKey, TRecord>
where TRecord : class
#pragma warning restore CA1711 // Identifiers should not have incorrect
{
/// <summary>The name of this database for telemetry purposes.</summary>
Expand Down
Loading
Loading