Skip to content

Chat memories are not deleted when chat session is deleted #946

@jonepo

Description

@jonepo

Describe the bug
I noticed that in static class ISemanticMemoryClientExtensions in extension method RemoveChatMemoriesAsync, it is parsing the document ids wrong.

public static async Task RemoveChatMemoriesAsync(
        this IKernelMemory memoryClient,
        string indexName,
        string chatId,
        CancellationToken cancellationToken = default)
    {
        var memories = await memoryClient.SearchMemoryAsync(indexName, "*", 0.0F, chatId, cancellationToken: cancellationToken);
        var documentIds = memories.Results.Select(memory => memory.Link.Split('/').First()).Distinct().ToArray();
        var tasks = documentIds.Select(documentId => memoryClient.DeleteDocumentAsync(documentId, indexName, cancellationToken)).ToArray();

        Task.WaitAll(tasks, cancellationToken);
    }

The method above tries to get the document id for each result with memory.Link.Split('/').First(). As the link is actually in format of $"{index}/{documentId}/{fileId}", it ends up picking up the index name. The documentIds array variable will always only contain the index names. Because of that, the document deletion doesn't work properly.

I can create a pull request to fix this if you agree of the issue. Instead of using the first element, we must get the second element in the split array. I don't have an instance of chat-copilot running so I couldn't test it in practice, but by reading the code, it seems to be a clear case.

To Reproduce
Steps to reproduce the behavior:

  1. Create a conversation
  2. Chat a few times to collect memories
  3. Confirm that the index contains memories for the session
  4. Delete the conversation (chat session)
  5. Check if the memories exist in the index

Expected behavior
Memories for the deleted chat session are deleted.

Platform

  • Language: C#
  • Source: main branch

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions