Skip to content

Commit

Permalink
Remove remnants of memory migration (microsoft#823)
Browse files Browse the repository at this point in the history
### 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 😄
  • Loading branch information
glahaye authored Feb 28, 2024
1 parent 8da61c2 commit 4559fce
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 110 deletions.
12 changes: 0 additions & 12 deletions webapi/Options/DocumentMemoryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,6 @@ public class DocumentMemoryOptions
/// </summary>
internal static readonly Guid GlobalDocumentChatId = Guid.Empty;

/// <summary>
/// Gets or sets the name of the global document collection.
/// </summary>
[Required, NotEmptyOrWhitespace]
public string GlobalDocumentCollectionName { get; set; } = "global-documents";

/// <summary>
/// Gets or sets the prefix for the chat document collection name.
/// </summary>
[Required, NotEmptyOrWhitespace]
public string ChatDocumentCollectionNamePrefix { get; set; } = "chat-documents-";

/// <summary>
/// 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:
Expand Down
2 changes: 1 addition & 1 deletion webapi/Plugins/Chat/ChatPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ private async Task<string> GetAllowedChatHistoryAsync(

/// <summary>
/// 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.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
Expand Down
95 changes: 0 additions & 95 deletions webapi/Services/SemanticKernelProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,27 +15,18 @@ namespace CopilotChat.WebApi.Services;
/// </summary>
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);
}

/// <summary>
/// Produce semantic-kernel with only completion services for chat.
/// </summary>
public Kernel GetCompletionKernel() => this._builderChat.Build();

/// <summary>
/// Produce semantic-kernel with kernel memory.
/// </summary>
public ISemanticTextMemory MigrationMemory => this._builderMemory.Build();

private static IKernelBuilder InitializeCompletionKernel(
IServiceProvider serviceProvider,
IConfiguration configuration,
Expand All @@ -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<OpenAIConfig>(configuration, "OpenAI");
#pragma warning disable CA2000 // No need to dispose of HttpClient instances from IHttpClientFactory
builder.AddOpenAIChatCompletion(
openAIOptions.TextModel,
openAIOptions.APIKey,
Expand All @@ -83,82 +66,4 @@ private static IKernelBuilder InitializeCompletionKernel(

return builder;
}

private static MemoryBuilder InitializeMigrationMemory(
IServiceProvider serviceProvider,
IConfiguration configuration,
IHttpClientFactory httpClientFactory)
{
var memoryOptions = serviceProvider.GetRequiredService<IOptions<KernelMemoryConfig>>().Value;

var builder = new MemoryBuilder();

builder.WithLoggerFactory(serviceProvider.GetRequiredService<ILoggerFactory>());
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<AzureOpenAIConfig>(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<OpenAIConfig>(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<QdrantConfig>(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<ILoggerFactory>());

case string x when x.Equals("AzureAISearch", StringComparison.OrdinalIgnoreCase):
var acsConfig = memoryOptions.GetServiceConfig<AzureAISearchConfig>(configuration, "AzureAISearch");
return new AzureAISearchMemoryStore(acsConfig.Endpoint, acsConfig.APIKey);

default:
throw new InvalidOperationException($"Invalid 'MemoryDbType' type '{memoryOptions.Retrieval.MemoryDbType}'.");
}
}
}
}
2 changes: 0 additions & 2 deletions webapi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 4559fce

Please sign in to comment.