Skip to content

Lesson 3: Simple Semantic Kernel chat agent with plugins

In this lesson we will a semantic kernel plugins to be able to retrieve stock pricing.

  1. Ensure all pre-requisites are met and installed (including updating the StockService apiKey value in the appSettings.json file using the key from polygon.io).

  2. Switch to Lesson 3 directory:

    cd ../Lesson3
    
  3. Start by copying appsettings.json from Lesson 1:

    cp ../Lesson1/appsettings.json .
    
  4. Run program and ask what the current date is:

    dotnet run
    

At the prompt enter

What is the current date?
 ```

Assistant will give a similar response:

```txt
Assistant > I can't access today's date, but imagine it’s an eternal "Fri-yay," ready for financial fun! How can I help you on this hypothetical day?
  1. Notice it does not provide a specific answer. We can use a Semantic Kernel Plugin to be able to fix that.

  2. In the Plugins directory from Core.Utilities directory review the file named TimeInformationPlugin.cs which has the following content:

    using System.ComponentModel;
    using Microsoft.SemanticKernel;
    
    namespace Plugins;
    
    public class TimeInformationPlugin
    {
        [KernelFunction] 
        [Description("Retrieves the current time in UTC.")]
        public string GetCurrentUtcTime() => DateTime.UtcNow.ToString("R");
    }
    
  3. Next locate TODO: Step 1 in Program.cs and add the following import line:

    using Core.Utilities.Plugins;
    
  4. Next locate TODO: Step 2 in Program.cs and provide the following line to register the TimeInformationPlugin:

    kernel.Plugins.AddFromObject(new TimeInformationPlugin());
    
  5. Next locate TODO: Step 3 and add the following line to be able to auto invoke kernel functions:

        ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions
    
  6. Next locate TODO: Step 4 and add the following parameters:

        await foreach (var chatUpdate in chatCompletionService.GetStreamingChatMessageContentsAsync(chatHistory, promptExecutionSettings, kernel))
    
  7. Re-run the program and ask what the current date is. The current date should be displayed this time:

    dotnet run
    What is the current date?
    

    Assistant response:

    Assistant > Today's date is October 4, 2024. Time flies like an arrow; fruit flies like a banana! 
    
  8. Congratulations you are now using your first Semantic Kernel plugin! Next, we are going to leverage another plugin that will provide a StockService. This plugin is included within the Core.Utilities project. Review the file named StockDataPlugin.cs from Core.Utilities\Plugins which includes 2 functions, one to retrieve the stock price for the current date and another one for a specific date:

    using Core.Utilities.Services;
    using Core.Utilities.Models;
    using Core.Utilities.Extensions;
    
    using Microsoft.SemanticKernel;
    using System.ComponentModel;
    
    public class StockDataPlugin(StocksService stockService)
    {
        private readonly StocksService _stockService = stockService;
    
        [KernelFunction, Description("Gets stock price")]
        public async Task<string> GetStockPrice(string symbol)
        {
            string tabularData = (await _stockService.GetStockDailyOpenClose(symbol)).FormatStockData();
            return tabularData;
        }
    
        [KernelFunction, Description("Gets stock price for a given date")]
        public async Task<string> GetStockPriceForDate(string symbol, DateTime date)
        {
            string tabularData = (await _stockService.GetStockDailyOpenClose(symbol, date)).FormatStockData();
            return tabularData;
        }
    
    }
    
  9. Next, locate TODO: Step 5 in Program.cs and add import required for StockService:

    using Core.Utilities.Services;
    
  10. Next locate TODO: Step 6 and provide the following line to register the new StockDataPlugin:

    HttpClient httpClient = new();
    StockDataPlugin stockDataPlugin = new(new StocksService(httpClient));
    kernel.Plugins.AddFromObject(stockDataPlugin);
    
  11. Next run program and ask stock pricing information:

    dotnet run
    What is MSFT price?
    

    Assistant response:

    Assistant > Hold onto your calculators! The price of MSFT is currently $417.63. 
    Looks like it's trying to outshine the stars!