microsoft / semantic-kernel

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

.Net: 'AzureOpenAIChatCompletionService' not registered. #6995

Closed luisindex closed 3 months ago

luisindex commented 3 months ago

*Sorry for my English:

I updated the semantic Kernel version to the version 1.15.0 and was calling a method in a minimal net 8 API and with the following code:

---In classCustom.cs //var _kernel = Kernel.CreateBuilder() // .AddAzureOpenAIChatCompletion(deploymentName: _azureChat.Value.ChatDeploymentName, endpoint: _azureChat.Value.Endpoint, apiKey: _azureChat.Value.ApiKey) // .Build(); var query = ""; //AzureOpenAIChatCompletionService chat = new(deploymentName: _azureChat.Value.ChatDeploymentName, apiKey: _azureChat.Value.ApiKey, endpoint: _azureChat.Value.Endpoint); var chat = _kernel.GetRequiredService<AzureOpenAIChatCompletionService>();

but I get the following error: Microsoft.SemanticKernel.KernelException: Service of type 'Microsoft.SemanticKernel.Connectors.OpenAI.AzureOpenAIChatCompletionService' not registered.

I have tried to register it in the program.cs as follows: in program.cs builder.Services.AddKernel().AddAzureOpenAIChatCompletion(deploymentName: AzureChat.ApiKey, endpoint: AzureChat.Endpoint, apiKey: AzureChat.ApiKey); builder.Services.AddTransient(sp => { Kernel kernel = new(sp); return kernel; }); builder.Services.AddTransient<IKernelBuilder>(serviceProvider => { var kernelBuilder = Kernel.CreateBuilder(); kernelBuilder.Services.AddAzureOpenAIChatCompletion(deploymentName: AzureChat.ApiKey, endpoint: AzureChat.Endpoint, apiKey: AzureChat.ApiKey); return kernelBuilder; });

also within the same method but it doesn't work for me. As you see in the initial part(Comment code). I tried different ways!!! help me please.

Csproj `

`

Additionally, I use the sdk in the same project

<PackageReference Include="Azure.AI.OpenAI" />

matthewbolanos commented 3 months ago

It looks like you are trying to add the chat completion service to the kernel (instead of your app's service collection), which is probably why DI is not working.

This sample, shows you how to add the chat completion service to the root service collection: https://github.com/microsoft/semantic-kernel/blob/ccc6ed04671ddbbc5aad893db0f2febbd0217679/dotnet/samples/Demos/HomeAutomation/Program.cs#L41