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

Fixes type for latencyMs from numeric to list to prepare for response metadata #275

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion libs/aws/langchain_aws/chat_models/bedrock_converse.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,15 @@ def _messages_to_bedrock(
return bedrock_messages, bedrock_system


def _extract_response_metadata(response: Dict[str, Any]) -> Dict[str, Any]:
response_metadata = response
# response_metadata only supports string, list or dict
if "metrics" in response and "latencyMs" in response["metrics"]:
response_metadata["metrics"]["latencyMs"] = [response["metrics"]["latencyMs"]]

return response_metadata


def _parse_response(response: Dict[str, Any]) -> AIMessage:
anthropic_content = _bedrock_to_anthropic(
response.pop("output")["message"]["content"]
Expand All @@ -735,7 +744,7 @@ def _parse_response(response: Dict[str, Any]) -> AIMessage:
return AIMessage(
content=_str_if_single_text_block(anthropic_content), # type: ignore[arg-type]
usage_metadata=usage,
response_metadata=response,
response_metadata=_extract_response_metadata(response),
tool_calls=tool_calls,
)

Expand Down
22 changes: 22 additions & 0 deletions libs/aws/tests/unit_tests/chat_models/test_bedrock_converse.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
_bedrock_to_anthropic,
_camel_to_snake,
_camel_to_snake_keys,
_extract_response_metadata,
_messages_to_bedrock,
_snake_to_camel,
_snake_to_camel_keys,
Expand Down Expand Up @@ -415,3 +416,24 @@ def test_set_disable_streaming(
) -> None:
llm = ChatBedrockConverse(model=model_id, region_name="us-west-2")
assert llm.disable_streaming == disable_streaming


def test__extract_response_metadata() -> None:
response = {
"ResponseMetadata": {
"RequestId": "xxxxxx",
"HTTPStatusCode": 200,
"HTTPHeaders": {
"date": "Wed, 06 Nov 2024 23:28:31 GMT",
"content-type": "application/json",
"content-length": "212",
"connection": "keep-alive",
"x-amzn-requestid": "xxxxx",
},
"RetryAttempts": 0,
},
"stopReason": "end_turn",
"metrics": {"latencyMs": 191},
}
response_metadata = _extract_response_metadata(response)
assert response_metadata["metrics"]["latencyMs"] == [191]
Loading