moderation action is specified as STOP, the chain will raise one of the following exceptions-- `ModerationPiiError`, for PII checks- `ModerationToxicityError`, for Toxicity checks - `ModerationIntentionError` for Intent checksIn addition to the moderation configuration, the AmazonComprehendModerationChain can also be initialized with the following parametersunique_id [Optional] a string parameter. This parameter can be used to pass any string value or ID. For example, in a chat application, you may want to keep track of abusive users, in this case, you can pass the user's username/email ID etc. This defaults to None.moderation_callback [Optional] the BaseModerationCallbackHandler will be called asynchronously (non-blocking to the chain). Callback functions are useful when you want to perform additional actions when the moderation functions are executed, for example logging into a database, or writing a log file. You can override three functions by subclassing BaseModerationCallbackHandler - on_after_pii(), on_after_toxicity(), and on_after_intent(). Note that all three functions must be async functions. These callback functions receive two arguments:moderation_beacon is a dictionary that will contain information about the moderation function, the full response from the Amazon Comprehend model, a unique chain id, the moderation status, and the input string which was validated. The dictionary is of the following schema-{     'moderation_chain_id': 'xxx-xxx-xxx', # Unique chain ID    'moderation_type': 'Toxicity' | 'PII' | 'Intent',     'moderation_status': 'LABELS_FOUND' | 'LABELS_NOT_FOUND',    'moderation_input': 'A sample SSN number looks like this 123-456-7890. Can you give me some more samples?',    'moderation_output': {...} #Full Amazon Comprehend PII, Toxicity, or Intent Model Output}unique_id if passed to the AmazonComprehendModerationChain NOTE: moderation_callback is different from LangChain Chain Callbacks. You can still use LangChain Chain callbacks with
This notebook shows how to use Amazon Comprehend to detect and handle Personally Identifiable Information (PII) and toxicity.
This notebook shows how to use Amazon Comprehend to detect and handle Personally Identifiable Information (PII) and toxicity. ->: moderation action is specified as STOP, the chain will raise one of the following exceptions-- `ModerationPiiError`, for PII checks- `ModerationToxicityError`, for Toxicity checks - `ModerationIntentionError` for Intent checksIn addition to the moderation configuration, the AmazonComprehendModerationChain can also be initialized with the following parametersunique_id [Optional] a string parameter. This parameter can be used to pass any string value or ID. For example, in a chat application, you may want to keep track of abusive users, in this case, you can pass the user's username/email ID etc. This defaults to None.moderation_callback [Optional] the BaseModerationCallbackHandler will be called asynchronously (non-blocking to the chain). Callback functions are useful when you want to perform additional actions when the moderation functions are executed, for example logging into a database, or writing a log file. You can override three functions by subclassing BaseModerationCallbackHandler - on_after_pii(), on_after_toxicity(), and on_after_intent(). Note that all three functions must be async functions. These callback functions receive two arguments:moderation_beacon is a dictionary that will contain information about the moderation function, the full response from the Amazon Comprehend model, a unique chain id, the moderation status, and the input string which was validated. The dictionary is of the following schema-{     'moderation_chain_id': 'xxx-xxx-xxx', # Unique chain ID    'moderation_type': 'Toxicity' | 'PII' | 'Intent',     'moderation_status': 'LABELS_FOUND' | 'LABELS_NOT_FOUND',    'moderation_input': 'A sample SSN number looks like this 123-456-7890. Can you give me some more samples?',    'moderation_output': {...} #Full Amazon Comprehend PII, Toxicity, or Intent Model Output}unique_id if passed to the AmazonComprehendModerationChain NOTE: moderation_callback is different from LangChain Chain Callbacks. You can still use LangChain Chain callbacks with