.Net: Bug: When calling KernelProcessStepContext.EmitEventAsync to send events within a loop, the timing does not match the expected behavior.
Describe the bug
When calling KernelProcessStepContext.EmitEventAsync to send events within a loop, the timing does not match the expected behavior.
To Reproduce Steps to reproduce the behavior:
- Define two steps:
LlmStepandMessageStep. InLlmStep, during the iteration ofGetStreamingChatMessageContentsAsync, events are sent to notifyMessageStepto send messages to the UI.
Snippet of LlmStep:
public async Task<string> GetStreamingChatMessageAsync()
{
var chatCompletionService = GetCompletionService() ?? throw new Exception();
var promptExecutionSettings = new PromptExecutionSettings() { ExtensionData = _state.ExtensionData };
var chatHistory = await GetChatMessagesAsync();
var usage = new CompletionUsage();
_result = string.Empty;
await foreach (var item in chatCompletionService.GetStreamingChatMessageContentsAsync(chatHistory, promptExecutionSettings))
{
if (string.IsNullOrWhiteSpace(item.Content)) continue;
_result += item.Content;
await _processStepContext.EmitEventAsync(new KernelProcessEvent { Id = StepEvent.LlmChatMessageEvent, Data = item.Content });
usage.PromptTokens = (item.Metadata != null && item.Metadata.TryGetValue(nameof(ChatCompletionMetadata.UsagePromptTokens), out var usagePromptTokens)
&& int.TryParse(usagePromptTokens?.ToString(), out var promptTokens)) ? promptTokens : usage.PromptTokens + 1;
usage.CompletionTokens = (item.Metadata != null && item.Metadata.TryGetValue(nameof(ChatCompletionMetadata.UsageCompletionTokens), out var usageCompletionTokens)
&& int.TryParse(usageCompletionTokens?.ToString(), out var completionTokens)) ? completionTokens : usage.CompletionTokens + 1;
await _processStepContext.EmitEventAsync(StepEvent.LlmChatMessageEvent, new LlmChatMessageEventData(_id, item.Content));
};
AddChatHistory(AuthorRole.Assistant, _result);
await PostExecuteAsync(_cancellationToken);
return _result;
}
Snippet of MessageStep:
[KernelFunction(StepTypeConst.MessageOnChatMessage)]
public async ValueTask OnChatMessageAsync(LlmChatMessageEventData data)
{
var input = _state.Inputs.FirstOrDefault(x => x.IdEqual(data.Id));
if (input == null) return;
await _streamResponse.WriteDataAsync(new { Name = input.Name, Value = data.Content });
}
KernelProcess Build:
llmStep.OnEvent(StepEvent.ExecutedEvent)
.SendEventTo(new ProcessFunctionTargetBuilder(messageStep, functionName: StepTypeConst.Message, parameterName: "content"));
llmStep.OnEvent(StepEvent.LlmChatMessageEvent)
.SendEventTo(new ProcessFunctionTargetBuilder(messageStep, functionName: StepTypeConst.MessageOnChatMessage, parameterName: "data"));
Expected behavior
Each event in LlmStep is expected to be immediately sent to MessageStep. However, the actual behavior is that MessageStep only receives the events after the entire iteration of GetStreamingChatMessageContentsAsync in LlmStep is completed. I have reviewed the source code but could not find the reason. I would appreciate any insights on this issue.
Platform
- OS: Windows
- IDE: Visual Studio
- Language: C#
- Source: NuGet package version 1.3.0
Additional context
Using Microsoft.SemanticKernel.Process.LocalRuntime.
@nels-ai-source - thanks for reporting this. @alliscode - can you take a look?
Hi @nels-ai-source, what you are describing is the expected behavior. The process framework uses an algorithm called Pregel to execute the process in a series of super steps where all events emitted during super step n will be processes in super step n+1. This is useful for preventing deadlocks that can otherwise occur.
We are considering adding functionality that would allow a Step to directly invoke another Step, but we are still working through the details of this, more to come...
For your scenario, it may be better to remove the UI updating from the process itself and either use dependency injection to provide your steps with a UI manager type object, or consider something like SignalR?
This issue is stale because it has been open for 90 days with no activity.
This issue was closed because it has been inactive for 14 days since being marked as stale.