From 73e1e258a360ada3d58090520224862661a8359a Mon Sep 17 00:00:00 2001 From: Hugo Ramos Montesinos Date: Fri, 7 Jun 2024 14:29:25 +0200 Subject: [PATCH] Fixed some changes --- .../MemoryContent.cs | 2 +- .../Plugins/ChatWithHistoryPlugin.cs | 1 - .../Extensions/KernelExtensions.cs | 2 +- .../MemoryStoreExtender.cs | 10 ++++++---- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Encamina.Enmarcha.SemanticKernel.Abstractions/MemoryContent.cs b/src/Encamina.Enmarcha.SemanticKernel.Abstractions/MemoryContent.cs index 4c1d6b5..1a1a51a 100644 --- a/src/Encamina.Enmarcha.SemanticKernel.Abstractions/MemoryContent.cs +++ b/src/Encamina.Enmarcha.SemanticKernel.Abstractions/MemoryContent.cs @@ -8,7 +8,7 @@ public sealed class MemoryContent /// /// Gets the metadata of the memory. /// - public IDictionary? Metadata { get; init; } + public IDictionary Metadata { get; init; } /// /// Gets the chunks of the memory. diff --git a/src/Encamina.Enmarcha.SemanticKernel.Plugins.Chat/Plugins/ChatWithHistoryPlugin.cs b/src/Encamina.Enmarcha.SemanticKernel.Plugins.Chat/Plugins/ChatWithHistoryPlugin.cs index 2e63039..dba0b30 100644 --- a/src/Encamina.Enmarcha.SemanticKernel.Plugins.Chat/Plugins/ChatWithHistoryPlugin.cs +++ b/src/Encamina.Enmarcha.SemanticKernel.Plugins.Chat/Plugins/ChatWithHistoryPlugin.cs @@ -4,7 +4,6 @@ using Encamina.Enmarcha.SemanticKernel.Abstractions; using Encamina.Enmarcha.SemanticKernel.Plugins.Chat.Options; -using Microsoft.Azure.Cosmos; using Microsoft.Extensions.Options; using Microsoft.SemanticKernel; diff --git a/src/Encamina.Enmarcha.SemanticKernel/Extensions/KernelExtensions.cs b/src/Encamina.Enmarcha.SemanticKernel/Extensions/KernelExtensions.cs index 97bba5c..f766f95 100644 --- a/src/Encamina.Enmarcha.SemanticKernel/Extensions/KernelExtensions.cs +++ b/src/Encamina.Enmarcha.SemanticKernel/Extensions/KernelExtensions.cs @@ -313,7 +313,7 @@ private static string GetResourceNameFromPluginInfoByFileName(IGrouping<(string private static string ReadResource(Assembly assembly, string resourceName) { - using var stream = assembly.GetManifestResourceStream(resourceName); + using var stream = assembly.GetManifestResourceStream(resourceName) ?? throw new InvalidOperationException($"The resource stream for '{resourceName}' is null. Ensure the resource name is correct and the resource is embedded in the assembly."); using var streamReader = new StreamReader(stream!); return streamReader.ReadToEnd(); diff --git a/src/Encamina.Enmarcha.SemanticKernel/MemoryStoreExtender.cs b/src/Encamina.Enmarcha.SemanticKernel/MemoryStoreExtender.cs index de6fc64..c75330d 100644 --- a/src/Encamina.Enmarcha.SemanticKernel/MemoryStoreExtender.cs +++ b/src/Encamina.Enmarcha.SemanticKernel/MemoryStoreExtender.cs @@ -76,9 +76,12 @@ public virtual async Task DeleteMemoryAsync(string memoryId, string collectionNa var memoryRecords = await MemoryStore.GetBatchAsync(collectionName, keys, cancellationToken: cancellationToken).ToListAsync(cancellationToken); RaiseMemoryStoreEvent(new() { EventType = MemoryStoreEventTypes.GetMemory, MemoryIds = [memoryId], CollectionName = collectionName }); + var metadata = JsonSerializer.Deserialize>(memoryRecords[0].Metadata.AdditionalMetadata); + metadata ??= new Dictionary(); + return new MemoryContent { - Metadata = JsonSerializer.Deserialize>(memoryRecords[0].Metadata.AdditionalMetadata), + Metadata = metadata, Chunks = memoryRecords.Select(m => m.Metadata.Text), }; } @@ -107,7 +110,7 @@ public virtual async IAsyncEnumerable BatchUpsertMemoriesAsync(string co if (totalChunks > 0) { - memoryContent.Metadata?.Add(Constants.MetadataTotalChunksCount, totalChunks.ToString()); + memoryContent.Metadata.Add(Constants.MetadataTotalChunksCount, totalChunks.ToString()); var embeddingTasks = memoryContent.Chunks.Select(async (chunk, i) => { @@ -126,8 +129,7 @@ public virtual async IAsyncEnumerable BatchUpsertMemoriesAsync(string co await foreach (var key in asyncKeys) { - var message = @"Processed memory record {item}."; - logger.LogInformation(message, key); + logger.LogInformation(@"Processed memory record {Item}.", key); keys.Add(key);