跳到主要內容
Open In ColabOpen on GitHub

如何從模型返回結構化資料

先決條件

本指南假設您熟悉以下概念

通常,讓模型返回符合特定結構描述的輸出非常有用。一個常見的用例是從文本中提取資料,以插入資料庫或與其他下游系統一起使用。本指南涵蓋了從模型取得結構化輸出的幾種策略。

.with_structured_output() 方法

支援的模型

這是取得結構化輸出最簡單且最可靠的方式。with_structured_output() 是為提供用於結構化輸出的原生 API 的模型實作的,例如工具/函式呼叫或 JSON 模式,並在底層使用這些功能。

此方法接受結構描述作為輸入,該結構描述指定所需輸出屬性的名稱、類型和描述。該方法返回一個類似模型的 Runnable,但不同之處在於,它輸出的不是字串或訊息,而是對應於給定結構描述的物件。結構描述可以指定為 TypedDict 類別、JSON Schema 或 Pydantic 類別。如果使用 TypedDict 或 JSON Schema,則 Runnable 將返回字典,如果使用 Pydantic 類別,則將返回 Pydantic 物件。

作為範例,讓我們讓模型生成一個笑話,並將笑點的鋪陳與笑點分開

pip install -qU "langchain[openai]"
import getpass
import os

if not os.environ.get("OPENAI_API_KEY"):
os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter API key for OpenAI: ")

from langchain.chat_models import init_chat_model

llm = init_chat_model("gpt-4o-mini", model_provider="openai")

Pydantic 類別

如果我們希望模型返回 Pydantic 物件,我們只需要傳入所需的 Pydantic 類別即可。使用 Pydantic 的主要優點是模型生成的輸出將經過驗證。如果缺少任何必填欄位或任何欄位類型錯誤,Pydantic 將引發錯誤。

from typing import Optional

from pydantic import BaseModel, Field


# Pydantic
class Joke(BaseModel):
"""Joke to tell user."""

setup: str = Field(description="The setup of the joke")
punchline: str = Field(description="The punchline to the joke")
rating: Optional[int] = Field(
default=None, description="How funny the joke is, from 1 to 10"
)


structured_llm = llm.with_structured_output(Joke)

structured_llm.invoke("Tell me a joke about cats")
Joke(setup='Why was the cat sitting on the computer?', punchline='Because it wanted to keep an eye on the mouse!', rating=7)
提示

除了 Pydantic 類別的結構之外,Pydantic 類別的名稱、文件字串以及參數的名稱和提供的描述都非常重要。大多數時候,with_structured_output 使用的是模型的函式/工具呼叫 API,您可以有效地將所有這些資訊視為已新增到模型提示中。

TypedDict 或 JSON Schema

如果您不想使用 Pydantic,明確地不想要驗證引數,或者希望能夠串流模型輸出,則可以使用 TypedDict 類別定義您的結構描述。我們可以選擇性地使用 LangChain 支援的特殊 Annotated 語法,該語法允許您指定欄位的預設值和描述。請注意,如果模型未產生預設值,則不會自動填入預設值,它僅用於定義傳遞給模型的結構描述。

要求
  • 核心:langchain-core>=0.2.26
  • 類型擴充:強烈建議從 typing_extensions 而不是 typing 匯入 AnnotatedTypedDict,以確保跨 Python 版本的一致行為。
from typing import Optional

from typing_extensions import Annotated, TypedDict


# TypedDict
class Joke(TypedDict):
"""Joke to tell user."""

setup: Annotated[str, ..., "The setup of the joke"]

# Alternatively, we could have specified setup as:

# setup: str # no default, no description
# setup: Annotated[str, ...] # no default, no description
# setup: Annotated[str, "foo"] # default, no description

punchline: Annotated[str, ..., "The punchline of the joke"]
rating: Annotated[Optional[int], None, "How funny the joke is, from 1 to 10"]


structured_llm = llm.with_structured_output(Joke)

structured_llm.invoke("Tell me a joke about cats")
{'setup': 'Why was the cat sitting on the computer?',
'punchline': 'Because it wanted to keep an eye on the mouse!',
'rating': 7}

或者,我們可以傳入 JSON Schema 字典。這不需要匯入或類別,並且可以非常清楚地了解每個參數的文檔記錄方式,但代價是有點冗長。

