langchain.chains import LLMChainfrom langchain.llms.fake import FakeListLLMtemplate = """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'])moderation_config and moderation execution order‚ÄãIf AmazonComprehendModerationChain is not initialized with any moderation_config then the default action is STOP and the default order of moderation check is as follows.AmazonComprehendModerationChain‚îÇ‚îî‚îÄ‚îÄCheck PII with Stop Action    ‚îú‚îÄ‚îÄ Callback (if available)    ‚îú‚îÄ‚îÄ Label Found ‚ü∂ [Error Stop]    ‚îî‚îÄ‚îÄ No Label Found         ‚îî‚îÄ‚îÄCheck Toxicity with Stop Action            ‚îú‚îÄ‚îÄ Callback (if available)            ‚îú‚îÄ‚îÄ Label Found ‚ü∂ [Error Stop]            ‚îî‚îÄ‚îÄ No Label Found                ‚îî‚îÄ‚îÄCheck Intent with Stop Action                    ‚îú‚îÄ‚îÄ Callback (if available)                    ‚îú‚îÄ‚îÄ Label Found ‚ü∂ [Error Stop]                    ‚îî‚îÄ‚îÄ No Label Found                        ‚îî‚îÄ‚îÄ Return PromptIf any of the checks raises an exception then the subsequent checks will not be performed. If a callback is provided in this case, then
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. ->: langchain.chains import LLMChainfrom langchain.llms.fake import FakeListLLMtemplate = """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'])moderation_config and moderation execution order‚ÄãIf AmazonComprehendModerationChain is not initialized with any moderation_config then the default action is STOP and the default order of moderation check is as follows.AmazonComprehendModerationChain‚îÇ‚îî‚îÄ‚îÄCheck PII with Stop Action    ‚îú‚îÄ‚îÄ Callback (if available)    ‚îú‚îÄ‚îÄ Label Found ‚ü∂ [Error Stop]    ‚îî‚îÄ‚îÄ No Label Found         ‚îî‚îÄ‚îÄCheck Toxicity with Stop Action            ‚îú‚îÄ‚îÄ Callback (if available)            ‚îú‚îÄ‚îÄ Label Found ‚ü∂ [Error Stop]            ‚îî‚îÄ‚îÄ No Label Found                ‚îî‚îÄ‚îÄCheck Intent with Stop Action                    ‚îú‚îÄ‚îÄ Callback (if available)                    ‚îú‚îÄ‚îÄ Label Found ‚ü∂ [Error Stop]                    ‚îî‚îÄ‚îÄ No Label Found                        ‚îî‚îÄ‚îÄ Return PromptIf any of the checks raises an exception then the subsequent checks will not be performed. If a callback is provided in this case, then