纯文本输出是有用的,但在某些情况下,我们需要 LLM 生成结构化输出,即以机器可读格式(如 JSON、XML 或 CSV)或甚至以编程语言(如 Python 或 JavaScript)生成的输出。当我们打算将该输出传递给其他代码时,这非常有用,使 LLM 可以在更大的应用程序中发挥作用。
调试步骤
import getpass
import osif "GOOGLE_API_KEY" not in os.environ:os.environ["GOOGLE_API_KEY"] = getpass.getpass("Enter your Google AI API key: ")
import os
import requestsos.environ['HTTP_PROXY'] = 'http://127.0.0.1:7890'
os.environ['HTTPS_PROXY'] = 'http://127.0.0.1:7890'r = requests.get("https://www.google.com")
print(r.status_code) # 能返回 200 就说明代理成功了
from langchain_google_genai import ChatGoogleGenerativeAIllm = ChatGoogleGenerativeAI(model="gemini-2.0-flash-001", # 或其他可用模型
)print(llm.invoke("你好呀!你现在通了吗?").content)
你好!我一直在线,随时待命。所以,是的,我可以说是“通了”!有什么我可以帮助你的吗?
JSON Output:JSON输出
使用 LLM 生成的最常见格式是 JSON,然后可以将其用于,例如:
-
将它发送到前端代码
-
将其保存到数据库中
# openai API
from langchain_openai import ChatOpenAI
from langchain_core.pydantic_v1 import BaseModelclass AnswerWithJustification(BaseModel):'''An answer to the user question along with justification for the answer.'''answer: str'''The answer to the user's question'''justification: str'''Justification for the answer'''llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
structured_llm = llm.with_structured_output(AnswerWithJustification)
structured_llm.invoke("What weighs more, a pound of bricks or a pound of feathers")
输出为:
{answer: "They weigh the same", justification: "Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volu"... 42 more characters
}
我们使用gemini API来复现
但是langchain_core.pydantic_v1 是为兼容旧版本 pydantic v1 而设的临时模块,但现在 LangChain 已经全面升级到了 pydantic v2,建议不要再用这个兼容模块了。
用from langchain_core.pydantic_v1 import BaseModel
出现了红色的提示报错。所以我们改写为from pydantic import BaseModel
, 这样就直接使用了最新版的 pydantic,不会再触发警告。
from langchain_google_genai import ChatGoogleGenerativeAI
from pydantic import BaseModelclass AnswerWithJustification(BaseModel):'''An answer to the user question along with justification for the answer.'''answer: str'''The answer to the user's question'''justification: str'''Justification for the answer'''llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash-001", # 或其他可用模型temperature=0 # 让输出更确定、更稳定(不会随机发挥)
)structured_llm = llm.with_structured_output(AnswerWithJustification)structured_llm.invoke("What weighs more, a pound of bricks or a pound of feathers")
AnswerWithJustification(answer='They weigh the same.', justification='A pound is a unit of weight, so a pound of bricks and a pound of feathers weigh the same amount..')
总体目标:
让大语言模型(LLM)返回结构化的数据(JSON),并且符合你自定义的格式(schema)。
第一步:定义了一个“结构模板”(schema):
class AnswerWithJustification(BaseModel):answer: strjustification: str
这就是你希望模型返回的数据格式 —— 一个包含两个字段的 JSON:
{"answer": "...","justification": "..."
}
第二步:让 LLM “知道” 要用这个格式
structured_llm = llm.with_structured_output(AnswerWithJustification)
第三步:使用这个结构化模型去提问
structured_llm.invoke("What weighs more, a pound of bricks or a pound of feathers?")
这一步就是真正向模型提问。模型在回答前,会参考你定义的 schema,生成符合格式的 JSON 数据。
首先将 schema 转为 JSON Schema,把你的 Python 模板类转成 JSON 格式的规则。然后发给LLM,告诉模型“你输出要符合这个格式”。最后验证输出,模型生成后再校验是否合规,确保你收到的数据符合格式。
你就像是在说:
“AI,你回答我的时候,不能随便写一段文字,必须照着我这张表格来写,字段名和格式都要对上!”
Other Machine-Readable Formats with Output Parsers:其他带有输出解析器的机器可读格式
输出解析器是干嘛的?
输出解析器是帮助大语言模型(LLM)把结果以特定格式输出的一种工具。它有两个主要功能:
- 提供格式说明(Providing format instructions)
你可以用解析器给提示(prompt)加上一些额外的说明,比如告诉模型:
“请把结果输出成 XML 格式” 或
“请生成一个 JSON 对象,字段有 name 和 age”
这样模型就知道你想要的输出长什么样。
- 验证和解析输出(Validating and parsing output)
LLM 返回结果后,输出解析器还可以:
把普通文本转换成结构化格式(如列表、XML、JSON等);
校验格式是否正确;
修复模型输出中不完整或多余的内容。
这是一个输出解析器的工作示例
from langchain_core.output_parsers import CommaSeparatedListOutputParserparser = CommaSeparatedListOutputParser()items = parser.invoke("apple, banana, cherry")print(items)
['apple', 'banana', 'cherry']
LangChain 为各种用例提供了多种输出解析器,包括 CSV、XML 等。在下一节中,我们将了解如何将输出解析器与模型和提示组合使用。