Skip to content

Commit

Permalink
Fixes type for latencyMs from numeric to list to prepare for response…
Browse files Browse the repository at this point in the history
… metadata (#275)

Fixes #223
  • Loading branch information
3coins authored Nov 8, 2024
1 parent d25b78e commit 53cda7f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
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]

0 comments on commit 53cda7f

Please sign in to comment.