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

Remove remnants of memory migration #823

Merged
merged 2 commits into from
Feb 28, 2024
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
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
Loading