How to apply memory filter when using Semantic Kernel Plugin and Chat Completion Service
bancroftway opened this issue · comments
Context / Scenario
I am using Memory Plugin (Microsoft.KernelMemory.SemanticKernelPlugin, version 0.75.240924.1) and Chat Completion Service. I have an 'args' variable containing the memory filter, however, it is not clear how to pass the KernelArguments to GetChatMessageContentAsync. Is there a way to pass in the Memory Filter or should I stick with the classic approach of using "kernel.InvokePromptAsync(prompt, arguments);"? Thanks
public async Task<Microsoft.SemanticKernel.ChatCompletion.ChatHistory> AskQuestionFromDocuments(string question,
Microsoft.SemanticKernel.ChatCompletion.ChatHistory chatHistory,
Dictionary<string, string> tagDictionary)
{
var skKernel = GetSkKernel();
var kmKernel = GetKernelMemoryClient();
var plugin = new MemoryPlugin(kmKernel, waitForIngestionToComplete: true);
skKernel.ImportPluginFromObject(plugin, "memory");
var skPrompt = $@"
Question to Kernel Memory: {question}
Kernel Memory Answer: {{memory.ask}}
If the answer is empty say 'I don't know', otherwise reply with the answer.
";
MemoryFilter memoryFilter = new MemoryFilter();
if (tagDictionary?.Any() == true)
{
foreach (var kvp in tagDictionary)
{
memoryFilter = memoryFilter.ByTag(kvp.Key, kvp.Value);
}
}
OpenAIPromptExecutionSettings settings = new()
{
ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions,
Temperature = 0.70,
};
var args = new KernelArguments(settings)
{
["input"] = question,
[MemoryPlugin.TagsParam] = memoryFilter
};
var chatCompletionService = skKernel.GetRequiredService<IChatCompletionService>();
chatHistory.AddMessage(Microsoft.SemanticKernel.ChatCompletion.AuthorRole.User, skPrompt);
var result = await chatCompletionService.GetChatMessageContentAsync(chatHistory, settings, skKernel);
chatHistory.AddMessage(Microsoft.SemanticKernel.ChatCompletion.AuthorRole.Assistant, result.Content);
return chatHistory;
}
What happened?
Memory filters are not getting applied when using chatCompletionService.GetChatMessageContentAsync
Importance
edge case
Platform, Language, Versions
.Net 9
Relevant log output
No response