-
//DI
var kernelBuilder = services.AddKernel();
services.AddSingleton<IEmailService, EmailService>();
kernelBuilder.Plugins.AddFromType<EmailPlugin>()
// plugin
public class EmailPlugin
{
public EmailPlugin(IEmailService emailService){}
}
// send email service
public interface IEmailService{}
public class EmailService : IEmailService{}
// sk service
public SKService(Kernel kernel)
{
_kernel = kernel;
}
public async Task<string[]> EmailSender()
{
// _kernel = _kernel.Clone();
// _kernel.Plugins.Clear();
// _kernel.Plugins.AddFromType<EmailPlugin>("EmailPlugin");
....
var result = _chatCompletionService.GetStreamingChatMessageContentsAsync(
chatMessages,
executionSettings: openAIPromptExecutionSettings,
kernel: _kernel);
...
} The above code throws an error after uncommenting the code. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Here's a potential workaround: _kernel = _kernel.Clone();
_kernel.Plugins.Clear();
_kernel.Plugins.AddFromType<EmailPlugin>("EmailPlugin", _kernel.Services); The |
Beta Was this translation helpful? Give feedback.
-
@runceel After looking into it, I found that not only can we write Thanks again for your helpful response! |
Beta Was this translation helpful? Give feedback.
Here's a potential workaround:
The
Plugins
property is essentially a collection ofKernelPlugin
instances. The key is to explicitly pass the services when adding the plugin. By passing_kernel.Services
as the second parameter, the plugin is instantiated using the services registered in the cloned kernel's DI container, enabling theEmailPlugin
to properly resolve itsIEmailService
dependency.