Skip to content

Commit

Permalink
.Net: Add Async Support for ConverseStreamResponse to Avoid Thread Bl…
Browse files Browse the repository at this point in the history
…ocking (#10159)

### Motivation and Context

This change replaces the use of `response.Stream.AsEnumerable()` with
`await foreach` for async streaming in the Amazon Bedrock SDK. The
previous method blocked the calling thread, leading to potential thread
exhaustion in high-concurrency scenarios. This update aligns with best
practices for asynchronous operations in .NET.

### Description

- Replaced blocking foreach with `await foreach` for streaming
`ConverseStreamResponse` chunks asynchronously.
- Upgraded Amazon Bedrock runtime and core SDKs to support this
functionality.

### Contribution Checklist

- [Y] The code builds clean without any errors or warnings
- [Y] 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
- [Y] All unit tests pass, and I have added new tests where possible
- [Y] I didn't break anyone 😄

Fixes #9519

---------

Co-authored-by: Adit Sheth <[email protected]>
  • Loading branch information
shethaadit and Adit Sheth authored Jan 16, 2025
1 parent 39934f5 commit b34b16e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions dotnet/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="AWSSDK.BedrockRuntime" Version="3.7.411.5" />
<PackageVersion Include="AWSSDK.BedrockRuntime" Version="4.0.0-preview.5" />
<PackageVersion Include="AWSSDK.Extensions.NETCore.Setup" Version="3.7.301" />
<PackageVersion Include="AWSSDK.Core" Version="3.7.400.64" />
<PackageVersion Include="AWSSDK.Core" Version="4.0.0-preview.5" />
<PackageVersion Include="Azure.AI.Inference" Version="1.0.0-beta.2" />
<PackageVersion Include="Dapr.Actors" Version="1.14.0" />
<PackageVersion Include="Dapr.Actors.AspNetCore" Version="1.14.0" />
Expand Down Expand Up @@ -165,4 +165,4 @@
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI.Cuda" Version="0.5.2" />
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI.DirectML" Version="0.5.2" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ internal async Task<IReadOnlyList<ChatMessageContent>> GenerateChatMessageAsync(
{
activityStatus = BedrockClientUtilities.ConvertHttpStatusCodeToActivityStatusCode(response.HttpStatusCode);
activity.SetStatus(activityStatus);
activity.SetPromptTokenUsage(response.Usage.InputTokens);
activity.SetCompletionTokenUsage(response.Usage.OutputTokens);
activity.SetPromptTokenUsage(response?.Usage?.InputTokens ?? default);
activity.SetCompletionTokenUsage(response?.Usage?.OutputTokens ?? default);
}
}
catch (Exception ex)
Expand All @@ -90,8 +90,8 @@ internal async Task<IReadOnlyList<ChatMessageContent>> GenerateChatMessageAsync(
{
activityStatus = BedrockClientUtilities.ConvertHttpStatusCodeToActivityStatusCode(response.HttpStatusCode);
activity.SetStatus(activityStatus);
activity.SetPromptTokenUsage(response.Usage.InputTokens);
activity.SetCompletionTokenUsage(response.Usage.OutputTokens);
activity.SetPromptTokenUsage(response?.Usage?.InputTokens ?? default);
activity.SetCompletionTokenUsage(response?.Usage?.OutputTokens ?? default);
}
else
{
Expand Down Expand Up @@ -191,7 +191,7 @@ internal async IAsyncEnumerable<StreamingChatMessageContent> StreamChatMessageAs
throw;
}
List<StreamingChatMessageContent>? streamedContents = activity is not null ? [] : null;
foreach (var chunk in response.Stream.AsEnumerable())
await foreach (var chunk in response.Stream.ConfigureAwait(false))
{
if (chunk is ContentBlockDeltaEvent deltaEvent)
{
Expand Down

0 comments on commit b34b16e

Please sign in to comment.