September 17th, 2024

New Function Calling Available in .NET for Semantic Kernel

Sergey Menshykh
Principal Software Engineer

We are happy to announce release of the new function-calling capabilities available in Semantic Kernel v1.20 (.NET).

The new capabilities incorporates the best parts of the existing function-calling, such as ease of use, and improves on it by making it more extensible and reusable.

Function calling allows Plugin descriptions to be passed into a model for dynamic planning to occur.  The AI model will create a step-by-step plan based on the users ask and based on the function descriptions that are passed into it.  Allowing for the right functions to be called, in the right order and used together to solve the users ask or question.  All of the calls are orchestrated by Semantic Kernel.

Let’s see this in action:

using Microsoft.SemanticKernel;

IKernelBuilder builder = Kernel.CreateBuilder();

// Register OpenAI connector
builder.AddOpenAIChatCompletion("<model-id>", "<api-key>");

// Register a few plugins
builder.Plugins.AddFromType<WeatherForecastUtils>();  
builder.Plugins.AddFromType<DateTimeUtils>();  

Kernel kernel = builder.Build();

// Configure function calling to:
// 1. Send all the functions from the plugins to the OpenAI model to decide which one(s) to choose.
// 2. Invoke them automatically by Semantic Kernel.
PromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }; 

await kernel.InvokePromptAsync("What is the likely color of the sky in Boston?", new(settings));

One function-calling capabilities for all SK connectors

Today, Semantic Kernel has a few sets of classes for function calling. One set is for the AzureOpenAI and OpenAI AI connectors. Another set is for the Gemini AI connector, and the third one is for the Mistral connector.

These sets of function-calling classes are almost identical, with only minor differences – each set references internal code specific to the AI connector. This makes it impossible to reuse the same function-calling classes for other AI connectors. As a result, each new AI connector requires a new set of function-calling classes to be created.

The new function-calling addresses this problem by being designed to be connector/service agnostic. It resides within Semantic Kernel abstractions and is intended for use by all AI connectors that work with function-calling capable AI models. This design makes the new function-calling model reusable and supports polymorphic scenarios, allowing the same instance of the function-calling configuration and behavior to be used by different AI connectors.

Note

As of today, the new function-calling model is used by the Azure OpenAI and OpenAI AI connectors only, with plans to add support for other AI connectors in future Semantic Kernel releases. See the Supported AI Connectors section to see the list of AI connectors supporting the new model.

Function-calling capabilities of modern AI models and Extensibility

The existing function-calling model in Semantic Kernel was created soon after the function-calling capabilities for AI models were introduced and has not changed since then. Meanwhile, there were a few small features added on the AI model side that have not found their way into the existing function-calling model.

The new capabilities incorporates some of these features from day one, such as support for multiple required functions and exposing the “None” function choice behavior, leaving others like parallel function calling and concurrent function invocation to future Semantic Kernel releases. The new model is designed to be extensible and support new AI model features as they may be introduced, regardless of whether they are applicable to all function-calling capable models or specific ones.

Prompt template configuration support

The new function-calling subsystem can be configured not only through code but also via prompt template configuration, as demonstrated by the example:

using Microsoft.SemanticKernel;

IKernelBuilder builder = Kernel.CreateBuilder();

// Register OpenAI connector
builder.AddOpenAIChatCompletion("<model-id>", "<api-key>");

// Register a few plugins
builder.Plugins.AddFromType<WeatherForecastUtils>();  
builder.Plugins.AddFromType<DateTimeUtils>();  

string promptTemplateConfig = """
    template_format: semantic-kernel
    template: What is the likely color of the sky in Boston?
    execution_settings:
      default:
        function_choice_behavior:
          type: auto
    """;

KernelFunction promptFunction = KernelFunctionYaml.FromPromptYaml(promptTemplateConfig);

Kernel kernel = builder.Build();

await kernel.InvokeAsync(promptFunction);

 

Next steps

Now that you know about the new function-calling capabilities, you can get more detailed information about it in the function choice behaviors article to start using it. If you are using existing function-calling and you would like to update to the latest technology – the migration guide can help you migrate your code to the new capabilities. For more hands-on samples, you can check the function calling samples

Please reach out if you have any questions or feedback through our Semantic Kernel GitHub Discussion Channel. We look forward to hearing from you! We would also love your support, if you’ve enjoyed using Semantic Kernel, give us a star on GitHub.

Author

Sergey Menshykh
Principal Software Engineer

0 comments