Skip to content

Fix BedrockConverse token count in usage to match OpenAI's format #18383

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ def chat(self, messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponse:
response
)

dict_response = dict(response)
# Add Bedrock's token count to usage dict to match OpenAI's format
dict_response["usage"] = self._get_response_token_counts(dict_response)

return ChatResponse(
message=ChatMessage(
role=MessageRole.ASSISTANT,
Expand All @@ -335,7 +339,7 @@ def chat(self, messages: Sequence[ChatMessage], **kwargs: Any) -> ChatResponse:
"status": status,
},
),
raw=dict(response),
raw=dict_response,
additional_kwargs=self._get_response_token_counts(dict(response)),
)

Expand Down Expand Up @@ -367,6 +371,10 @@ def stream_chat(
**all_kwargs,
)

dict_response = dict(response)
# Add Bedrock's token count to usage dict to match OpenAI's format
dict_response["usage"] = self._get_response_token_counts(dict_response)
Copy link
Collaborator

Choose a reason for hiding this comment

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

You'll actually need to get the usage on each chunk in the stream? I think? You might need to debug whats beining emitted in the chunk stream below to get the usage properly

Copy link
Collaborator

Choose a reason for hiding this comment

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

It can't know the complete usage until the stream is finished, for example


def gen() -> ChatResponseGen:
content = {}
role = MessageRole.ASSISTANT
Expand All @@ -392,7 +400,7 @@ def gen() -> ChatResponseGen:
},
),
delta=content_delta.get("text", ""),
raw=response,
raw=dict_response,
additional_kwargs=self._get_response_token_counts(
dict(response)
),
Expand All @@ -417,7 +425,7 @@ def gen() -> ChatResponseGen:
"status": status,
},
),
raw=response,
raw=dict_response,
additional_kwargs=self._get_response_token_counts(
dict(response)
),
Expand Down Expand Up @@ -458,6 +466,10 @@ async def achat(
response
)

dict_response = dict(response)
# Add Bedrock's token count to usage dict to match OpenAI's format
dict_response["usage"] = self._get_response_token_counts(dict_response)

return ChatResponse(
message=ChatMessage(
role=MessageRole.ASSISTANT,
Expand All @@ -468,7 +480,7 @@ async def achat(
"status": status,
},
),
raw=dict(response),
raw=dict_response,
additional_kwargs=self._get_response_token_counts(dict(response)),
)

Expand Down