microsoft / kernel-memory

RAG architecture: index and query any data using LLM and natural language, track sources, show citations, asynchronous memory patterns.

Home Page:https://microsoft.github.io/kernel-memory

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] MemoryFilter issue

KSemenenko opened this issue · comments

Context / Scenario

I found a problem with filtering,
I need to search in several files whose id I know

var filter = new MemoryFilter();

foreach (var file in _files)
{
    if (file.IsReady)
    {
        filter = filter.ByDocument(file.DocumentId);
    }
}

but as a result, I don't get any answers.
although when I have one file everything works fine.

but if I will use some like

var filter = new MemoryFilter().ByTag("tag", "MyFilesTag");

then it works

What happened?

search cannot find the night one when two files are added in the filter

Importance

a fix would make my life easier

Platform, Language, Versions

C#

Relevant log output

No response

 List<MemoryFilter> filters = new List<MemoryFilter>();
foreach (var file in _files)
{
    if(file.IsReady)
        filters.Add(new MemoryFilter().ByDocument(file.DocumentId));
}

MemoryAnswer answer = await this._memory.AskAsync(
question: question,
filters: filters,
minRelevance: MinRelevance,
cancellationToken: cancellationToken).ConfigureAwait(false);

this is works fine too

hi @KSemenenko that code works as intended, I don't think it's a bug. Let's say you import two files as two separate documents:

File 1 => Doc ID = A
File 2 => Doc ID = B

In the initial code, you are using a single filter with two conditions, asking for memories extracted from both files, and the result is empty, which is correct.
The code is doing:

var filter = new MemoryFilter();
filter.ByDocument("A");
filter.ByDocument("B");

which results in:

find memories where docId == "A" AND docId == "B"

In your latest snippet of code, you're using two filters, which results in:

find memories where docId == "A" OR docId == "B"

Now I get it, thanks for explanation 👍