From 4559fcec6ccd9b09543f392dce55c79e78ab16fa Mon Sep 17 00:00:00 2001 From: Gil LaHaye Date: Wed, 28 Feb 2024 09:39:03 -0800 Subject: [PATCH] Remove remnants of memory migration (#823) ### Motivation and Context Memory migration has been removed but some of its code was still present. ### Description Remove leftover memory migration code ### Contribution Checklist - [ ] The code builds clean without any errors or warnings - [ ] The PR follows the [Contribution Guidelines](https://github.com/microsoft/chat-copilot/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/chat-copilot/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone :smile: --- webapi/Options/DocumentMemoryOptions.cs | 12 --- webapi/Plugins/Chat/ChatPlugin.cs | 2 +- webapi/Services/SemanticKernelProvider.cs | 95 ----------------------- webapi/appsettings.json | 2 - 4 files changed, 1 insertion(+), 110 deletions(-) diff --git a/webapi/Options/DocumentMemoryOptions.cs b/webapi/Options/DocumentMemoryOptions.cs index f126c31f3..5d50aa6b8 100644 --- a/webapi/Options/DocumentMemoryOptions.cs +++ b/webapi/Options/DocumentMemoryOptions.cs @@ -17,18 +17,6 @@ public class DocumentMemoryOptions /// internal static readonly Guid GlobalDocumentChatId = Guid.Empty; - /// - /// Gets or sets the name of the global document collection. - /// - [Required, NotEmptyOrWhitespace] - public string GlobalDocumentCollectionName { get; set; } = "global-documents"; - - /// - /// Gets or sets the prefix for the chat document collection name. - /// - [Required, NotEmptyOrWhitespace] - public string ChatDocumentCollectionNamePrefix { get; set; } = "chat-documents-"; - /// /// Gets or sets the maximum number of tokens to use when splitting a document into "lines". /// For more details on tokens and how to count them, see: diff --git a/webapi/Plugins/Chat/ChatPlugin.cs b/webapi/Plugins/Chat/ChatPlugin.cs index 7e33951c1..6a8869bfd 100644 --- a/webapi/Plugins/Chat/ChatPlugin.cs +++ b/webapi/Plugins/Chat/ChatPlugin.cs @@ -276,7 +276,7 @@ private async Task GetAllowedChatHistoryAsync( /// /// This is the entry point for getting a chat response. It manages the token limit, saves - /// messages to memory, and fill in the necessary context variables for completing the + /// messages to memory, and fills in the necessary context variables for completing the /// prompt that will be rendered by the template engine. /// /// The cancellation token. diff --git a/webapi/Services/SemanticKernelProvider.cs b/webapi/Services/SemanticKernelProvider.cs index 9879d54d7..218ed140d 100644 --- a/webapi/Services/SemanticKernelProvider.cs +++ b/webapi/Services/SemanticKernelProvider.cs @@ -2,17 +2,11 @@ using System; using System.Net.Http; -using System.Threading; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.KernelMemory; using Microsoft.SemanticKernel; -using Microsoft.SemanticKernel.Connectors.AzureAISearch; -using Microsoft.SemanticKernel.Connectors.OpenAI; -using Microsoft.SemanticKernel.Connectors.Qdrant; -using Microsoft.SemanticKernel.Memory; namespace CopilotChat.WebApi.Services; @@ -21,15 +15,11 @@ namespace CopilotChat.WebApi.Services; /// public sealed class SemanticKernelProvider { - private static IMemoryStore? _volatileMemoryStore; - private readonly IKernelBuilder _builderChat; - private readonly MemoryBuilder _builderMemory; public SemanticKernelProvider(IServiceProvider serviceProvider, IConfiguration configuration, IHttpClientFactory httpClientFactory) { this._builderChat = InitializeCompletionKernel(serviceProvider, configuration, httpClientFactory); - this._builderMemory = InitializeMigrationMemory(serviceProvider, configuration, httpClientFactory); } /// @@ -37,11 +27,6 @@ public SemanticKernelProvider(IServiceProvider serviceProvider, IConfiguration c /// public Kernel GetCompletionKernel() => this._builderChat.Build(); - /// - /// Produce semantic-kernel with kernel memory. - /// - public ISemanticTextMemory MigrationMemory => this._builderMemory.Build(); - private static IKernelBuilder InitializeCompletionKernel( IServiceProvider serviceProvider, IConfiguration configuration, @@ -64,12 +49,10 @@ private static IKernelBuilder InitializeCompletionKernel( azureAIOptions.Endpoint, azureAIOptions.APIKey, httpClient: httpClientFactory.CreateClient()); -#pragma warning restore CA2000 break; case string x when x.Equals("OpenAI", StringComparison.OrdinalIgnoreCase): var openAIOptions = memoryOptions.GetServiceConfig(configuration, "OpenAI"); -#pragma warning disable CA2000 // No need to dispose of HttpClient instances from IHttpClientFactory builder.AddOpenAIChatCompletion( openAIOptions.TextModel, openAIOptions.APIKey, @@ -83,82 +66,4 @@ private static IKernelBuilder InitializeCompletionKernel( return builder; } - - private static MemoryBuilder InitializeMigrationMemory( - IServiceProvider serviceProvider, - IConfiguration configuration, - IHttpClientFactory httpClientFactory) - { - var memoryOptions = serviceProvider.GetRequiredService>().Value; - - var builder = new MemoryBuilder(); - - builder.WithLoggerFactory(serviceProvider.GetRequiredService()); - builder.WithMemoryStore(CreateMemoryStore()); - - switch (memoryOptions.Retrieval.EmbeddingGeneratorType) - { - case string x when x.Equals("AzureOpenAI", StringComparison.OrdinalIgnoreCase): - case string y when y.Equals("AzureOpenAIEmbedding", StringComparison.OrdinalIgnoreCase): - var azureAIOptions = memoryOptions.GetServiceConfig(configuration, "AzureOpenAIEmbedding"); -#pragma warning disable CA2000 // No need to dispose of HttpClient instances from IHttpClientFactory - builder.WithAzureOpenAITextEmbeddingGeneration( - azureAIOptions.Deployment, - azureAIOptions.Endpoint, - azureAIOptions.APIKey, - httpClient: httpClientFactory.CreateClient()); -#pragma warning restore CA2000 - break; - - case string x when x.Equals("OpenAI", StringComparison.OrdinalIgnoreCase): - var openAIOptions = memoryOptions.GetServiceConfig(configuration, "OpenAI"); -#pragma warning disable CA2000 // No need to dispose of HttpClient instances from IHttpClientFactory - builder.WithOpenAITextEmbeddingGeneration( - openAIOptions.EmbeddingModel, - openAIOptions.APIKey, - httpClient: httpClientFactory.CreateClient()); -#pragma warning restore CA2000 - break; - - default: - throw new ArgumentException($"Invalid {nameof(memoryOptions.Retrieval.EmbeddingGeneratorType)} value in 'KernelMemory' settings."); - } - return builder; - - IMemoryStore CreateMemoryStore() - { - switch (memoryOptions.Retrieval.MemoryDbType) - { - case string x when x.Equals("SimpleVectorDb", StringComparison.OrdinalIgnoreCase): - // Maintain single instance of volatile memory. - Interlocked.CompareExchange(ref _volatileMemoryStore, new VolatileMemoryStore(), null); - return _volatileMemoryStore; - - case string x when x.Equals("Qdrant", StringComparison.OrdinalIgnoreCase): - var qdrantConfig = memoryOptions.GetServiceConfig(configuration, "Qdrant"); - -#pragma warning disable CA2000 // Ownership passed to QdrantMemoryStore - HttpClient httpClient = new(new HttpClientHandler { CheckCertificateRevocationList = true }); -#pragma warning restore CA2000 // Ownership passed to QdrantMemoryStore - if (!string.IsNullOrWhiteSpace(qdrantConfig.APIKey)) - { - httpClient.DefaultRequestHeaders.Add("api-key", qdrantConfig.APIKey); - } - - return - new QdrantMemoryStore( - httpClient: httpClient, - 1536, - qdrantConfig.Endpoint, - loggerFactory: serviceProvider.GetRequiredService()); - - case string x when x.Equals("AzureAISearch", StringComparison.OrdinalIgnoreCase): - var acsConfig = memoryOptions.GetServiceConfig(configuration, "AzureAISearch"); - return new AzureAISearchMemoryStore(acsConfig.Endpoint, acsConfig.APIKey); - - default: - throw new InvalidOperationException($"Invalid 'MemoryDbType' type '{memoryOptions.Retrieval.MemoryDbType}'."); - } - } - } } diff --git a/webapi/appsettings.json b/webapi/appsettings.json index f11e34665..10316c136 100644 --- a/webapi/appsettings.json +++ b/webapi/appsettings.json @@ -95,8 +95,6 @@ // https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-6.0 // "DocumentMemory": { - "GlobalDocumentCollectionName": "global-documents", // OBSOLETE: Used for legacy migration - "ChatDocumentCollectionNamePrefix": "chat-documents-", // OBSOLETE: Used for legacy migration "DocumentLineSplitMaxTokens": 72, "DocumentChunkMaxTokens": 512, "FileSizeLimit": 4000000,