Replies: 1 comment
-
Tagging @markwallace-microsoft |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I am programming a chatbot in .NET and trying to obtain the token usage for each AI execution.
I am using the OpenAIChatCompletion service.
I have seen that I should use the Metadata property and the Usage field.
The Metadata property is an 'IReadOnlyDictionary<string, object?>?.'.
When deserializing the 'Usage' field, I have often seen the object being cast to 'CompletionUsage'. However, this type is only available in the Semantic Kernel Python version and not in the newer .NET versions.
After doing some checks, I discovered that the 'Usage' object needs to be cast to 'OpenAI.Chat.ChatTokenUsage' (as shown in the image below).
My question is whether there is something like 'CompletionUsage' in .NET that I am missing or if I should simply use the native type from the OpenAI package. It works fine, but I find it strange that this type seems to be available only in the Python version.
I have seen many examples where the AI return object is a 'FunctionResult' because 'InvokeAsync' is used, but very few where the AI return object is a 'ChatMessageContent.' I am not sure if this is the "correct" way to obtain the token count.
Execution prints:
OpenAI.Chat.ChatTokenUsage
Token usage. Input tokens: 848; Output tokens: 22
CompletionUsage class in Python:
https://learn.microsoft.com/en-us/python/api/semantic-kernel/semantic_kernel.connectors.ai.completion_usage.completionusage?view=semantic-kernel-python
OS: Windows
IDE: Visual Studio 2022
Language: C#
Source: NuGet package version 1.32.0
Beta Was this translation helpful? Give feedback.
All reactions