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

Update Memory Manager #54

Merged
merged 1 commit into from
Jan 19, 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
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 @@ -21,10 +21,10 @@
/// <param name="memoryId">The memory unique identifier.</param>
/// <param name="collectionName">Name of the collection where the content will be saved.</param>
/// <param name="chunks">List of strings containing the content of the memory.</param>
/// <param name="cancellationToken">Cancellation token to cancel the operation.</param>

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

View workflow job for this annotation

GitHub Actions / CI

The parameter documentation for 'cancellationToken' should be at position 6 (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1612.md)

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

View workflow job for this annotation

GitHub Actions / CI

The parameter documentation for 'cancellationToken' should be at position 6 (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1612.md)
/// <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

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


/// <summary>
/// Deletes the memory content from a collection.
Expand Down Expand Up @@ -53,7 +53,7 @@
/// The <c>key</c> in the dictionary must contain a unique identifier for the content of the memory,
/// and the <c>value</c> of the dictionary must provide the memory content (chunks and metadata).
/// </param>
/// <param name="cancellationToken">Cancellation token to cancel the operation.</param>

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

View workflow job for this annotation

GitHub Actions / CI

The parameter documentation for 'cancellationToken' should be at position 4 (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1612.md)

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

View workflow job for this annotation

GitHub Actions / CI

The parameter documentation for 'cancellationToken' should be at position 4 (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1612.md)
/// <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

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 @@

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)

Check warning on line 29 in src/Encamina.Enmarcha.SemanticKernel/MemoryManager.cs

View workflow job for this annotation

GitHub Actions / CI

Parameter 'logger' has no matching param tag in the XML comment for 'MemoryManager.MemoryManager(IMemoryStore, ILogger<MemoryManager>)' (but other parameters do)

Check warning on line 29 in src/Encamina.Enmarcha.SemanticKernel/MemoryManager.cs

View workflow job for this annotation

GitHub Actions / CI

Parameter 'logger' has no matching param tag in the XML comment for 'MemoryManager.MemoryManager(IMemoryStore, ILogger<MemoryManager>)' (but other parameters do)
{
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 @@
}

/// <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 @@
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 @@
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 @@
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