OutputParser

🔹 2. OutputParser

| 功能 | 让 LLM 输出符合你定义的结构(如 JSON、数值、列表等) |
| 常见类型 | StructuredOutputParser, PydanticOutputParser, RegexParser, CommaSeparatedListOutputParser |

示例(使用 JSON 格式):

python 复制编辑 from langchain.output_parsers import StructuredOutputParser from langchain.output_parsers.format_instructions import get_format_instructions parser = StructuredOutputParser.from_names_and_descriptions({ "name": "The person's name", "age": "The person's age" }) format_instructions = parser.get_format_instructions() prompt = PromptTemplate( template="Extract name and age:\n\n{text}\n\n{format_instructions}", input_variables=["text"], partial_variables={"format_instructions": format_instructions} )