json_schema = {
"title": "joke",
"description": "Joke to tell user.",
"type": "object",
"properties": {
"setup": {
"type": "string",
"description": "The setup of the joke",
},
"punchline": {
"type": "string",
"description": "The punchline to the joke",
},
"rating": {
"type": "integer",
"description": "How funny the joke is, from 1 to 10",
"default": None,
},
},
"required": ["setup", "punchline"],
}
structured_llm = llm.with_structured_output(json_schema)

structured_llm.invoke("Tell me a joke about cats")
{'setup': 'Why was the cat sitting on the computer?',
'punchline': 'Because it wanted to keep an eye on the mouse!',
'rating': 7}

在多個結構描述之間選擇

讓模型從多個結構描述中選擇的最簡單方法是建立一個具有 Union 類型屬性的父結構描述。

使用 Pydantic

from typing import Union


class Joke(BaseModel):
"""Joke to tell user."""

setup: str = Field(description="The setup of the joke")
punchline: str = Field(description="The punchline to the joke")
rating: Optional[int] = Field(
default=None, description="How funny the joke is, from 1 to 10"
)


class ConversationalResponse(BaseModel):
"""Respond in a conversational manner. Be kind and helpful."""

response: str = Field(description="A conversational response to the user's query")


class FinalResponse(BaseModel):
final_output: Union[Joke, ConversationalResponse]


structured_llm = llm.with_structured_output(FinalResponse)

structured_llm.invoke("Tell me a joke about cats")
FinalResponse(final_output=Joke(setup='Why was the cat sitting on the computer?', punchline='Because it wanted to keep an eye on the mouse!', rating=7))
structured_llm.invoke("How are you today?")
FinalResponse(final_output=ConversationalResponse(response="I'm just a computer program, so I don't have feelings, but I'm here and ready to help you with whatever you need!"))

使用 TypedDict

from typing import Optional, Union

from typing_extensions import Annotated, TypedDict


class Joke(TypedDict):
"""Joke to tell user."""

setup: Annotated[str, ..., "The setup of the joke"]
punchline: Annotated[str, ..., "The punchline of the joke"]
rating: Annotated[Optional[int], None, "How funny the joke is, from 1 to 10"]


class ConversationalResponse(TypedDict):
"""Respond in a conversational manner. Be kind and helpful."""

response: Annotated[str, ..., "A conversational response to the user's query"]


class FinalResponse(TypedDict):
final_output: Union[Joke, ConversationalResponse]


structured_llm = llm.with_structured_output(FinalResponse)

structured_llm.invoke("Tell me a joke about cats")
{'final_output': {'setup': 'Why was the cat sitting on the computer?',
'punchline': 'Because it wanted to keep an eye on the mouse!',
'rating': 7}}
structured_llm.invoke("How are you today?")
{'final_output': {'response': "I'm just a computer program, so I don't have feelings, but I'm here and ready to help you with whatever you need!"}}

回應應與 Pydantic 範例中顯示的回應相同。

或者,如果您的所選模型支援,您可以直接使用工具呼叫來允許模型在選項之間進行選擇。這需要更多的解析和設定,但在某些情況下可以帶來更好的效能,因為您不必使用巢狀結構描述。有關更多詳細資訊,請參閱此操作指南

串流

當輸出類型為字典時(即,當結構描述指定為 TypedDict 類別或 JSON Schema 字典時),我們可以從結構化模型串流輸出。

資訊

請注意,產生的已經是聚合區塊,而不是增量。

from typing_extensions import Annotated, TypedDict


# TypedDict
class Joke(TypedDict):
"""Joke to tell user."""

setup: Annotated[str, ..., "The setup of the joke"]
punchline: Annotated[str, ..., "The punchline of the joke"]
rating: Annotated[Optional[int], None, "How funny the joke is, from 1 to 10"]


structured_llm = llm.with_structured_output(Joke)

