输出解析器
注意
此处的信息指的是解析器,它们接收模型的文本输出并尝试将其解析为更结构化的表示形式。 越来越多的模型支持函数(或工具)调用,这可以自动处理此类任务。 建议使用函数/工具调用而非输出解析。 相关文档请参见 此处。
Output parser 负责接收模型的输出,并将其转换为更适合下游任务的格式。
当您使用大语言模型(LLM)生成结构化数据,或需要对聊天模型和大语言模型的输出进行规范化时,此功能非常有用。
LangChain 拥有多种不同类型的输出解析器。以下是 LangChain 支持的输出解析器列表。下表包含各种信息:
- 名称: 输出解析器的名称
- 支持流式传输: 输出解析器是否支持流式传输。
- 是否有格式指令: 输出解析器是否包含格式指令。通常可用,除非 (a) 所需模式未在提示词中指定,而是在其他参数中指定(例如 OpenAI 函数调用),或 (b) 当 OutputParser 包装了另一个 OutputParser 时。
- 调用大语言模型: 此输出解析器本身是否调用了大语言模型。这通常仅由尝试修正格式错误输出的输出解析器执行。
- 输入类型: 预期的输入类型。大多数输出解析器同时支持字符串和消息,但某些(如 OpenAI Functions)需要具有特定 kwargs 的消息。
- 输出类型: 解析器返回的对象的输出类型。
- 描述: 我们对这个输出解析器的评论以及何时使用它。
| 名称 | 支持流式传输 | 具有格式说明 | 调用大语言模型 | 输入类型 | 输出类型 | 描述 |
|---|---|---|---|---|---|---|
| Str | ✅ | str | Message | String | Parses texts from message objects. Useful for handling variable formats of message content (e.g., extracting text from content blocks). | ||
| JSON | ✅ | ✅ | str | Message | JSON object | Returns 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. | |
| XML | ✅ | ✅ | str | Message | dict | Returns a dictionary of tags. Use when XML output is needed. Use with models that are good at writing XML (like Anthropic's). | |
| CSV | ✅ | ✅ | str | Message | List[str] | Returns a list of comma separated values. | |
| OutputFixing | ✅ | str | Message | Wraps 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. | |||
| RetryWithError | ✅ | str | Message | Wraps 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. | |||
| Pydantic | ✅ | str | Message | pydantic.BaseModel | Takes a user defined Pydantic model and returns data in that format. | ||
| YAML | ✅ | str | Message | pydantic.BaseModel | Takes a user defined Pydantic model and returns data in that format. Uses YAML to encode it. | ||
| PandasDataFrame | ✅ | str | Message | dict | Useful for doing operations with pandas DataFrames. | ||
| Enum | ✅ | str | Message | Enum | Parses response into one of the provided enum values. | ||
| Datetime | ✅ | str | Message | datetime.datetime | Parses response into a datetime string. | ||
| Structured | ✅ | str | Message | Dict[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. |
关于如何使用输出解析器,请参阅此处相关的操操作指南。