Skip to content

Commit

Permalink
Merge pull request #54 from Encamina/@rliberoff/improvements_on_ephem…
Browse files Browse the repository at this point in the history
…aral_memory_handler

Update Memory Manager
  • Loading branch information
rliberoff authored Jan 19, 2024
2 parents e415f80 + 2fe6db3 commit ddd61f6
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 23 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Previous classification is not required if changes are simple or all belong to t
- Removed visibility modifiers in `IMemoryManager` interface.
- Signature of `UpsertMemoryAsync` method has changed in `IMemoryManager` interface.
- Signature of `BatchUpsertMemoriesAsync` method has changed in `IMemoryManager` interface.
- Dependency with `Kernel` has been replace with dependency to `ITextEmbeddingGenerationService` in `MemoryManager` class. Also, added dependency with `ILogger`.
- Dependency with `Kernel` has been removed in `MemoryManager` class. Also, added dependency with `ILogger`.

### Major change
- Method `GetDocumentConnector` in `DocumentContentExtractorBase` is now `public` instead of `protected`.
Expand All @@ -36,6 +36,7 @@ Previous classification is not required if changes are simple or all belong to t
- In `EphemeralMemoryStoreHandler`, property `CollectionNamePrefix` has the value `ephemeral-` fixed.
- Fixed some typos and grammatical errors (mostly on code comments).
- Added new extension method `AddDefaultDocumentConnectorProvider` in `Encamina.Enmarcha.SemanticKernel.Connectors.Document` to get access to a default implementation of a `IDocumentConnector`.
- Updated sample projects with latest changes.

## [8.1.1]

Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<PropertyGroup>
<VersionPrefix>8.1.2</VersionPrefix>
<VersionSuffix>preview-7</VersionSuffix>
<VersionSuffix>preview-8</VersionSuffix>
</PropertyGroup>

<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ public static async Task RunAsync()

var host = hostBuilder.Build();

var kernel = host.Services.GetRequiredService<Kernel>();

// Initialize mock memory
var mockMemoryInformation = new MockMemoryInformation(host.Services.GetRequiredService<IMemoryManager>(), host.Services.GetRequiredService<IMemoryStore>());
await mockMemoryInformation.CreateCollection();
await mockMemoryInformation.SaveDataMockAsync();
await mockMemoryInformation.SaveDataMockAsync(kernel);

// Initialize Q&A from Memory
var testQuestionAnswering = new TestQuestionAnswering(host.Services.GetService<Kernel>());
var testQuestionAnswering = new TestQuestionAnswering(kernel);
var result = await testQuestionAnswering.TestQuestionAnsweringFromMemoryAsync();

Console.WriteLine($@"RESULT: {result}");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Encamina.Enmarcha.SemanticKernel.Abstractions;

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Memory;

namespace Encamina.Enmarcha.Samples.SemanticKernel.QuestionAnswering;
Expand All @@ -20,7 +21,7 @@ public async Task CreateCollection()
await memoryStore.CreateCollectionAsync(collectionName: "my-collection", CancellationToken.None);
}