for chunk in structured_llm.stream("Tell me a joke about cats"):
print(chunk)
{}
{'setup': ''}
{'setup': 'Why'}
{'setup': 'Why was'}
{'setup': 'Why was the'}
{'setup': 'Why was the cat'}
{'setup': 'Why was the cat sitting'}
{'setup': 'Why was the cat sitting on'}
{'setup': 'Why was the cat sitting on the'}
{'setup': 'Why was the cat sitting on the computer'}
{'setup': 'Why was the cat sitting on the computer?'}
{'setup': 'Why was the cat sitting on the computer?', 'punchline': ''}
{'setup': 'Why was the cat sitting on the computer?', 'punchline': 'Because'}
{'setup': 'Why was the cat sitting on the computer?', 'punchline': 'Because it'}
{'setup': 'Why was the cat sitting on the computer?', 'punchline': 'Because it wanted'}
{'setup': 'Why was the cat sitting on the computer?', 'punchline': 'Because it wanted to'}
{'setup': 'Why was the cat sitting on the computer?', 'punchline': 'Because it wanted to keep'}
{'setup': 'Why was the cat sitting on the computer?', 'punchline': 'Because it wanted to keep an'}
{'setup': 'Why was the cat sitting on the computer?', 'punchline': 'Because it wanted to keep an eye'}
{'setup': 'Why was the cat sitting on the computer?', 'punchline': 'Because it wanted to keep an eye on'}
{'setup': 'Why was the cat sitting on the computer?', 'punchline': 'Because it wanted to keep an eye on the'}
{'setup': 'Why was the cat sitting on the computer?', 'punchline': 'Because it wanted to keep an eye on the mouse'}
{'setup': 'Why was the cat sitting on the computer?', 'punchline': 'Because it wanted to keep an eye on the mouse!'}
{'setup': 'Why was the cat sitting on the computer?', 'punchline': 'Because it wanted to keep an eye on the mouse!', 'rating': 7}

少量提示

對於更複雜的結構描述,在提示中新增少量範例非常有用。這可以透過幾種方式完成。

最簡單且最通用的方法是在提示的系統訊息中新增範例

from langchain_core.prompts import ChatPromptTemplate

system = """You are a hilarious comedian. Your specialty is knock-knock jokes. \
Return a joke which has the setup (the response to "Who's there?") and the final punchline (the response to "<setup> who?").

Here are some examples of jokes:

example_user: Tell me a joke about planes
example_assistant: {{"setup": "Why don't planes ever get tired?", "punchline": "Because they have rest wings!", "rating": 2}}

example_user: Tell me another joke about planes
example_assistant: {{"setup": "Cargo", "punchline": "Cargo 'vroom vroom', but planes go 'zoom zoom'!", "rating": 10}}

example_user: Now about caterpillars
example_assistant: {{"setup": "Caterpillar", "punchline": "Caterpillar really slow, but watch me turn into a butterfly and steal the show!", "rating": 5}}"""

prompt = ChatPromptTemplate.from_messages([("system", system), ("human", "{input}")])

few_shot_structured_llm = prompt | structured_llm
few_shot_structured_llm.invoke("what's something funny about woodpeckers")
API 參考:ChatPromptTemplate
{'setup': 'Woodpecker',
'punchline': "Woodpecker you a joke, but I'm afraid it might be too 'hole-some'!",
'rating': 7}

當結構化輸出的底層方法是工具呼叫時,我們可以將範例作為明確的工具呼叫傳入。您可以查看您使用的模型是否在其 API 參考中使用了工具呼叫。

from langchain_core.messages import AIMessage, HumanMessage, ToolMessage

examples = [
HumanMessage("Tell me a joke about planes", name="example_user"),
AIMessage(
"",
name="example_assistant",
tool_calls=[
{
"name": "joke",
"args": {
"setup": "Why don't planes ever get tired?",
"punchline": "Because they have rest wings!",
"rating": 2,
},
"id": "1",
}
],
),
# Most tool-calling models expect a ToolMessage(s) to follow an AIMessage with tool calls.
ToolMessage("", tool_call_id="1"),
# Some models also expect an AIMessage to follow any ToolMessages,
# so you may need to add an AIMessage here.
HumanMessage("Tell me another joke about planes", name="example_user"),
AIMessage(
"",
name="example_assistant",
tool_calls=[
{
"name": "joke",
"args": {
"setup": "Cargo",
"punchline": "Cargo 'vroom vroom', but planes go 'zoom zoom'!",
"rating": 10,
},
"id": "2",
}
],
),
ToolMessage("", tool_call_id="2"),
HumanMessage("Now about caterpillars", name="example_user"),
AIMessage(
"",
tool_calls=[
{
"name": "joke",
"args": {
"setup": "Caterpillar",
"punchline": "Caterpillar really slow, but watch me turn into a butterfly and steal the show!",
"rating": 5,
},
"id": "3",
}
],
),
ToolMessage("", tool_call_id="3"),
]
system = """You are a hilarious comedian. Your specialty is knock-knock jokes. \
Return a joke which has the setup (the response to "Who's there?") \
and the final punchline (the response to "<setup> who?")."""

