An optional dictionary to configure guardrails for Bedrock.
This field guardrails consists of two keys: 'guardrailId' and
'guardrailVersion', which should be strings, but are initialized to None.
It's used to determine if specific guardrails are enabled and properly set.
guardrails: Optional[Mapping[str, Any]] = {'trace': None, 'guardrailIdentifier': None, 'guardrailVersion': None}Type:
Optional[Mapping[str, str]]: A mapping with 'guardrailId' and 'guardrailVersion' keys.
Example:
llm = BedrockLLM(model_id="<model_id>", client=<bedrock_client>,
model_kwargs={},
guardrails={
"guardrailId": "<guardrail_id>",
"guardrailVersion": "<guardrail_version>"})
To enable tracing for guardrails, set the 'trace' key to True and pass a callback handler to the 'run_manager' parameter of the 'generate', '_call' methods.
Example:
llm = BedrockLLM(model_id="<model_id>", client=<bedrock_client>,
model_kwargs={},
guardrails={
"guardrailId": "<guardrail_id>",
"guardrailVersion": "<guardrail_version>",
"trace": True},
callbacks=[BedrockAsyncCallbackHandler()])
https://python.langchain.com/docs/concepts/callbacks/ for more information on callback handlers.
class BedrockAsyncCallbackHandler(AsyncCallbackHandler): async def on_llm_error( self, error: BaseException, **kwargs: Any, ) -> Any: reason = kwargs.get("reason") if reason == "GUARDRAIL_INTERVENED": ...Logic to handle guardrail intervention...