public async Task SaveDataMockAsync()
public async Task SaveDataMockAsync(Kernel kernel)
{
var memoryId = Guid.NewGuid().ToString();

Expand All @@ -33,6 +34,6 @@ public async Task SaveDataMockAsync()
Console.WriteLine($"# Second chunk: {secondChunkText} \n");
Console.WriteLine($"# Third chunk: {thirdChunkText} \n");

await memoryManager.UpsertMemoryAsync(memoryId: memoryId, collectionName: "my-collection", chunks: new List<string> { firstChunkText, secondChunkText, thirdChunkText }, cancellationToken: CancellationToken.None);
await memoryManager.UpsertMemoryAsync(memoryId: memoryId, collectionName: "my-collection", chunks: new List<string> { firstChunkText, secondChunkText, thirdChunkText }, kernel, cancellationToken: CancellationToken.None);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface IMemoryManager
/// <param name="cancellationToken">Cancellation token to cancel the operation.</param>
/// <param name="metadata">Metadata of the memory.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task UpsertMemoryAsync(string memoryId, string collectionName, IEnumerable<string> chunks, IDictionary<string, string> metadata = null, Kernel kernel = null, CancellationToken cancellationToken = default);
Task UpsertMemoryAsync(string memoryId, string collectionName, IEnumerable<string> chunks, Kernel kernel, IDictionary<string, string> metadata = null, CancellationToken cancellationToken = default);

Check warning on line 27 in src/Encamina.Enmarcha.SemanticKernel.Abstractions/IMemoryManager.cs

View workflow job for this annotation

GitHub Actions / CI

Parameter 'kernel' has no matching param tag in the XML comment for 'IMemoryManager.UpsertMemoryAsync(string, string, IEnumerable<string>, Kernel, IDictionary<string, string>, CancellationToken)' (but other parameters do)

Check warning on line 27 in src/Encamina.Enmarcha.SemanticKernel.Abstractions/IMemoryManager.cs

View workflow job for this annotation

GitHub Actions / CI

Split this 202 characters long line (which is greater than 200 authorized). (https://rules.sonarsource.com/csharp/RSPEC-103)

Check warning on line 27 in src/Encamina.Enmarcha.SemanticKernel.Abstractions/IMemoryManager.cs

View workflow job for this annotation

GitHub Actions / CI

Use the overloading mechanism instead of the optional parameters. (https://rules.sonarsource.com/csharp/RSPEC-2360)

Check warning on line 27 in src/Encamina.Enmarcha.SemanticKernel.Abstractions/IMemoryManager.cs

View workflow job for this annotation

GitHub Actions / CI

Use the overloading mechanism instead of the optional parameters. (https://rules.sonarsource.com/csharp/RSPEC-2360)

Check warning on line 27 in src/Encamina.Enmarcha.SemanticKernel.Abstractions/IMemoryManager.cs

View workflow job for this annotation

GitHub Actions / CI

Parameter 'kernel' has no matching param tag in the XML comment for 'IMemoryManager.UpsertMemoryAsync(string, string, IEnumerable<string>, Kernel, IDictionary<string, string>, CancellationToken)' (but other parameters do)

/// <summary>
/// Deletes the memory content from a collection.
Expand Down Expand Up @@ -55,5 +55,5 @@ public interface IMemoryManager
/// </param>
/// <param name="cancellationToken">Cancellation token to cancel the operation.</param>
/// <returns>The unique identifiers for the memory records (not necessarily the same as the unique identifier of the memory content).</returns>
IAsyncEnumerable<string> BatchUpsertMemoriesAsync(string collectionName, IDictionary<string, MemoryContent> memoryContents, Kernel kernel = null, CancellationToken cancellationToken = default);
IAsyncEnumerable<string> BatchUpsertMemoriesAsync(string collectionName, IDictionary<string, MemoryContent> memoryContents, Kernel kernel, CancellationToken cancellationToken);

Check warning on line 58 in src/Encamina.Enmarcha.SemanticKernel.Abstractions/IMemoryManager.cs

View workflow job for this annotation

GitHub Actions / CI

Parameter 'kernel' has no matching param tag in the XML comment for 'IMemoryManager.BatchUpsertMemoriesAsync(string, IDictionary<string, MemoryContent>, Kernel, CancellationToken)' (but other parameters do)

Check warning on line 58 in src/Encamina.Enmarcha.SemanticKernel.Abstractions/IMemoryManager.cs

View workflow job for this annotation

GitHub Actions / CI

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

using Microsoft.SemanticKernel.Memory;

namespace Encamina.Enmarcha.SemanticKernel;

/// <summary>
Expand Down
19 changes: 6 additions & 13 deletions src/Encamina.Enmarcha.SemanticKernel/MemoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,21 @@ public class MemoryManager : IMemoryManager

private readonly ILogger logger;

private readonly ITextEmbeddingGenerationService textEmbeddingGenerationService;

/// <summary>
/// Initializes a new instance of the <see cref="MemoryManager"/> class.
/// </summary>
/// <param name="kernel">
/// A valid instance of <see cref="Kernel"/>, used to get the configured text embeddings generation service (<see cref="ITextEmbeddingGenerationService"/>) required by this manager.
/// </param>
/// <param name="memoryStore">A valid instance of a <see cref="IMemoryStore"/> to manage.</param>
public MemoryManager(IMemoryStore memoryStore, ITextEmbeddingGenerationService textEmbeddingGenerationService, ILogger<MemoryManager> logger)
public MemoryManager(IMemoryStore memoryStore, ILogger<MemoryManager> logger)
{
this.logger = logger;
MemoryStore = memoryStore;

this.textEmbeddingGenerationService = textEmbeddingGenerationService;
}

/// <inheritdoc/>
public IMemoryStore MemoryStore { get; init; }

/// <inheritdoc/>
public virtual async Task UpsertMemoryAsync(string memoryId, string collectionName, IEnumerable<string> chunks, IDictionary<string, string> metadata = null, Kernel kernel = null, CancellationToken cancellationToken = default)
public virtual async Task UpsertMemoryAsync(string memoryId, string collectionName, IEnumerable<string> chunks, Kernel kernel, IDictionary<string, string> metadata = null, CancellationToken cancellationToken = default)
{
var memoryChunkSize = await GetChunkSize(memoryId, collectionName, cancellationToken);

Expand Down Expand Up @@ -87,7 +80,7 @@ public virtual async Task<MemoryContent> GetMemoryAsync(string memoryId, string
}

/// <inheritdoc/>
public virtual async IAsyncEnumerable<string> BatchUpsertMemoriesAsync(string collectionName, IDictionary<string, MemoryContent> memoryContents, Kernel kernel = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
public virtual async IAsyncEnumerable<string> BatchUpsertMemoriesAsync(string collectionName, IDictionary<string, MemoryContent> memoryContents, Kernel kernel, [EnumeratorCancellation] CancellationToken cancellationToken)
{
var memoryRecords = new Collection<MemoryRecord>();

Expand All @@ -104,7 +97,7 @@ public virtual async IAsyncEnumerable<string> BatchUpsertMemoriesAsync(string co
for (var i = 0; i < totalChunks; i++)
{
var chunk = memoryContent.Chunks.ElementAt(i);
var embedding = await textEmbeddingGenerationService.GenerateEmbeddingAsync(chunk, kernel, cancellationToken);
var embedding = await kernel.GetRequiredService<ITextEmbeddingGenerationService>().GenerateEmbeddingAsync(chunk, kernel, cancellationToken);
memoryRecords.Add(MemoryRecord.LocalRecord($@"{memoryContentId}-{i}", chunk, null, embedding, JsonSerializer.Serialize(memoryContent.Metadata)));
}
}
Expand Down Expand Up @@ -140,7 +133,7 @@ private async Task DeleteMemoryAsync(string memoryId, string collectionName, int
await MemoryStore.RemoveBatchAsync(collectionName, Enumerable.Range(0, chunkSize).Select(i => BuildMemoryIdentifier(memoryId, i)), cancellationToken);
}

private async Task SaveChunks(string memoryid, string collectionName, IEnumerable<string> chunks, IDictionary<string, string> metadata, Kernel kernel = null, CancellationToken cancellationToken = default)
private async Task SaveChunks(string memoryid, string collectionName, IEnumerable<string> chunks, IDictionary<string, string> metadata, Kernel kernel, CancellationToken cancellationToken)
{
metadata ??= new Dictionary<string, string>();

Expand All @@ -155,7 +148,7 @@ private async Task SaveChunks(string memoryid, string collectionName, IEnumerabl
for (var i = 0; i < chunksCount; i++)
{
var chunk = chunks.ElementAt(i);
var embedding = await textEmbeddingGenerationService.GenerateEmbeddingAsync(chunk, kernel, cancellationToken);
var embedding = await kernel.GetRequiredService<ITextEmbeddingGenerationService>().GenerateEmbeddingAsync(chunk, kernel, cancellationToken);
memoryRecords.Add(MemoryRecord.LocalRecord(BuildMemoryIdentifier(memoryid, i), chunk, null, embedding, metadataJson));
}

Expand Down

0 comments on commit ddd61f6

Please sign in to comment.