fix(main.py): fix stream_chunk_builder usage calc

Closes https://github.com/BerriAI/litellm/issues/4496
This commit is contained in:
Krrish Dholakia 2024-07-06 14:52:38 -07:00
parent e34ba00a51
commit 27e9f96380
3 changed files with 57 additions and 12 deletions

View file

@ -5022,10 +5022,9 @@ def stream_chunk_builder(
for chunk in chunks:
if "usage" in chunk:
if "prompt_tokens" in chunk["usage"]:
prompt_tokens += chunk["usage"].get("prompt_tokens", 0) or 0
prompt_tokens = chunk["usage"].get("prompt_tokens", 0) or 0
if "completion_tokens" in chunk["usage"]:
completion_tokens += chunk["usage"].get("completion_tokens", 0) or 0
completion_tokens = chunk["usage"].get("completion_tokens", 0) or 0
try:
response["usage"]["prompt_tokens"] = prompt_tokens or token_counter(
model=model, messages=messages