Skip to content

Commit

Permalink
.Net: Stream response should not skip white space (microsoft#2922)
Browse files Browse the repository at this point in the history
### Motivation and Context
The current Azure OpenAI streaming implementation skips white space (ie.
space character token ID 220). All non-empty tokens should be streamed,
including space characters. This is rare as most tokens are prefixed
with a space by default, but still an issue in some cases (such as
formatted markdown, code, etc).

Fixes microsoft#2921 

### Description
Modify the streaming logic to check for null or empty instead of null,
empty and white space.

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

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

Co-authored-by: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com>
  • Loading branch information
anthonypuppo and RogerBarreto committed Sep 22, 2023
1 parent 810627a commit 4d7dfe3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async IAsyncEnumerable<ChatMessageBase> GetStreamingChatMessageAsync([Enu
{
await foreach (var message in this._choice.GetMessageStreaming(cancellationToken))
{
if (!string.IsNullOrWhiteSpace(message.Content))
if (message.Content is { Length: > 0 })
{
yield return new SKChatMessage(message);
}
Expand All @@ -65,9 +65,9 @@ public async IAsyncEnumerable<string> GetCompletionStreamingAsync([EnumeratorCan
{
await foreach (var result in this.GetStreamingChatMessageAsync(cancellationToken).ConfigureAwait(false))
{
if (!string.IsNullOrWhiteSpace(result.Content))
if (result.Content is string content and { Length: > 0 })
{
yield return result.Content;
yield return content;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public async IAsyncEnumerable<ChatMessageBase> GetStreamingChatMessageAsync([Enu
{
var message = await this.GetChatMessageAsync(cancellationToken).ConfigureAwait(false);

if (!string.IsNullOrWhiteSpace(message.Content))
if (message.Content is { Length: > 0 })
{
yield return message;
}
Expand All @@ -54,9 +54,9 @@ public async IAsyncEnumerable<string> GetCompletionStreamingAsync([EnumeratorCan
{
await foreach (var result in this.GetStreamingChatMessageAsync(cancellationToken))
{
if (!string.IsNullOrWhiteSpace(result.Content))
if (result.Content is string content and { Length: > 0 })
{
yield return result.Content;
yield return content;
}
}
}
Expand Down

0 comments on commit 4d7dfe3

Please sign in to comment.