Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net: Bug: Token usage missing for Azure OpenAI Streaming Completion #9420

Closed
lukasz-appstream opened this issue Oct 24, 2024 · 8 comments · Fixed by #9651
Closed

.Net: Bug: Token usage missing for Azure OpenAI Streaming Completion #9420

lukasz-appstream opened this issue Oct 24, 2024 · 8 comments · Fixed by #9651
Assignees
Labels
ai connector Anything related to AI connectors blocked This issue is blocked from making progress .NET Issue or Pull requests regarding .NET code

Comments

@lukasz-appstream
Copy link

lukasz-appstream commented Oct 24, 2024

Bug summary
When I use Azure OpenAI I don't get Usage in metadata any of the chunks.

Technical Details
SemanticKernel: 1.24.1 (.NET)
Model: gpt-4o
Azure OpenAI Api version: 2024-10-01-PREVIEW

Bug Description
I checked any chunk and I haven't got Usage in any of them when using chat completion with streaming and from what I've read - Usage should be there for Azure OpenAI Service in this API.

@lukasz-appstream lukasz-appstream added the bug Something isn't working label Oct 24, 2024
@moonbox3
Copy link
Contributor

Hi @lukasz-appstream, thanks for filing the issue. For the streaming API, the usage is included in the last chunk. @markwallace-microsoft / @dmytrostruk for visibility, just in case.

@lukasz-appstream
Copy link
Author

Thanks @moonbox3 for the response! Yeah, I thought so, I updated the library, I updated the api version, I saved the last message and it still had metadata?["Usage"] == null... Does it actually work with Azure OpenAI Connector, or for now only for OpenAI Connector?

@JadynWong
Copy link
Contributor

JadynWong commented Oct 25, 2024

While the new api version supports stream token usage, the azure openai sdk still does not support it at this time. openai sdk has always supported it for raw openai, but SK has not implemented it yet.
Streaming token usage added to OpenAI in #9022

see #9005

https://github.com/Azure/azure-sdk-for-net/blob/main/sdk%2Fopenai%2FAzure.AI.OpenAI%2Fsrc%2FCustom%2FChat%2FAzureChatClient.cs#L82

@RogerBarreto
Copy link
Member

RogerBarreto commented Oct 25, 2024

@JadynWong thanks for adding the context to this issue.

This issue will be blocked until Azure SDK provides support for getting the usage.

For context we are tracking those issues in Azure SDK repo

@github-project-automation github-project-automation bot moved this from Sprint: Planned to Sprint: Done in Semantic Kernel Oct 25, 2024
@RogerBarreto RogerBarreto reopened this Oct 25, 2024
@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code triage labels Oct 25, 2024
@github-actions github-actions bot changed the title Bug: Token usage missing for Azure OpenAI Streaming Completion .Net: Bug: Token usage missing for Azure OpenAI Streaming Completion Oct 25, 2024
@RogerBarreto RogerBarreto added blocked This issue is blocked from making progress and removed triage labels Oct 25, 2024
@RogerBarreto RogerBarreto moved this from Sprint: Done to Bug in Semantic Kernel Oct 25, 2024
@RogerBarreto RogerBarreto added ai connector Anything related to AI connectors and removed bug Something isn't working labels Oct 25, 2024
@RogerBarreto RogerBarreto moved this from Bug to Backlog: Planned in Semantic Kernel Oct 25, 2024
@JadynWong
Copy link
Contributor

JadynWong commented Nov 6, 2024

@lukasz-appstream
Copy link
Author

@RogerBarreto I'm not sure I follow. So you are saying that this is now completed and we can get token usage for streaming?

@lukasz-appstream
Copy link
Author

@RogerBarreto nevermind ;) tested it, works, last chunk has the usage, thanks!

@DIDPT
Copy link

DIDPT commented Dec 18, 2024

@RogerBarreto

Is there a Timeline for a solution that the Token Usage can be reported when using Streaming and OnYourData?

/**
* As of 2024-09-01-preview, stream_options support for include_usage (which reports token usage while streaming)
* is conditionally supported:
* - When using On Your Data (non-null data_sources), stream_options is not considered valid
* - When using image input (any content part of "image" type), stream_options is not considered valid
* - Otherwise, stream_options can be defaulted to enabled per parity surface.
*/
private static void PostfixClearStreamOptions(IEnumerable messages, ref ChatCompletionOptions options)
{
if (AdditionalPropertyHelpers
.GetAdditionalListProperty(options?.SerializedAdditionalRawData, "data_sources")?.Count > 0
|| messages?.Any(
message => message?.Content?.Any(
contentPart => contentPart?.Kind == ChatMessageContentPartKind.Image) == true)
== true)
{
options ??= new();
options.StreamOptions = null;
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ai connector Anything related to AI connectors blocked This issue is blocked from making progress .NET Issue or Pull requests regarding .NET code
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

6 participants