Skip to content

Commit

Permalink
Volatile memory singleton for memory "migration" (#476)
Browse files Browse the repository at this point in the history
### Motivation and Context

<!-- Thank you for your contribution to the chat-copilot repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->
Muliple instances of VolatileMemory do not share state.

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] 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
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄

---------

Co-authored-by: Gil LaHaye <[email protected]>
  • Loading branch information
crickman and glahaye authored Oct 6, 2023
1 parent 112691c commit 3cde0ef
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion webapi/Controllers/MaintenanceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public MaintenanceController(

var migrationStatus = await migrationMonitor.GetCurrentStatusAsync(cancellationToken);

if (migrationStatus == ChatMigrationStatus.Upgrading)
if (migrationStatus != ChatMigrationStatus.None)
{
result =
new MaintenanceResult
Expand Down
10 changes: 3 additions & 7 deletions webapi/Services/MemoryMigration/ChatMigrationMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,11 @@ async Task<ChatMigrationStatus> QueryStatusAsync()
try
{
var result =
await this._memory.SearchAsync(
await this._memory.GetAsync(
this._indexNameGlobalDocs,
MigrationKey,
limit: 1,
minRelevanceScore: -1,
withEmbeddings: false,
cancellationToken)
.SingleOrDefaultAsync(cancellationToken)
;
withEmbedding: false,
cancellationToken);

if (result == null)
{
Expand Down
7 changes: 6 additions & 1 deletion webapi/Services/SemanticKernelProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Net.Http;
using System.Threading;
using CopilotChat.WebApi.Options;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -22,6 +23,8 @@ namespace CopilotChat.WebApi.Services;
/// </summary>
public sealed class SemanticKernelProvider
{
private static IMemoryStore? _volatileMemoryStore;

private readonly IServiceProvider _serviceProvider;
private readonly IConfiguration _configuration;

Expand Down Expand Up @@ -159,7 +162,9 @@ IMemoryStore CreateMemoryStore()
switch (memoryOptions.Retrieval.VectorDbType)
{
case string x when x.Equals("SimpleVectorDb", StringComparison.OrdinalIgnoreCase):
return new VolatileMemoryStore();
// 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>(this._configuration, "Qdrant");
Expand Down
3 changes: 1 addition & 2 deletions webapp/src/components/views/BackendProbe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ export const BackendProbe: FC<IData> = ({ onBackendFound }) => {
.then((data) => {
// Body has payload. This means the app is in maintenance
setModel(data as IMaintenance);
clearInterval(timer);
return false;
return true;
})
.catch((e: any) => {
if (e instanceof TypeError) {
Expand Down

0 comments on commit 3cde0ef

Please sign in to comment.