Skip to content

Commit

Permalink
Merge branch 'main' into users/markwallace/enable_inmemorytextsearcht…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
markwallace-microsoft committed Oct 15, 2024
2 parents 30d60ce + e6c20cd commit af81b4a
Show file tree
Hide file tree
Showing 98 changed files with 332 additions and 253 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/dotnet-build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ jobs:
AzureOpenAITextToImage__Endpoint: ${{ secrets.AZUREOPENAITEXTTOIMAGE__ENDPOINT }}
AzureOpenAITextToImage__DeploymentName: ${{ vars.AZUREOPENAITEXTTOIMAGE__DEPLOYMENTNAME }}
Bing__ApiKey: ${{ secrets.BING__APIKEY }}
Google__SearchEngineId: ${{ secrets.GOOGLE__SEARCHENGINEID }}
Google__ApiKey: ${{ secrets.GOOGLE__APIKEY }}
OpenAI__ApiKey: ${{ secrets.OPENAI__APIKEY }}
OpenAI__ChatModelId: ${{ vars.OPENAI__CHATMODELID }}
AzureAIInference__ApiKey: ${{ secrets.AZUREAIINFERENCE__APIKEY }}
Expand Down
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
6 changes: 3 additions & 3 deletions dotnet/samples/Demos/StepwisePlannerMigration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ IChatCompletionService chatCompletionService = kernel.GetRequiredService<IChatCo
ChatHistory chatHistory = [];
chatHistory.AddUserMessage("Check current UTC time and return current weather in Boston city.");

OpenAIPromptExecutionSettings executionSettings = new() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
OpenAIPromptExecutionSettings executionSettings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() };

await chatCompletionService.GetChatMessageContentAsync(chatHistory, executionSettings, kernel);

Expand Down Expand Up @@ -87,7 +87,7 @@ Kernel kernel = Kernel
.AddOpenAIChatCompletion("gpt-4", Environment.GetEnvironmentVariable("OpenAI__ApiKey"))
.Build();

OpenAIPromptExecutionSettings executionSettings = new() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
OpenAIPromptExecutionSettings executionSettings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() };

FunctionResult result = await kernel.InvokePromptAsync("Check current UTC time and return current weather in Boston city.", new(executionSettings));

Expand Down Expand Up @@ -124,7 +124,7 @@ IChatCompletionService chatCompletionService = kernel.GetRequiredService<IChatCo

ChatHistory existingPlan = GetExistingPlan(); // plan can be stored in database for reusability.
OpenAIPromptExecutionSettings executionSettings = new() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
OpenAIPromptExecutionSettings executionSettings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() };

ChatMessageContent result = await chatCompletionService.GetChatMessageContentAsync(chatHistory, executionSettings, kernel);

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
Loading

0 comments on commit af81b4a

Please sign in to comment.