Skip to content

Commit

Permalink
Azure OpenAI: fix streaming completions deserialization (#34486)
Browse files Browse the repository at this point in the history
* Azure OpenAI: fix streaming completions deserialization

* Refresh release date/changelog for retry after yesterday's pipeline infra issues
  • Loading branch information
trrwilson authored Feb 23, 2023
1 parent bbb988a commit b53d456
Show file tree
Hide file tree
Showing 6 changed files with 457 additions and 294 deletions.
11 changes: 3 additions & 8 deletions sdk/openai/Azure.AI.OpenAI/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
# Release History

## 1.0.0-beta.4 (Unreleased)
## 1.0.0-beta.4 (2023-02-23)

### Features Added

### Breaking Changes

### Bugs Fixed

### Other Changes
### Bugs fixed
- Addressed issues that sometimes caused `beta.3`'s new `GetStreamingCompletions` method to execute indefinitely

## 1.0.0-beta.3 (2023-02-17)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// <auto-generated/>

#nullable disable

using System.Text.Json;

namespace Azure.AI.OpenAI
{
public partial class CompletionsUsage
{
internal static CompletionsUsage DeserializeCompletionsUsage(JsonElement element)
{
if (element.ValueKind == JsonValueKind.Null)
{
// Handle deserialization of "usage":null in payloads
return null;
}

int completionTokens = default;
int promptTokens = default;
int totalTokens = default;
foreach (var property in element.EnumerateObject())
{
if (property.NameEquals("completion_tokens"u8))
{
completionTokens = property.Value.GetInt32();
continue;
}
if (property.NameEquals("prompt_tokens"u8))
{
promptTokens = property.Value.GetInt32();
continue;
}
if (property.NameEquals("total_tokens"u8))
{
totalTokens = property.Value.GetInt32();
continue;
}
}
return new CompletionsUsage(completionTokens, promptTokens, totalTokens);
}

/// <summary> Deserializes the model from a raw response. </summary>
/// <param name="response"> The response to deserialize the model from. </param>
internal static CompletionsUsage FromResponse(Response response)
{
using var document = JsonDocument.Parse(response.Content);
return DeserializeCompletionsUsage(document.RootElement);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public async IAsyncEnumerable<StreamingChoice> GetChoicesStreaming(
lock (_streamingChoicesLock)
{
doneWaiting = _streamingTaskComplete || i < _streamingChoices.Count;
isFinalIndex = _streamingTaskComplete && i == _streamingChoices.Count - 1;
isFinalIndex = _streamingTaskComplete && i >= _streamingChoices.Count - 1;
}

if (!doneWaiting)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b53d456

Please sign in to comment.