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.
-
Ensure all pre-requisites are met and installed (including updating the StockService
apiKey
value in theappSettings.json
file using the key from polygon.io). -
Switch to Lesson 3 directory:
-
Start by copying
appsettings.json
from Lesson 1: -
Run program and ask what the current date is:
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?
-
Notice it does not provide a specific answer. We can use a Semantic Kernel Plugin to be able to fix that.
-
In the
Plugins
directory fromCore.Utilities
directory review the file namedTimeInformationPlugin.cs
which has the following content: -
Next locate TODO: Step 1 in
Program.cs
and add the following import line: -
Next locate TODO: Step 2 in
Program.cs
and provide the following line to register theTimeInformationPlugin
: -
Next locate TODO: Step 3 and add the following line to be able to auto invoke kernel functions:
-
Next locate TODO: Step 4 and add the following parameters:
-
Re-run the program and ask what the current date is. The current date should be displayed this time:
Assistant response:
-
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 theCore.Utilities
project. Review the file namedStockDataPlugin.cs
fromCore.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; } }
-
Next, locate TODO: Step 5 in
Program.cs
and add import required forStockService
: -
Next locate TODO: Step 6 and provide the following line to register the new
StockDataPlugin
: -
Next run program and ask stock pricing information:
Assistant response: