Skip to content

Commit

Permalink
fix: don't rely on usage data for token count
Browse files Browse the repository at this point in the history
  • Loading branch information
sjmonson committed Sep 27, 2024
1 parent a924bc5 commit 53d5370
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions plugins/openai_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,10 @@ def streaming_request_http(self, query: dict, user_id: int, test_end_time: float
message = self._process_resp(resps[-1]['data'])
# If stream_options.include_usage == True then the final
# message contains only token stats
expected_output_tokens = None
if message and not message.get("choices") and message.get('usage'):
result.output_tokens = deepget(message, "usage", "completion_tokens")
# We want to count output tokens ourselves, but we can check our work with usage data.
expected_output_tokens = deepget(message, "usage", "completion_tokens")
result.input_tokens = deepget(message, "usage", "prompt_tokens")
# We don't want to record this message
resps.pop()
Expand Down Expand Up @@ -320,9 +322,9 @@ def streaming_request_http(self, query: dict, user_id: int, test_end_time: float
logger.warning("Input token count not found in response, using dataset input_tokens")
result.input_tokens = query.get("input_tokens")

if not result.output_tokens:
logger.warning("Output token count not found in response, length of token list")
result.output_tokens = len(tokens)
result.output_tokens = len(tokens)
if expected_output_tokens and result.output_tokens != expected_output_tokens:
logger.warning(f"Received {result.output_tokens} tokens but expected {expected_output_tokens} tokens")

# If test duration timeout didn't happen before the last token is received,
# total tokens before the timeout will be equal to the total tokens in the response.
Expand Down

0 comments on commit 53d5370

Please sign in to comment.