This project demonstrates how to extend OpenAI's Semantic Kernel functionalities by incorporating additional services like plugins.
-
Open the project you have created in 02 Add Chat History in VS Code or Visual Studio.
-
Add the following using statment to the top of
Program.cs
file.using Microsoft.SemanticKernel.Connectors.OpenAI;
-
Define a class named
DemographicInfo
withGetAge
function at the bottom ofProgram.cs
. The GetAge function is also decorated with KernelFunction to mark it as a kernel function.class DemographicInfo { [KernelFunction] public int GetAge(string name) { return name switch { "Alice" => 25, "Bob" => 30, _ => 0 }; } }
-
Add the following next to kernel initialization to import a plugin to the kernel from type
DemographicInfo
.// Import the DemographicInfo class to the kernel, so it can be used in the chat completion service. // this plugin could be from other options such as functions, prompts directory, etc. kernel.ImportPluginFromType<DemographicInfo>();
-
Add the following function calling behavior setting. The setting is configured to call the method
GetAge
automatically when the user requests the age of the person with the provided name.var settings = new OpenAIPromptExecutionSettings() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };// Set the settings for the chat completion service.
-
Modify the while loop by adding the following code
var response = await chatService.GetChatMessageContentAsync(chatHistory, settings, kernel);// Get chat response based on chat history
-
Run the application by entering
dotnet run
into the terminal. Experiment with a user prompt "My name is Alice. How old Am I?" ,you will get something similar output as shownQ: My name is Alice. How old am I? Alice, you are 25 years old. Q:
View the completed sample in the 03 Add Plugin (Function Call) project.