check keys before using (#475)

This commit is contained in:
Harrison Chase 2022-12-29 22:16:35 -05:00 committed by GitHub
parent 12aa43469f
commit 3e41ab7bff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -142,11 +142,12 @@ class BaseOpenAI(BaseLLM, BaseModel):
token_usage = {} token_usage = {}
# Get the token usage from the response. # Get the token usage from the response.
# Includes prompt, completion, and total tokens used. # Includes prompt, completion, and total tokens used.
_keys = ["completion_tokens", "prompt_tokens", "total_tokens"] _keys = {"completion_tokens", "prompt_tokens", "total_tokens"}
for _prompts in sub_prompts: for _prompts in sub_prompts:
response = self.client.create(prompt=_prompts, **params) response = self.client.create(prompt=_prompts, **params)
choices.extend(response["choices"]) choices.extend(response["choices"])
for _key in _keys: _keys_to_use = _keys.intersection(response["usage"])
for _key in _keys_to_use:
if _key not in token_usage: if _key not in token_usage:
token_usage[_key] = response["usage"][_key] token_usage[_key] = response["usage"][_key]
else: else: