kernel-memory icon indicating copy to clipboard operation
kernel-memory copied to clipboard

[Bug] Using KM Plugin, cannot get RelevantSources

Open bancroftway opened this issue 9 months ago • 1 comments

Context / Scenario

I am using KM Plugin along with chat completion service's GetChatMessageContentAsync as I want to pass in a chat history. By using the plugin as opposed to calling KM's AskAsync method separately, I am not sure how to get the RelevantSources...Please advise how I can get Citations. My code is below:


public async Task<ChatHistory> AskQuestionFromDocuments(string userMessage, 
      string productName, 
      ChatHistory chatHistory, 
      string systemMessage,
      Dictionary<string, string> tagDictionary)
  {
      var skKernel = GetSkKernel();           

      MemoryFilter memoryFilter = new();
      if (tagDictionary?.Any() == true)
      {
          foreach (var kvp in tagDictionary)
          {
              memoryFilter = memoryFilter.ByTag(kvp.Key, kvp.Value);
          }
      }

      if (chatHistory.Any() == false)
      {
          chatHistory.AddSystemMessage(systemMessage);
      }            

      //Compile a final user question
      var currentUser = await currentUserService.GetApplicationUser();
      var userQuestion = $"This question pertains to product '{productName}'. User's name is '{currentUser.FirstName} {currentUser.LastName}' and email address is '{currentUser.Email}'. Question: {userMessage}. ";

      // Add user message to chat history
      chatHistory.AddUserMessage(userMessage);

      // Get your chat completion settings and kernel from configuration
      var chatCompletionService = skKernel.GetRequiredService<IChatCompletionService>();

      var kernelArguments = new KernelArguments()
      {
          ["input"] = userQuestion,
          [MemoryPlugin.QuestionParam] = userMessage,
          [MemoryPlugin.TagsParam] = memoryFilter,                
      };

      // Get assistant response
#pragma warning disable SKEXP0010 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
      var settings = new OpenAIPromptExecutionSettings
      {
          //ResponseFormat = "json_object", //what to put here
          Temperature =0.0,
          FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(),
          ExtensionData = kernelArguments.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)
      };
#pragma warning restore SKEXP0010 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.

      var assistantResponse = await chatCompletionService.GetChatMessageContentAsync(chatHistory,settings, skKernel);

      **//HOW TO RETRIEVE RELEVANTSOURCES (CITATIONS)**

      // Add assistant response to chat history
      chatHistory.AddAssistantMessage(assistantResponse.Content!);

      return  new ChatHistory(chatHistory.Where(x => !string.IsNullOrEmpty(x.Content)));                
  }

 private Kernel GetSkKernel()
    {
        var skKernel = Kernel.CreateBuilder()
        .AddAzureOpenAIChatCompletion(
            deploymentName: configuration["AzureServices:OpenAI:ModelName"]!,
            endpoint: configuration["AzureServices:OpenAI:Url"]!,
            apiKey: configuration["AzureServices:OpenAI:ApiKey"]!,
            apiVersion: configuration["AzureServices:OpenAI:ApiVersion"]!
        )
        .Build();

        // Register the SavingsCalculatorPlugin
        skKernel.Plugins.AddFromType<SavingsCalculatorPlugin>();


        var kmKernel = GetKernelMemoryClient();
        var plugin = new MemoryPlugin(kmKernel, waitForIngestionToComplete: true);
        skKernel.ImportPluginFromObject(plugin, "memory");

        return skKernel;
    }

What happened?

Cannot get citations when using KM Plugin

Importance

a fix would make my life easier

Platform, Language, Versions

.Net 9. KM Plugin version 0.97.250211.1, C#

Relevant log output


bancroftway avatar Mar 14 '25 02:03 bancroftway