microsoft / kernel-memory

RAG architecture: index and query any data using LLM and natural language, track sources, show citations, asynchronous memory patterns.
https://microsoft.github.io/kernel-memory
MIT License
1.58k stars 305 forks source link

Can't find a way to get the Summary out that was generated for the imported document? #194

Closed JohnGalt1717 closed 10 months ago

JohnGalt1717 commented 10 months ago

Bing chat says that this should work:

var document = new Document("path/to/your/document");
document.Import();

// Get the summary
var summary = document.GetSummary();

which obviously doesn't and I can't find anything in the documentation to get the summary that was extracted during the pipeline stage when using Constants.PipelineWithSummary

How does one retrieve it?

dluc commented 10 months ago

hi @JohnGalt1717 we've made it easier in the latest version 0.20, e.g.

var results = await memory.SearchSummariesAsync(filter: MemoryFilters.ByDocument("doc1"));

That will work with any pipeline as long as the summarization step is included. Here's a complete example: https://github.com/microsoft/kernel-memory/blob/main/examples/106-dotnet-retrieve-synthetics/Program.cs

You can leverage the same feature if you have custom handlers that generate synthetic memories (the summary is a synthetic memory). For instance, assume you have a TranslationHandler that translates documents, adding the "translation" synthetic tag (AddSyntheticTag("translation")), you can then retrieve those translations with code like this:

var results = await memory.SearchSyntheticsAsync("translation", filter: MemoryFilters.ByDocument("doc1"));
dluc commented 10 months ago

forgot to add, if you're using the web service, you can also do:

POST /search
{
    "filters": [
        {
            "__document_id": ["doc1"],
            "__synth": ["summary"]
        }
    ]
}
JohnGalt1717 commented 10 months ago

Great! Thanks!