microsoft / semantic-kernel

Integrate cutting-edge LLM technology quickly and easily into your apps
https://aka.ms/semantic-kernel
MIT License
22.15k stars 3.31k forks source link

.Net: Bug: Using keyed services with `IServiceCollection.AddKernel` gives `Required service not registered` when kernel.Invoke is used with no serviceId provided. #7620

Open RogerBarreto opened 4 months ago

RogerBarreto commented 4 months ago

Describe the bug When using IServiceCollection.AddKernel and all connectors are key based (using ServiceId), is not possible to call kernel.InvokePromptAsync without specifying a serviceId in the configuration.

To Reproduce

image

using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;

#pragma warning disable SKEXP0010 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
#pragma warning disable IDE0211 // Convert to 'Program.Main' style program

var service = new ServiceCollection();
service.AddKernel()
    .AddOpenAIChatCompletion(serviceId: "service1", apiKey: null, modelId: "1", endpoint: new Uri("https://localhost"))
    .AddOpenAIChatCompletion(serviceId: "service2", apiKey: null, modelId: "2", endpoint: new Uri("https://localhost"))
    .AddOpenAIChatCompletion(serviceId: "service3", apiKey: " key ", modelId: "gpt-4o");

var provider = service.BuildServiceProvider();

var kernel = provider.GetRequiredService<Kernel>();

await kernel.InvokePromptAsync("test");

Expected behavior The last registered service in the builder that matches any of the ITextGenerationService or IChatCompletionService interfaces is used similar to var kernel = builder.Build().

RogerBarreto commented 1 month ago

Currently this cannot be until AnyKey is backported to .Net 8 or we move to .Net 9. Detailed below:

The recommended approach for this scenario is to build the Kernel from KernelBuilder and it will work for keyed services.