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

feat: HuggingFaceAPIChatGenerator add token usage data #8375

Merged
merged 4 commits into from
Sep 23, 2024

Conversation

vblagoje
Copy link
Member

@vblagoje vblagoje commented Sep 17, 2024

Why:

Adds token usage metadata to responses from HuggingFaceAPIChatGenerator. usage dictionary response meta field has the following two keys prompt_tokens and completion_tokens matching OpenAI format in token counting.

This feature, i.e. OpenAI token usage format compatibility, aside from chat generators interchangeability benefits, is needed for full support of Langfuse GENERATION token usage renderings in traces. See https://github.com/deepset-ai/haystack-private/issues/82 for more details.

What:

  • Added a new usage meta field with the keys prompt_tokens and completion_tokens to HuggingFaceAPIChatGenerator.
  • Modified the streaming and non-streaming response generation code to include the new usage information in the message metadata.

How can it be used:

# Get the usage information from the first reply
usage_info = response["replies"][0].meta["usage"]
prompt_tokens_used = usage_info["prompt_tokens"]
completion_tokens_used = usage_info["completion_tokens"]

print(f"Prompt tokens used: {prompt_tokens_used}, Completion tokens used: {completion_tokens_used}")

How did you test it:

  • Updated unit tests are included to ensure the presence of the usage meta field and its contained prompt_tokens and completion_tokens keys in the reply messages.
  • Tests cover non-streaming and streaming scenarios, ensuring compatibility with different API types.

Notes for the reviewer:

@vblagoje vblagoje added the ignore-for-release-notes PRs with this flag won't be included in the release notes. label Sep 17, 2024
@coveralls
Copy link
Collaborator

coveralls commented Sep 17, 2024

Pull Request Test Coverage Report for Build 10957361582

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 11 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.09%) to 90.358%

Files with Coverage Reduction New Missed Lines %
components/classifiers/zero_shot_document_classifier.py 3 91.07%
utils/filters.py 3 96.91%
components/evaluators/llm_evaluator.py 5 95.08%
Totals Coverage Status
Change from base Build 10899485113: 0.09%
Covered Lines: 7338
Relevant Lines: 8121

💛 - Coveralls

@vblagoje vblagoje removed the ignore-for-release-notes PRs with this flag won't be included in the release notes. label Sep 17, 2024
@github-actions github-actions bot added the type:documentation Improvements on the docs label Sep 17, 2024
@vblagoje vblagoje changed the title WIP: Ensure HuggingFaceAPIChatGenerator has token usage data feat: Add HuggingFaceAPI(Chat)Generator token 'usage' data Sep 17, 2024
@vblagoje vblagoje changed the title feat: Add HuggingFaceAPI(Chat)Generator token 'usage' data feat: Add HuggingFaceAPI(Chat)Generator token usage data Sep 17, 2024
@vblagoje vblagoje marked this pull request as ready for review September 17, 2024 14:11
@vblagoje vblagoje requested review from a team as code owners September 17, 2024 14:11
@vblagoje vblagoje requested review from shadeMe and removed request for a team September 17, 2024 14:11
@vblagoje
Copy link
Member Author

Please don't review this PR unless you are @anakin87

@vblagoje vblagoje removed request for a team and shadeMe September 17, 2024 14:18
@vblagoje vblagoje changed the title feat: Add HuggingFaceAPI(Chat)Generator token usage data feat: HuggingFaceAPIChatGenerator add token usage data Sep 20, 2024
Copy link
Member

@anakin87 anakin87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@vblagoje vblagoje merged commit 09b9574 into main Sep 23, 2024
18 checks passed
@vblagoje vblagoje deleted the hf_api_token_counting branch September 23, 2024 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic:tests type:documentation Improvements on the docs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants