importrandomfromdatetimeimportdatetime,timedeltafromtypingimportListfromlangchain_core.exceptionsimportOutputParserExceptionfromlangchain_core.output_parsersimportBaseOutputParserfromlangchain_core.utilsimportcomma_listdef_generate_random_datetime_strings(pattern:str,n:int=3,start_date:datetime=datetime(1,1,1),end_date:datetime=datetime.now()+timedelta(days=3650),)->List[str]:"""Generates n random datetime strings conforming to the given pattern within the specified date range. Pattern should be a string containing the desired format codes. start_date and end_date should be datetime objects representing the start and end of the date range. """examples=[]delta=end_date-start_dateforiinrange(n):random_delta=random.uniform(0,delta.total_seconds())dt=start_date+timedelta(seconds=random_delta)date_string=dt.strftime(pattern)examples.append(date_string)returnexamples
[docs]classDatetimeOutputParser(BaseOutputParser[datetime]):"""Parse the output of an LLM call to a datetime."""format:str="%Y-%m-%dT%H:%M:%S.%fZ""""The string value that used as the datetime format."""
[docs]defget_format_instructions(self)->str:examples=comma_list(_generate_random_datetime_strings(self.format))return(f"Write a datetime string that matches the "f"following pattern: '{self.format}'.\n\n"f"Examples: {examples}\n\n"f"Return ONLY this string, no other words!")
[docs]defparse(self,response:str)->datetime:try:returndatetime.strptime(response.strip(),self.format)exceptValueErrorase:raiseOutputParserException(f"Could not parse datetime string: {response}")frome