prompt = ChatPromptTemplate.from_messages(
[("system", system), ("placeholder", "{examples}"), ("human", "{input}")]
)
few_shot_structured_llm = prompt | structured_llm
few_shot_structured_llm.invoke({"input": "crocodiles", "examples": examples})
{'setup': 'Crocodile',
'punchline': 'Crocodile be seeing you later, alligator!',
'rating': 6}

有關使用工具呼叫時少量提示的更多資訊,請參閱此處

(進階)指定結構化輸出方法

對於支援多種結構化輸出方式的模型(即,它們同時支援工具呼叫和 JSON 模式),您可以使用 method= 引數指定要使用的方法。

JSON 模式

如果使用 JSON 模式,您仍然需要在模型提示中指定所需的結構描述。您傳遞給 with_structured_output 的結構描述僅用於解析模型輸出,它不會像工具呼叫那樣傳遞給模型。

若要查看您使用的模型是否支援 JSON 模式,請查看 API 參考中的模型條目。

structured_llm = llm.with_structured_output(None, method="json_mode")

structured_llm.invoke(
"Tell me a joke about cats, respond in JSON with `setup` and `punchline` keys"
)
{'setup': 'Why was the cat sitting on the computer?',
'punchline': 'Because it wanted to keep an eye on the mouse!'}

(進階)原始輸出

LLM 在生成結構化輸出方面並不完美,尤其是在結構描述變得複雜時。您可以透過傳遞 include_raw=True 來避免引發例外狀況並自行處理原始輸出。這會變更輸出格式,使其包含原始訊息輸出、parsed 值(如果成功)以及任何產生的錯誤

structured_llm = llm.with_structured_output(Joke, include_raw=True)

structured_llm.invoke("Tell me a joke about cats")
{'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_f25ZRmh8u5vHlOWfTUw8sJFZ', 'function': {'arguments': '{"setup":"Why was the cat sitting on the computer?","punchline":"Because it wanted to keep an eye on the mouse!","rating":7}', 'name': 'Joke'}, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 33, 'prompt_tokens': 93, 'total_tokens': 126}, 'model_name': 'gpt-4o-2024-05-13', 'system_fingerprint': 'fp_4e2b2da518', 'finish_reason': 'stop', 'logprobs': None}, id='run-d880d7e2-df08-4e9e-ad92-dfc29f2fd52f-0', tool_calls=[{'name': 'Joke', 'args': {'setup': 'Why was the cat sitting on the computer?', 'punchline': 'Because it wanted to keep an eye on the mouse!', 'rating': 7}, 'id': 'call_f25ZRmh8u5vHlOWfTUw8sJFZ', 'type': 'tool_call'}], usage_metadata={'input_tokens': 93, 'output_tokens': 33, 'total_tokens': 126}),
'parsed': {'setup': 'Why was the cat sitting on the computer?',
'punchline': 'Because it wanted to keep an eye on the mouse!',
'rating': 7},
'parsing_error': None}

直接提示和解析模型輸出

並非所有模型都支援 .with_structured_output(),因為並非所有模型都支援工具呼叫或 JSON 模式。對於此類模型,您需要直接提示模型使用特定格式,並使用輸出解析器從原始模型輸出中提取結構化回應。

使用 PydanticOutputParser

以下範例使用內建的 PydanticOutputParser 來解析提示為符合給定 Pydantic 結構描述的聊天模型的輸出。請注意,我們正在從解析器的方法直接將 format_instructions 新增到提示中

from typing import List

from langchain_core.output_parsers import PydanticOutputParser
from langchain_core.prompts import ChatPromptTemplate
from pydantic import BaseModel, Field


class Person(BaseModel):
"""Information about a person."""

name: str = Field(..., description="The name of the person")
height_in_meters: float = Field(
..., description="The height of the person expressed in meters."
)


class People(BaseModel):
"""Identifying information about all people in a text."""

people: List[Person]


# Set up a parser
parser = PydanticOutputParser(pydantic_object=People)

# Prompt
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"Answer the user query. Wrap the output in `json` tags\n{format_instructions}",
),
("human", "{query}"),
]
).partial(format_instructions=parser.get_format_instructions())

