Skip to content

Files

Latest commit

922a8cc · Aug 27, 2024

History

History
65 lines (47 loc) · 2.37 KB

03 Add Plugin (Function Call).md

File metadata and controls

65 lines (47 loc) · 2.37 KB

Exercise - Add Plugin (Function Call)

This project demonstrates how to extend OpenAI's Semantic Kernel functionalities by incorporating additional services like plugins.

  1. Open the project you have created in 02 Add Chat History in VS Code or Visual Studio.

  2. Add the following using statment to the top of Program.cs file.

    using Microsoft.SemanticKernel.Connectors.OpenAI;
  3. Define a class named DemographicInfo with GetAge function at the bottom of Program.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
            };
        }
    }
  4. 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>();
  5. 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.
  6. Modify the while loop by adding the following code

    var response = await chatService.GetChatMessageContentAsync(chatHistory, settings, kernel);// Get chat response based on chat history
    
  7. 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 shown

    Q: My name is Alice. How old am I?
    Alice, you are 25 years old.
    Q: 

Complete sample project

View the completed sample in the 03 Add Plugin (Function Call) project.

Next unit: Exercise - Add Logging

Continue