純文本輸出是有用的,但在某些情況下,我們需要 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 等。在下一節中,我們將了解如何將輸出解析器與模型和提示組合使用。