You can still use LangChain Chain callbacks with AmazonComprehendModerationChain via the callbacks parameter. Example: from langchain.callbacks.stdout import StdOutCallbackHandler comp_moderation_with_config = AmazonComprehendModerationChain(verbose=True, callbacks=[StdOutCallbackHandler()])from langchain_experimental.comprehend_moderation import BaseModerationCallbackHandler# Define callback handlers by subclassing BaseModerationCallbackHandlerclass MyModCallback(BaseModerationCallbackHandler):        async def on_after_pii(self, output_beacon, unique_id):        import json        moderation_type = output_beacon['moderation_type']        chain_id = output_beacon['moderation_chain_id']        with open(f'output-{moderation_type}-{chain_id}.json', 'w') as file:            data = { 'beacon_data': output_beacon, 'unique_id': unique_id }            json.dump(data, file)        '''    async def on_after_toxicity(self, output_beacon, unique_id):        pass        async def on_after_intent(self, output_beacon, unique_id):        pass    '''    my_callback = MyModCallback()moderation_config = {         "filters": [                 BaseModerationFilters.PII,                 BaseModerationFilters.TOXICITY        ],        "pii":{                 "action": BaseModerationActions.STOP,                 "threshold":0.5,                 "labels":["SSN"],                 "mask_character": "X"         },        "toxicity":{                 "action": BaseModerationActions.STOP,                 "threshold":0.5         }}comp_moderation_with_config = AmazonComprehendModerationChain(        moderation_config=moderation_config, # specify the configuration        client=comprehend_client,            # optionally pass the Boto3 Client        unique_id='john.doe@email.com',      # A unique ID        moderation_callback=my_callback,     # BaseModerationCallbackHandler        verbose=True)from langchain.prompts import PromptTemplatefrom langchain.chains import LLMChainfrom
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. ->: You can still use LangChain Chain callbacks with AmazonComprehendModerationChain via the callbacks parameter. Example: from langchain.callbacks.stdout import StdOutCallbackHandler comp_moderation_with_config = AmazonComprehendModerationChain(verbose=True, callbacks=[StdOutCallbackHandler()])from langchain_experimental.comprehend_moderation import BaseModerationCallbackHandler# Define callback handlers by subclassing BaseModerationCallbackHandlerclass MyModCallback(BaseModerationCallbackHandler):        async def on_after_pii(self, output_beacon, unique_id):        import json        moderation_type = output_beacon['moderation_type']        chain_id = output_beacon['moderation_chain_id']        with open(f'output-{moderation_type}-{chain_id}.json', 'w') as file:            data = { 'beacon_data': output_beacon, 'unique_id': unique_id }            json.dump(data, file)        '''    async def on_after_toxicity(self, output_beacon, unique_id):        pass        async def on_after_intent(self, output_beacon, unique_id):        pass    '''    my_callback = MyModCallback()moderation_config = {         "filters": [                 BaseModerationFilters.PII,                 BaseModerationFilters.TOXICITY        ],        "pii":{                 "action": BaseModerationActions.STOP,                 "threshold":0.5,                 "labels":["SSN"],                 "mask_character": "X"         },        "toxicity":{                 "action": BaseModerationActions.STOP,                 "threshold":0.5         }}comp_moderation_with_config = AmazonComprehendModerationChain(        moderation_config=moderation_config, # specify the configuration        client=comprehend_client,            # optionally pass the Boto3 Client        unique_id='john.doe@email.com',      # A unique ID        moderation_callback=my_callback,     # BaseModerationCallbackHandler        verbose=True)from langchain.prompts import PromptTemplatefrom langchain.chains import LLMChainfrom