DatetimeOutputParser
Author: Donghak Lee
Proofread : Two-Jay
This is a part of LangChain Open Tutorial
Overview
The DatetimeOutputParser is an output parser that generates structured outputs in the form of datetime objects.
By converting the outputs of LLMs into datetime objects, it enables more systematic and consistent processing of date and time data, making it useful for data processing and analysis.
This tutorial demonstrates how to use the DatetimeOutputParser to:
Set up and initialize the parser for
datetimegenerationConvert a
datetimeobject to a string
Table of Contents
References
Environment Setup
Set up the environment. You may refer to Environment Setup for more details.
[Note]
langchain-opentutorialis a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.You can checkout the
langchain-opentutorialfor more details.
You can alternatively set OPENAI_API_KEY in .env file and load it.
[Note] This is not necessary if you've already set OPENAI_API_KEY in previous steps.
Using the DatetimeOutputParser
DatetimeOutputParserIf you need to generate output in the form of a date or time, the DatetimeOutputParser from LangChain simplifies the process.
The format of the DatetimeOutputParser can be specified by referring to the table below.
%Y
4-digit year
2024
%y
2-digit year
24
%m
2-digit month
07
%d
2-digit day
04
%H
24-hour format hour
14
%I
12-hour format hour
02
%p
AM or PM
PM
%M
2-digit minute
45
%S
2-digit second
08
%f
Microsecond (6 digits)
000123
%z
UTC offset
+0900
%Z
Timezone name
KST
%a
Abbreviated weekday
Thu
%A
Full weekday name
Thursday
%b
Abbreviated month
Jul
%B
Full month name
July
%c
Full date and time
Thu Jul 4 14:45:08 2024
%x
Full date
07/04/24
%X
Full time
14:45:08
Using DatetimeOutputParser in astream
DatetimeOutputParser in astreamRefer to the user-defined generator to create a generator function.
Let's create a simple example that converts astream output to datetime objects using a generator function.
Last updated