[docs]classRegexParser(BaseOutputParser[Dict[str,str]]):"""Parse the output of an LLM call using a regex."""@classmethoddefis_lc_serializable(cls)->bool:returnTrueregex:str"""The regex to use to parse the output."""output_keys:List[str]"""The keys to use for the output."""default_output_key:Optional[str]=None"""The default key to use for the output."""@propertydef_type(self)->str:"""Return the type key."""return"regex_parser"
[docs]defparse(self,text:str)->Dict[str,str]:"""Parse the output of an LLM call."""match=re.search(self.regex,text)ifmatch:return{key:match.group(i+1)fori,keyinenumerate(self.output_keys)}else:ifself.default_output_keyisNone:raiseValueError(f"Could not parse output: {text}")else:return{key:textifkey==self.default_output_keyelse""forkeyinself.output_keys}