langchain-ai / langchain-aws

Build LangChain Applications on AWS
MIT License
104 stars 81 forks source link

fix Bedrock Guardrails intervention detection #78

Open will-gao42 opened 4 months ago

will-gao42 commented 4 months ago

in llms/bedrock.py, line 682 the function _is_guardrails_intervention uses body.get(GUARDRAILS_BODY_KEY) == "GUARDRAIL_INTERVENED" to detect intervention, where GUARDRAILS_BODY_KEY is defined as "amazon-bedrock-guardrailAction" on line 35. However the payload from Bedrock actually uses 'amazon-bedrock-guardrailAction': 'INTERVENED' to indicate intervention.

Fix: line 35: GUARDRAILS_BODY_KEY=amazon-bedrock-guardrailAction line 682: body.get(GUARDRAILS_BODY_KEY) == "INTERVENED"

christopherwerner commented 2 months ago

I concur, but it's even worse. Currently the GUARDRAILS_BODY_KEY is using "..Assessment" instead of "...Action" whereas the body returned (using boto3/core v 1.34.162) returns ..."Action" as mentioned in the original post. The op is also correct about the value: it returns INTERVENED and the code still checks for GUARDRAILS_INTERVENED

Current: line 40: GUARDRAILS_BODY_KEY = "amazon-bedrock-guardrailAssessment" Should be: GUARDRAILS_BODY_KEY = "amazon-bedrock-guardrailAction"