LangChain之Output parsers
Output parsers将LLM输出的文本,转换为structured data
CommaSeparatedListOutputParser
解析结果为List,提示词如下:
def get_format_instructions(self) -> str:
return (
"Your response should be a list of comma separated values, "
"eg: `foo, bar, baz`"
)
解析方法如下:
def parse(self, text: str) -> List[str]:
"""Parse the output of an LLM call."""
return text.strip().split(", ")
DatetimeOutputParser
解析结果为日期时间
output_parser = DatetimeOutputParser()
print(output_parser.get_format_instructions())
上面代码输出的提示词
Write a datetime string that matches the
following pattern: "%Y-%m-%dT%H:%M:%S.%fZ". Examples: 477-06-08T11:31:35.756750Z, 245-12-26T14:36:39.117625Z, 711-05-08T07:41:23.815247Z
EnumOutputParser
解析结果为枚举类型,且枚举类型只支持str
提示词如下:
def get_format_instructions(self) -> str:
return f"Select one of the following options: {
', '.join(self._valid_values)}"
示例代码:
class Colors(Enum):
RED