forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Azure OpenAI: fix streaming completions deserialization (Azure#34486)
* Azure OpenAI: fix streaming completions deserialization * Refresh release date/changelog for retry after yesterday's pipeline infra issues
- Loading branch information
1 parent
f461c4b
commit 6a557a6
Showing
6 changed files
with
457 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
sdk/openai/Azure.AI.OpenAI/src/Custom/CompletionsUsage.Serialization.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
sdk/openai/Azure.AI.OpenAI/src/Generated/CompletionsUsage.Serialization.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.