I'm trying out Microsoft's Semantic Kernel for chat completion in a Blazor app. I added a service that uses EF Core:
public interface ISearchService
{
Task<string?> GetCustomerAsync(string surname);
}
This is the interface. The implementation is straightforward. I then created a plugin class:
[Description("Representats customer data")]
public class SearchPlugin(ISearchService searchService)
{
[KernelFunction("GetCustomer"), Description("Get customer details by their surname")]
[return: Description("Customer details")]
public async Task<string?> GetCustomer([Description("Customer surname")] string surname)
=> await searchService.GetCustomerAsync(surname);
}
I register the service as Scoped:
services.AddScoped<ISearchService, SearchService>();
And register the kernel:
services.AddSemanticKernel(configuration)
.Plugins.AddFromType<SearchPlugin>();
Where AddSemanticKernel is an extension method:
public static IKernelBuilder AddSemanticKernel(this IServiceCollection services, IConfiguration configuration)
{
var modelId = "llama3.2:3b";
var endpoint = new Uri("http://localhost:11424");
services.AddOllamaChatCompletion(modelId, endpoint);
return services.AddKernel();
}
However, when I start the app, I get the following exception:
System.InvalidOperationException: Cannot resolve scoped service 'MyApp.Services.Interfaces.ISearchService' from root provider.
What am I doing wrong?
kernel.Plugins.AddFromType<TesterTools>(pluginName: "Tester", serviceProvider: sp)services.AddKernel().AddOllamaChatCompletion()