Skip to content

Commit

Permalink
fix: OpenAI API handle optional usage field
Browse files Browse the repository at this point in the history
  • Loading branch information
drewrip committed Jun 7, 2024
1 parent 3d76871 commit 366c7ce
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions plugins/openai_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,15 @@ def streaming_request_http(self, query: dict, user_id: int, test_end_time: float

# Last token comes with finish_reason set.
if message.get("choices", [])[0].get("finish_reason", None):
result.output_tokens = message["usage"]["completion_tokens"]
result.input_tokens = message["usage"]["prompt_tokens"]
result.stop_reason = message["choices"][0]["finish_reason"]
if message.get("usage"):
result.output_tokens = message["usage"]["completion_tokens"]
result.input_tokens = message["usage"]["prompt_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.
if not result.output_tokens_before_timeout:
result.output_tokens_before_timeout = result.output_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.
if not result.output_tokens_before_timeout:
result.output_tokens_before_timeout = result.output_tokens
result.stop_reason = message["choices"][0]["finish_reason"]

except KeyError:
logging.exception("KeyError, unexpected response format in line: %s", line)
Expand Down

0 comments on commit 366c7ce

Please sign in to comment.