- allows the prompt to pass through but masks detected PII in case of PII check. The default behavior is to run and redact all PII entities. If there is an entity specified in the labels field, then only those entities will go through the PII check and masked.BaseModerationActions.STOP - stops the prompt from passing through to the next step in case any PII, Toxicity, or incorrect Intent is detected. The action of BaseModerationActions.STOP will raise a Python Exception essentially stopping the chain in progress.Using the configuration in the previous cell will perform PII checks and will allow the prompt to pass through however it will mask any SSN numbers present in either the prompt or the LLM output.comp_moderation_with_config = AmazonComprehendModerationChain(    moderation_config=moderation_config, #specify the configuration    client=comprehend_client,            #optionally pass the Boto3 Client    verbose=True)template = """Question: {question}Answer:"""prompt = PromptTemplate(template=template, input_variables=["question"])responses = [    "Final Answer: A credit card number looks like 1289-2321-1123-2387. A fake SSN number looks like 323-22-9980. John Doe's phone number is (999)253-9876.",     "Final Answer: This is a really shitty way of constructing a birdhouse. This is fucking insane to think that any birds would actually create their motherfucking nests here."]llm = FakeListLLM(responses=responses)llm_chain = LLMChain(prompt=prompt, llm=llm)chain = (     prompt     | comp_moderation_with_config     | {llm_chain.input_keys[0]: lambda x: x['output'] }      | llm_chain     | { "input": lambda x: x['text'] }     | comp_moderation_with_config )try:    response = chain.invoke({"question": "A sample SSN number looks like this 123-456-7890. Can you give me some more samples?"})except Exception as e:    print(str(e))else:    print(response['output'])Unique ID, and Moderation Callbacks‚ÄãWhen Amazon Comprehend moderation action is specified as STOP, the chain
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. ->: - allows the prompt to pass through but masks detected PII in case of PII check. The default behavior is to run and redact all PII entities. If there is an entity specified in the labels field, then only those entities will go through the PII check and masked.BaseModerationActions.STOP - stops the prompt from passing through to the next step in case any PII, Toxicity, or incorrect Intent is detected. The action of BaseModerationActions.STOP will raise a Python Exception essentially stopping the chain in progress.Using the configuration in the previous cell will perform PII checks and will allow the prompt to pass through however it will mask any SSN numbers present in either the prompt or the LLM output.comp_moderation_with_config = AmazonComprehendModerationChain(    moderation_config=moderation_config, #specify the configuration    client=comprehend_client,            #optionally pass the Boto3 Client    verbose=True)template = """Question: {question}Answer:"""prompt = PromptTemplate(template=template, input_variables=["question"])responses = [    "Final Answer: A credit card number looks like 1289-2321-1123-2387. A fake SSN number looks like 323-22-9980. John Doe's phone number is (999)253-9876.",     "Final Answer: This is a really shitty way of constructing a birdhouse. This is fucking insane to think that any birds would actually create their motherfucking nests here."]llm = FakeListLLM(responses=responses)llm_chain = LLMChain(prompt=prompt, llm=llm)chain = (     prompt     | comp_moderation_with_config     | {llm_chain.input_keys[0]: lambda x: x['output'] }      | llm_chain     | { "input": lambda x: x['text'] }     | comp_moderation_with_config )try:    response = chain.invoke({"question": "A sample SSN number looks like this 123-456-7890. Can you give me some more samples?"})except Exception as e:    print(str(e))else:    print(response['output'])Unique ID, and Moderation Callbacks‚ÄãWhen Amazon Comprehend moderation action is specified as STOP, the chain