Skip to content

Hacky workaround to get token usage from chat completions api response #1565

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

Closed
singhk97 opened this issue Jun 20, 2023 · 2 comments · Fixed by #2657
Closed

Hacky workaround to get token usage from chat completions api response #1565

singhk97 opened this issue Jun 20, 2023 · 2 comments · Fixed by #2657
Assignees
Labels
.NET Issue or Pull requests regarding .NET code

Comments

@singhk97
Copy link

I am trying to get the token usage data from the response of a call to the OpenAI's chat completion api but it doesn't seem possible. Here are the details:

IChatCompletion.GetChatCompletionsAsync returns a list of IChatResult objects. It represents the response from OpenAI's chat completion API. The issue is that this interface does not have the ModelResult property which contains token usage data. Hence, there's no way to get the token usage data when using the chat completion api. In contrast, the ITextCompletion.GetCompletionsAsync returns a list of ITextResult objects which do contain the ModelResult property.

Looking deeper into the code, it looks like the concrete object type of IChatResult which is the internal class ChatResult has the ModelResult property. So then I was able to cast the IChatResult to a ITextResult and access that property:

Screenshot 2023-06-19 174511

There should be a simple fix for this.

@craigomatic
Copy link
Contributor

@RogerBarreto any thoughts here?

@RogerBarreto
Copy link
Member

@singhk97, Addressing the well spotted problem.

github-merge-queue bot pushed a commit that referenced this issue Aug 30, 2023
…2657)

### Motivation and Context

`IChatResult` interface misses the ModelResult abstraction

Resolves #1565
Closes #1565

### Description

Added missing abstraction.

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
@github-project-automation github-project-automation bot moved this to Sprint: Done in Semantic Kernel Aug 30, 2023
SOE-YoungS pushed a commit to SOE-YoungS/semantic-kernel that referenced this issue Nov 1, 2023
…icrosoft#2657)

### Motivation and Context

`IChatResult` interface misses the ModelResult abstraction

Resolves microsoft#1565
Closes microsoft#1565

### Description

Added missing abstraction.

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
.NET Issue or Pull requests regarding .NET code
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants