[docs]classBooleanOutputParser(BaseOutputParser[bool]):"""Parse the output of an LLM call to a boolean."""true_val:str="YES""""The string value that should be parsed as True."""false_val:str="NO""""The string value that should be parsed as False."""
[docs]defparse(self,text:str)->bool:"""Parse the output of an LLM call to a boolean. Args: text: output of a language model Returns: boolean """regexp=rf"\b({self.true_val}|{self.false_val})\b"truthy={val.upper()forvalinre.findall(regexp,text,flags=re.IGNORECASE|re.MULTILINE)}ifself.true_val.upper()intruthy:ifself.false_val.upper()intruthy:raiseValueError(f"Ambiguous response. Both {self.true_val} and {self.false_val} "f"in received: {text}.")returnTrueelifself.false_val.upper()intruthy:ifself.true_val.upper()intruthy:raiseValueError(f"Ambiguous response. Both {self.true_val} and {self.false_val} "f"in received: {text}.")returnFalseraiseValueError(f"BooleanOutputParser expected output value to include either "f"{self.true_val} or {self.false_val}. Received {text}.")
@propertydef_type(self)->str:"""Snake-case string identifier for an output parser type."""return"boolean_output_parser"