Skip to main content
Open on GitHub

输出解析器

笔记

此处的信息指的是将模型的文本输出解析为更结构化表示的解析器。 越来越多的模型支持函数(或工具)调用,这可以自动处理此问题。 建议使用函数/工具调用,而不是输出解析。 有关这方面的文档,请点击 这里

Output parser 负责获取模型的输出并将其转换为更适合下游任务的格式。 当您使用大语言模型生成结构化数据,或对聊天模型和大语言模型的输出进行规范化时,此功能非常有用。

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

  • 名称: 输出解析器的名称
  • 支持流式传输: 输出解析器是否支持流式传输。
  • 是否包含格式说明: 输出解析器是否包含格式说明。通常情况下,这都是可用的,除非满足以下条件之一:(a) 所需的模式未在提示中指定,而是通过其他参数(如 OpenAI 函数调用)指定;或 (b) 输出解析器包装了另一个输出解析器。
  • 调用LLM: 此输出解析器是否自身调用一个LLM。这通常只由试图纠正格式错误输出的输出解析器执行。
  • 输入类型: 期望的输入类型。大多数输出解析器既支持字符串也支持消息,但有些(如OpenAI函数)需要带有特定关键字参数的消息。
  • 输出类型: 解析器返回的对象的输出类型。
  • 描述: 我们对这个输出解析器的评论以及何时使用它。
名称支持流式传输格式说明调用大语言模型输入类型输出类型描述
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.

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