Skip to main content
在 GitHub 上打开

输出解析器

注意

此处的信息是指从模型中获取文本输出的解析器尝试将其解析为更结构化的表示形式。 越来越多的模型支持函数(或工具)调用,它会自动处理这个问题。 建议使用函数/工具调用而不是输出解析。 请在此处查看相关文档。

Output parser负责获取模型的输出并将其转换为更适合下游任务的格式。 当您使用 LLM 生成结构化数据或规范化聊天模型和 LLM 的输出时,这很有用。

LangChain 有许多不同类型的输出解析器。这是 LangChain 支持的输出解析器列表。下表包含各种信息:

  • Name:输出解析器的名称
  • Supports Streaming:输出解析器是否支持流式处理。
  • Has Format Instructions(具有格式指令):输出解析器是否具有格式指令。这通常可用,除非 (a) 所需的架构未在提示符中指定,而是在其他参数(如 OpenAI 函数调用)中指定,或者 (b) 当 OutputParser 包装另一个 OutputParser 时。
  • 调用 LLM:此输出解析器本身是否调用 LLM。这通常仅由尝试更正格式错误输出的输出解析器完成。
  • Input Type(输入类型):预期的输入类型。大多数输出解析器同时处理字符串和消息,但有些(如 OpenAI Functions)需要带有特定 kwargs 的消息。
  • Output Type(输出类型):解析器返回的对象的输出类型。
  • 描述:我们对这个输出解析器以及何时使用它的评论。
名字支持流式处理有格式说明调用 LLM输入类型输出类型描述
Strstr | MessageStringParses texts from message objects. Useful for handling variable formats of message content (e.g., extracting text from content blocks).
JSONstr | MessageJSON objectReturns a JSON object as specified. You can specify a Pydantic model and it will return JSON for that model. Probably the most reliable output parser for getting structured data that does NOT use function calling.
XMLstr | MessagedictReturns a dictionary of tags. Use when XML output is needed. Use with models that are good at writing XML (like Anthropic's).
CSVstr | MessageList[str]Returns a list of comma separated values.
OutputFixingstr | MessageWraps another output parser. If that output parser errors, then this will pass the error message and the bad output to an LLM and ask it to fix the output.
RetryWithErrorstr | MessageWraps another output parser. If that output parser errors, then this will pass the original inputs, the bad output, and the error message to an LLM and ask it to fix it. Compared to OutputFixingParser, this one also sends the original instructions.
Pydanticstr | Messagepydantic.BaseModelTakes a user defined Pydantic model and returns data in that format.
YAMLstr | Messagepydantic.BaseModelTakes a user defined Pydantic model and returns data in that format. Uses YAML to encode it.
PandasDataFramestr | MessagedictUseful for doing operations with pandas DataFrames.
Enumstr | MessageEnumParses response into one of the provided enum values.
Datetimestr | Messagedatetime.datetimeParses response into a datetime string.
Structuredstr | MessageDict[str, str]An output parser that returns structured information. It is less powerful than other output parsers since it only allows for fields to be strings. This can be useful when you are working with smaller LLMs.

有关如何使用输出解析器的具体信息,请参阅此处的相关操作指南