deepset-ai / haystack

AI orchestration framework to build customizable, production-ready LLM applications. Connect components (models, vector DBs, file converters) to pipelines or agents that can interact with your data. With advanced retrieval methods, it's best suited for building RAG, question answering, semantic search or conversational agent chatbots.
https://haystack.deepset.ai
Apache License 2.0
17.72k stars 1.92k forks source link

feat: HuggingFaceAPIChatGenerator add token `usage` data #8375

Closed vblagoje closed 1 month ago

vblagoje commented 2 months ago

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:

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:

Notes for the reviewer:

coveralls commented 2 months ago

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


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%
<!-- Total: 11 -->
Totals Coverage Status
Change from base Build 10899485113: 0.09%
Covered Lines: 7338
Relevant Lines: 8121

💛 - Coveralls
vblagoje commented 2 months ago

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