讓我們看看發送到模型的資訊

query = "Anna is 23 years old and she is 6 feet tall"

print(prompt.invoke({"query": query}).to_string())
System: Answer the user query. Wrap the output in `json` tags
The output should be formatted as a JSON instance that conforms to the JSON schema below.

As an example, for the schema {"properties": {"foo": {"title": "Foo", "description": "a list of strings", "type": "array", "items": {"type": "string"}}}, "required": ["foo"]}
the object {"foo": ["bar", "baz"]} is a well-formatted instance of the schema. The object {"properties": {"foo": ["bar", "baz"]}} is not well-formatted.

Here is the output schema:
\`\`\`
{"description": "Identifying information about all people in a text.", "properties": {"people": {"title": "People", "type": "array", "items": {"$ref": "#/definitions/Person"}}}, "required": ["people"], "definitions": {"Person": {"title": "Person", "description": "Information about a person.", "type": "object", "properties": {"name": {"title": "Name", "description": "The name of the person", "type": "string"}, "height_in_meters": {"title": "Height In Meters", "description": "The height of the person expressed in meters.", "type": "number"}}, "required": ["name", "height_in_meters"]}}}
\`\`\`
Human: Anna is 23 years old and she is 6 feet tall

現在讓我們調用它

chain = prompt | llm | parser

chain.invoke({"query": query})
People(people=[Person(name='Anna', height_in_meters=1.8288)])

如需深入了解如何將輸出解析器與提示技術結合使用以獲得結構化輸出,請參閱本指南

自訂解析

您也可以使用 LangChain 運算式語言 (LCEL) 建立自訂提示和解析器,並使用純函式來解析模型的輸出

import json
import re
from typing import List

from langchain_core.messages import AIMessage
from langchain_core.prompts import ChatPromptTemplate
from pydantic import BaseModel, Field


class Person(BaseModel):
"""Information about a person."""

name: str = Field(..., description="The name of the person")
height_in_meters: float = Field(
..., description="The height of the person expressed in meters."
)


class People(BaseModel):
"""Identifying information about all people in a text."""

people: List[Person]


# Prompt
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"Answer the user query. Output your answer as JSON that "
"matches the given schema: \`\`\`json\n{schema}\n\`\`\`. "
"Make sure to wrap the answer in \`\`\`json and \`\`\` tags",
),
("human", "{query}"),
]
).partial(schema=People.schema())


# Custom parser
def extract_json(message: AIMessage) -> List[dict]:
"""Extracts JSON content from a string where JSON is embedded between \`\`\`json and \`\`\` tags.

Parameters:
text (str): The text containing the JSON content.

Returns:
list: A list of extracted JSON strings.
"""
text = message.content
# Define the regular expression pattern to match JSON blocks
pattern = r"\`\`\`json(.*?)\`\`\`"

# Find all non-overlapping matches of the pattern in the string
matches = re.findall(pattern, text, re.DOTALL)

# Return the list of matched JSON strings, stripping any leading or trailing whitespace
try:
return [json.loads(match.strip()) for match in matches]
except Exception:
raise ValueError(f"Failed to parse: {message}")

以下是發送到模型的提示

query = "Anna is 23 years old and she is 6 feet tall"

print(prompt.format_prompt(query=query).to_string())
System: Answer the user query. Output your answer as JSON that  matches the given schema: \`\`\`json
{'title': 'People', 'description': 'Identifying information about all people in a text.', 'type': 'object', 'properties': {'people': {'title': 'People', 'type': 'array', 'items': {'$ref': '#/definitions/Person'}}}, 'required': ['people'], 'definitions': {'Person': {'title': 'Person', 'description': 'Information about a person.', 'type': 'object', 'properties': {'name': {'title': 'Name', 'description': 'The name of the person', 'type': 'string'}, 'height_in_meters': {'title': 'Height In Meters', 'description': 'The height of the person expressed in meters.', 'type': 'number'}}, 'required': ['name', 'height_in_meters']}}}
\`\`\`. Make sure to wrap the answer in \`\`\`json and \`\`\` tags
Human: Anna is 23 years old and she is 6 feet tall

以下是我們調用它的外觀

chain = prompt | llm | extract_json

chain.invoke({"query": query})
[{'people': [{'name': 'Anna', 'height_in_meters': 1.8288}]}]

此頁面是否對您有幫助?