Skip to content

Commit 3d21f17

Browse files
authored
.Net: Change ChatCompletionAgent to notify intermediate messages as soon as they are available. (#12575)
### Motivation and Context Addresses #12521 ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [ ] The code builds clean without any errors or warnings - [ ] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄
1 parent de7470c commit 3d21f17

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

dotnet/src/Agents/Core/ChatCompletionAgent.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ private async IAsyncEnumerable<StreamingChatMessageContent> InternalInvokeStream
391391

392392
this.Logger.LogAgentChatServiceInvokedStreamingAgent(nameof(InvokeAsync), this.Id, agentName, serviceType);
393393

394+
int messageIndex = messageCount;
394395
AuthorRole? role = null;
395396
StringBuilder builder = new();
396397
await foreach (StreamingChatMessageContent message in messages.ConfigureAwait(false))
@@ -401,18 +402,18 @@ private async IAsyncEnumerable<StreamingChatMessageContent> InternalInvokeStream
401402

402403
builder.Append(message.ToString());
403404

404-
yield return message;
405-
}
405+
// Capture mutated messages related function calling / tools
406+
for (; messageIndex < chat.Count; messageIndex++)
407+
{
408+
ChatMessageContent chatMessage = chat[messageIndex];
406409

407-
// Capture mutated messages related function calling / tools
408-
for (int messageIndex = messageCount; messageIndex < chat.Count; messageIndex++)
409-
{
410-
ChatMessageContent message = chat[messageIndex];
410+
chatMessage.AuthorName = this.Name;
411411

412-
message.AuthorName = this.Name;
412+
await onNewMessage(chatMessage).ConfigureAwait(false);
413+
history.Add(chatMessage);
414+
}
413415

414-
await onNewMessage(message).ConfigureAwait(false);
415-
history.Add(message);
416+
yield return message;
416417
}
417418

418419
// Do not duplicate terminated function result to history

0 commit comments

Comments
 (0)