Using Semantic Kernel with multiple services

2024-09-09 Development Artur

The builder for the Semantic Kernel (SK) allows to add multiple services to the kernel. I was wondering how the SK knows which service to choose during the runtime. The answer is quite simple.

By default, the SK uses the OrderedAIServiceSelector class, which chooses the last registered service. So, in this example the SK would use the OpenAI service.

var builder = Kernel.CreateBuilder()
    .AddAzureOpenAIChatCompletion(
        ...,
        serviceId: "AzureOpenAIChat")
    .AddOpenAIChatCompletion(
        ...,
        serviceId: "OpenAIChat");

Each AddService method has the optional serviceId parameter, with which the services can be named and distinguished from each other. And the IAIServiceSelector interface allows us to create own service selectors based on the use-case. Here you can find an example.

For creating a fallback strategy, you can also use the IFunctionInvocationFilter interface to create own filters as shown in this example.