microsoft / semantic-kernel

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

.Net Processes: Create a sample showing how to use DI with Steps #9032

Open alliscode opened 1 week ago

alliscode commented 1 week ago
          I spent a couple hours trying to figure out how to get access to the primary IServiceProvider DI container from within a step but came up with nothing so far.

So, it would be helpful to show one practical example of interacting with a service external to the SK Process/Steps instead of having placeholder comments. The service doesn't actually need to do anything... SDK consumers just need to see how to get access to the service.

For example, in NewAccountStep instead of:

[KernelFunction(Functions.CreateNewAccount)]
    public async Task CreateNewAccountAsync(KernelProcessStepContext context, bool previousCheckSucceeded, NewCustomerForm customerDetails, List<ChatMessageContent> interactionTranscript, Kernel _kernel)
    {
        // Placeholder for a call to API to create new account for user
        var accountId = new Guid();
        ...
    }

It would be:

[KernelFunction(Functions.CreateNewAccount)]
    public async Task CreateNewAccountAsync(KernelProcessStepContext context, bool previousCheckSucceeded, NewCustomerForm customerDetails, List<ChatMessageContent> interactionTranscript, Kernel _kernel)
    {
        // var accountApiService = how do I resolve this service? As far as I can tell, there is no access to the DI container from within a step.
        var accountId = new Guid();
        ...
    }

Originally posted by @rjygraham in https://github.com/microsoft/semantic-kernel/issues/8990#issuecomment-2380356865

rjygraham commented 6 days ago

Per @alliscode, DI support for Process Steps is available when constructing the Kernel as illustrated in this sample:

https://github.com/microsoft/semantic-kernel/blob/cdb2d5930bf6e0cffec13ec393fb68040cabb940/dotnet/samples/Concepts/DependencyInjection/Kernel_Building.cs#L62

Given this redirect, I'm happy to create a sample showing DI within SK Process Steps. The SK Process documentation lists 3 business process examples in which SK Process Framework could be a fit. We already have a sample for Account Opening, so I could either update existing sample or start new using the Food Delivery, Support Ticket, or some other sample.

Any preferences?