def_contains_pii(self,prompt_value:str,config:Any=None)->str:""" Checks for Personally Identifiable Information (PII) labels above a specified threshold. Uses Amazon Comprehend Contains PII Entities API. See - https://docs.aws.amazon.com/comprehend/latest/APIReference/API_ContainsPiiEntities.html Args: prompt_value (str): The input text to be checked for PII labels. config (Dict[str, Any]): Configuration for PII check and actions. Returns: str: the original prompt Note: - The provided client should be initialized with valid AWS credentials. """pii_identified=self.client.contains_pii_entities(Text=prompt_value,LanguageCode="en")ifself.callbackandself.callback.pii_callback:self.moderation_beacon["moderation_input"]=prompt_valueself.moderation_beacon["moderation_output"]=pii_identifiedthreshold=config.get("threshold")pii_labels=config.get("labels")pii_found=Falseforentityinpii_identified["Labels"]:if(entity["Score"]>=thresholdandentity["Name"]inpii_labels)or(entity["Score"]>=thresholdandnotpii_labels):pii_found=Truebreakifself.callbackandself.callback.pii_callback:ifpii_found:self.moderation_beacon["moderation_status"]="LABELS_FOUND"asyncio.create_task(self.callback.on_after_pii(self.moderation_beacon,self.unique_id))ifpii_found:raiseModerationPiiErrorreturnprompt_valuedef_detect_pii(self,prompt_value:str,config:Optional[Dict[str,Any]])->str:""" Detects and handles Personally Identifiable Information (PII) entities in the given prompt text using Amazon Comprehend's detect_pii_entities API. The function provides options to redact or stop processing based on the identified PII entities and a provided configuration. Uses Amazon Comprehend Detect PII Entities API. Args: prompt_value (str): The input text to be checked for PII entities. config (Dict[str, Any]): A configuration specifying how to handle PII entities. Returns: str: The processed prompt text with redacted PII entities or raised exceptions. Raises: ValueError: If the prompt contains configured PII entities for stopping processing. Note: - If PII is not found in the prompt, the original prompt is returned. - The client should be initialized with valid AWS credentials. """pii_identified=self.client.detect_pii_entities(Text=prompt_value,LanguageCode="en")ifself.callbackandself.callback.pii_callback:self.moderation_beacon["moderation_input"]=prompt_valueself.moderation_beacon["moderation_output"]=pii_identifiedif(pii_identified["Entities"])==[]:ifself.callbackandself.callback.pii_callback:asyncio.create_task(self.callback.on_after_pii(self.moderation_beacon,self.unique_id))returnprompt_valuepii_found=Falseifnotconfigandpii_identified["Entities"]:forentityinpii_identified["Entities"]:ifentity["Score"]>=0.5:pii_found=Truebreakifself.callbackandself.callback.pii_callback:ifpii_found:self.moderation_beacon["moderation_status"]="LABELS_FOUND"asyncio.create_task(self.callback.on_after_pii(self.moderation_beacon,self.unique_id))ifpii_found:raiseModerationPiiErrorelse:threshold=config.get("threshold")# type: ignorepii_labels=config.get("labels")# type: ignoremask_marker=config.get("mask_character")# type: ignorepii_found=Falseforentityinpii_identified["Entities"]:if(pii_labelsandentity["Type"]inpii_labelsandentity["Score"]>=threshold)or(notpii_labelsandentity["Score"]>=threshold):pii_found=Truechar_offset_begin=entity["BeginOffset"]char_offset_end=entity["EndOffset"]mask_length=char_offset_end-char_offset_begin+1masked_part=mask_marker*mask_lengthprompt_value=(prompt_value[:char_offset_begin]+masked_part+prompt_value[char_offset_end+1:])ifself.callbackandself.callback.pii_callback:ifpii_found:self.moderation_beacon["moderation_status"]="LABELS_FOUND"asyncio.create_task(self.callback.on_after_pii(self.moderation_beacon,self.unique_id))returnprompt_value