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 OpenAI - Add Usage information for Streaming #6826

Closed
RogerBarreto opened this issue Jun 19, 2024 · 2 comments · Fixed by #9022
Closed

.Net OpenAI - Add Usage information for Streaming #6826

RogerBarreto opened this issue Jun 19, 2024 · 2 comments · Fixed by #9022
Assignees
Labels
ai connector Anything related to AI connectors Ignite .NET Issue or Pull requests regarding .NET code

Comments

@RogerBarreto
Copy link
Member

Recently OpenAI added a stream_options.include_usage = true parameter that when set provide one last chunk with the Usage information, this can be set on by default in our connector for text streaming APIs.

https://platform.openai.com/docs/api-reference/chat/create#chat-create-stream_options

Last chunk info example

{
    "id": "chatcmpl-9bs3D3THDTOtsjYcokah40Ub",
    "object": "chat.completion.chunk",
    "created": 1718812935,
    "model": "gpt-4o-2024-05-13",
    "system_fingerprint": "fp_f4e629d0a5",
    "choices": [],
    "usage": {
        "prompt_tokens": 13,
        "completion_tokens": 7,
        "total_tokens": 20
    }
}
@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code triage ai connector Anything related to AI connectors and removed triage labels Jun 19, 2024
@arynaq
Copy link

arynaq commented Jun 25, 2024

Ah I was going crazy, wondering why this was not available

var response = chat_completion.GetStreamingChatMessageContentsAsync(history, cancellationToken: cancellationToken);
        await foreach (var chunk in response)
        {
            
            if (chunk.Metadata != null)
            {
                var as_json = JsonSerializer.Serialize(chunk.Metadata, new JsonSerializerOptions { WriteIndented = true });
                Console.WriteLine(as_json);
            }
            if (chunk.Content == null)
            {
                continue;
            }

            if (chunk.Content.Length > 0)
            {
                yield return new ServerSideEvent("message", chunk.Content, Guid.NewGuid().ToString(), "1000");
            }
        }

Expected the usage metadata to be there, but this is not implemented yet, in the meantime is there any way we can get this in a streaming mode? It is quite important to track usage on a per-user model in our app.

@AdaTheDev
Copy link

@arynaq Don't believe there is - I ended up created a temporary wrapper connector around the official OpenAI connector, that then calculates the token usage manually using Tiktoken.

github-merge-queue bot pushed a commit that referenced this issue Sep 30, 2024
### Motivation and Context

Currently we have metadata for non-streaming results, and to be
consistent, the same should be available for streaming for the final
chunk.

- Resolves #6826
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 Ignite .NET Issue or Pull requests regarding .NET code
Projects
Status: Sprint: Done
Development

Successfully merging a pull request may close this issue.

5 participants