Web app instantiation #7090
-
I'm creating a web app chat client using c# asp.net MVC. What's the most efficient way of instantiating and instance of the kernel per chat session? I doubt instantiating a new instance per web request is a great idea? Also, each chat session will a differing set of plugins and kernel.Data. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @idlemind !
Kernel is a lightweight object, so it should be safe to initialize a new instance as Scoped (per request) or Transient (per object). There is an extension method to easily add Kernel to your application, and we add it as Transient by default: In your application you can add it to DI container like this: var builder = WebApplication.CreateBuilder(args);
builder.Services.AddKernel();
builder.Services.AddOpenAIChatCompletion(openAIOptions.ChatModelId, openAIOptions.ApiKey);
If you will initialize Kernel instance per request or per object, it will have 0 imported plugins, so in each place you can import different set of plugins and have different |
Beta Was this translation helpful? Give feedback.
Hi @idlemind !
Kernel is a lightweight object, so it should be safe to initialize a new instance as Scoped (per request) or Transient (per object).
There is an extension method to easily add Kernel to your application, and we add it as Transient by default:
semantic-kernel/dotnet/src/SemanticKernel.Abstractions/Services/KernelServiceCollectionExtensions.cs
Lines 18 to 29 in e77ffdf