-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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)), | ||
) | ||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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) | ||
), | ||
|
@@ -417,7 +425,7 @@ def gen() -> ChatResponseGen: | |
"status": status, | ||
}, | ||
), | ||
raw=response, | ||
raw=dict_response, | ||
additional_kwargs=self._get_response_token_counts( | ||
dict(response) | ||
), | ||
|
@@ -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, | ||
|
@@ -468,7 +480,7 @@ async def achat( | |
"status": status, | ||
}, | ||
), | ||
raw=dict(response), | ||
raw=dict_response, | ||
additional_kwargs=self._get_response_token_counts(dict(response)), | ||
) | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.