Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support nested dicts #26414

Merged
merged 6 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion libs/community/langchain_community/chat_models/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,22 @@ def _convert_delta_to_message_chunk(
return default_class(content=content) # type: ignore[call-arg]


def _update_token_usage(
overall_token_usage: Union[int, dict], new_usage: Union[int, dict]
):
# Token usage is either ints or dictionaries
# `reasoning_tokens` is nested inside `completion_tokens_details`
if isinstance(new_usage, int):
return new_usage
elif isinstance(new_usage, dict):
return {
k: _update_token_usage(overall_token_usage.get(k, 0), v)
for k, v in new_usage.items()
}
else:
raise TypeError(f"Unexpected type for token usage: {type(new_usage)}")


@deprecated(
since="0.0.10", removal="1.0", alternative_import="langchain_openai.ChatOpenAI"
)
Expand Down Expand Up @@ -374,7 +390,9 @@ def _combine_llm_outputs(self, llm_outputs: List[Optional[dict]]) -> dict:
if token_usage is not None:
for k, v in token_usage.items():
if k in overall_token_usage:
overall_token_usage[k] += v
overall_token_usage[k] = _update_token_usage(
overall_token_usage[k], v
)
else:
overall_token_usage[k] = v
if system_fingerprint is None:
Expand Down
20 changes: 19 additions & 1 deletion libs/partners/openai/langchain_openai/chat_models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,22 @@ def _convert_chunk_to_generation_chunk(
return generation_chunk


def _update_token_usage(
overall_token_usage: Union[int, dict], new_usage: Union[int, dict]
):
# Token usage is either ints or dictionaries
# `reasoning_tokens` is nested inside `completion_tokens_details`
if isinstance(new_usage, int):
return new_usage
elif isinstance(new_usage, dict):
return {
k: _update_token_usage(overall_token_usage.get(k, 0), v)
for k, v in new_usage.items()
Comment on lines +357 to +358

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest inspecting the value to ensure it's an integer. The OpenAI API may add additional fields within usage that are not integers in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Believe this actually handles what you're asking for! Recursively calls the same function, the adding only happens if it's an int. Otherwise hits the warning "else" case at the bottom

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed with @efriis , The best way to handle this by checking the instance types programmatically for int and dict. And the else case will then take care of warnings.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks! I don't think you need to warn in the else condition though — we may add non-integer fields in usage in the future, and that's not a breaking change or unexpected.

}
else:
raise TypeError(f"Unexpected type for token usage: {type(new_usage)}")


class _FunctionCall(TypedDict):
name: str

Expand Down Expand Up @@ -561,7 +577,9 @@ def _combine_llm_outputs(self, llm_outputs: List[Optional[dict]]) -> dict:
if token_usage is not None:
for k, v in token_usage.items():
if k in overall_token_usage:
overall_token_usage[k] += v
overall_token_usage[k] = _update_token_usage(
overall_token_usage[k], v
)
else:
overall_token_usage[k] = v
if system_fingerprint is None:
Expand Down
Loading