跳到主要内容
Open In ColabOpen on GitHub

构建提取链

在本教程中,我们将使用聊天模型工具调用功能,从非结构化文本中提取结构化信息。我们还将演示如何在这种情况下使用少样本提示来提高性能。

重要

本教程需要langchain-core>=0.3.20,并且只适用于支持工具调用的模型。

设置

Jupyter Notebook

本教程及其他教程在Jupyter notebooks中运行可能最为方便。在交互式环境中学习指南是更好地理解它们的绝佳方式。有关安装说明,请参见此处

安装

要安装 LangChain,请运行

pip install --upgrade langchain-core

更多详情,请参阅我们的安装指南

LangSmith

您使用 LangChain 构建的许多应用程序将包含多个步骤和多次 LLM 调用。随着这些应用程序变得越来越复杂,能够检查您的链或代理内部究竟发生了什么变得至关重要。最好的方法是使用LangSmith

在上述链接注册后,请务必设置您的环境变量以开始记录跟踪日志

export LANGSMITH_TRACING="true"
export LANGSMITH_API_KEY="..."

或者,如果在Notebook中,您可以这样设置

import getpass
import os

os.environ["LANGSMITH_TRACING"] = "true"
os.environ["LANGSMITH_API_KEY"] = getpass.getpass()

模式

首先,我们需要描述我们要从文本中提取哪些信息。

我们将使用 Pydantic 来定义一个示例模式以提取个人信息。

from typing import Optional

from pydantic import BaseModel, Field


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

# ^ Doc-string for the entity Person.
# This doc-string is sent to the LLM as the description of the schema Person,
# and it can help to improve extraction results.

# Note that:
# 1. Each field is an `optional` -- this allows the model to decline to extract it!
# 2. Each field has a `description` -- this description is used by the LLM.
# Having a good description can help improve extraction results.
name: Optional[str] = Field(default=None, description="The name of the person")
hair_color: Optional[str] = Field(
default=None, description="The color of the person's hair if known"
)
height_in_meters: Optional[str] = Field(
default=None, description="Height measured in meters"
)

定义模式时有两条最佳实践:

  1. 文档化属性模式本身:此信息将发送给 LLM,用于提高信息提取的质量。
  2. 不要强制LLM编造信息!上面我们对属性使用了Optional,允许LLM在不知道答案时输出None
重要

为获得最佳性能,请充分文档化模式,并确保在文本中没有可提取信息时,模型不会被迫返回结果。

提取器

让我们使用上面定义的模式创建一个信息提取器。

from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder

# Define a custom prompt to provide instructions and any additional context.
# 1) You can add examples into the prompt template to improve extraction quality
# 2) Introduce additional parameters to take context into account (e.g., include metadata
# about the document from which the text was extracted.)
prompt_template = ChatPromptTemplate.from_messages(
[
(
"system",
"You are an expert extraction algorithm. "
"Only extract relevant information from the text. "
"If you do not know the value of an attribute asked to extract, "
"return null for the attribute's value.",
),
# Please see the how-to about improving performance with
# reference examples.
# MessagesPlaceholder('examples'),
("human", "{text}"),
]
)

我们需要使用支持函数/工具调用的模型。

请查阅文档,了解所有可与此 API 配合使用的模型。

pip install -qU "langchain[google-genai]"
import getpass
import os

if not os.environ.get("GOOGLE_API_KEY"):
os.environ["GOOGLE_API_KEY"] = getpass.getpass("Enter API key for Google Gemini: ")

from langchain.chat_models import init_chat_model

llm = init_chat_model("gemini-2.0-flash", model_provider="google_genai")
structured_llm = llm.with_structured_output(schema=Person)

我们来测试一下

text = "Alan Smith is 6 feet tall and has blond hair."
prompt = prompt_template.invoke({"text": text})
structured_llm.invoke(prompt)
Person(name='Alan Smith', hair_color='blond', height_in_meters='1.83')
重要

提取是生成式的 🤯

LLM 是生成模型,所以它们可以做一些非常酷的事情,比如即使身高以英尺提供,也能正确地提取出以米为单位的身高!

我们可以在此处查看 LangSmith 跟踪。请注意,跟踪的聊天模型部分揭示了发送给模型的精确消息序列、调用的工具以及其他元数据。

多实体

大多数情况下,您应该提取实体列表而不是单个实体。

这可以通过 Pydantic 通过模型嵌套来实现。

from typing import List, Optional

from pydantic import BaseModel, Field


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

# ^ Doc-string for the entity Person.
# This doc-string is sent to the LLM as the description of the schema Person,
# and it can help to improve extraction results.

# Note that:
# 1. Each field is an `optional` -- this allows the model to decline to extract it!
# 2. Each field has a `description` -- this description is used by the LLM.
# Having a good description can help improve extraction results.
name: Optional[str] = Field(default=None, description="The name of the person")
hair_color: Optional[str] = Field(
default=None, description="The color of the person's hair if known"
)
height_in_meters: Optional[str] = Field(
default=None, description="Height measured in meters"
)


class Data(BaseModel):
"""Extracted data about people."""

# Creates a model so that we can extract multiple entities.
people: List[Person]
重要

提取结果可能不完美。继续阅读以了解如何使用参考示例来提高提取质量,并查看我们的提取操作指南以获取更多详细信息。

structured_llm = llm.with_structured_output(schema=Data)
text = "My name is Jeff, my hair is black and i am 6 feet tall. Anna has the same color hair as me."
prompt = prompt_template.invoke({"text": text})
structured_llm.invoke(prompt)
Data(people=[Person(name='Jeff', hair_color='black', height_in_meters='1.83'), Person(name='Anna', hair_color='black', height_in_meters=None)])
提示

当模式支持提取多个实体时,如果文本中没有相关信息,它也允许模型通过提供空列表来提取零实体

这通常是件好事!它允许指定实体的必填属性,而无需强制模型检测到该实体。

我们可以在此处查看 LangSmith 跟踪。

参考示例

LLM 应用程序的行为可以通过少样本提示进行引导。对于聊天模型,这可以采取输入和响应消息对序列的形式,以展示所需的行为。

例如,我们可以通过交替的userassistant消息来传达符号的含义

messages = [
{"role": "user", "content": "2 🦜 2"},
{"role": "assistant", "content": "4"},
{"role": "user", "content": "2 🦜 3"},
{"role": "assistant", "content": "5"},
{"role": "user", "content": "3 🦜 4"},
]

response = llm.invoke(messages)
print(response.content)
7

结构化输出通常在底层使用工具调用。这通常涉及生成包含工具调用的AI 消息,以及包含工具调用结果的工具消息。在这种情况下,消息序列应该是什么样子?

不同的聊天模型提供商对有效消息序列有不同的要求。有些会接受以下形式的(重复)消息序列:

  • 用户消息
  • 包含工具调用的AI消息
  • 包含结果的工具消息

其他则要求最终的AI消息包含某种形式的响应。

LangChain 包含一个实用函数tool_example_to_messages,它将为大多数模型提供商生成有效的序列。它通过仅要求 Pydantic 表示相应的工具调用来简化结构化少样本示例的生成。

让我们试试这个。我们可以将输入字符串和所需的 Pydantic 对象对转换为可提供给聊天模型的消息序列。在底层,LangChain 会将工具调用格式化为每个提供商所需的格式。

注意:此版本的tool_example_to_messages需要langchain-core>=0.3.20

from langchain_core.utils.function_calling import tool_example_to_messages

examples = [
(
"The ocean is vast and blue. It's more than 20,000 feet deep.",
Data(people=[]),
),
(
"Fiona traveled far from France to Spain.",
Data(people=[Person(name="Fiona", height_in_meters=None, hair_color=None)]),
),
]


messages = []

for txt, tool_call in examples:
if tool_call.people:
# This final message is optional for some providers
ai_response = "Detected people."
else:
ai_response = "Detected no people."
messages.extend(tool_example_to_messages(txt, [tool_call], ai_response=ai_response))

检查结果,我们看到这两对示例生成了八条消息

for message in messages:
message.pretty_print()
================================ Human Message =================================

The ocean is vast and blue. It's more than 20,000 feet deep.
================================== Ai Message ==================================
Tool Calls:
Data (d8f2e054-7fb9-417f-b28f-0447a775b2c3)
Call ID: d8f2e054-7fb9-417f-b28f-0447a775b2c3
Args:
people: []
================================= Tool Message =================================

You have correctly called this tool.
================================== Ai Message ==================================

Detected no people.
================================ Human Message =================================

Fiona traveled far from France to Spain.
================================== Ai Message ==================================
Tool Calls:
Data (0178939e-a4b1-4d2a-a93e-b87f665cdfd6)
Call ID: 0178939e-a4b1-4d2a-a93e-b87f665cdfd6
Args:
people: [{'name': 'Fiona', 'hair_color': None, 'height_in_meters': None}]
================================= Tool Message =================================

You have correctly called this tool.
================================== Ai Message ==================================

Detected people.

让我们比较一下有无这些消息的性能。例如,我们传递一个不打算提取任何人物的消息

message_no_extraction = {
"role": "user",
"content": "The solar system is large, but earth has only 1 moon.",
}

structured_llm = llm.with_structured_output(schema=Data)
structured_llm.invoke([message_no_extraction])
Data(people=[Person(name='Earth', hair_color='None', height_in_meters='0.00')])

在此示例中,模型容易错误地生成人员记录。

因为我们的少样本示例包含“否定”示例,所以我们鼓励模型在这种情况下表现正确。

structured_llm.invoke(messages + [message_no_extraction])
Data(people=[])
提示

运行的LangSmith跟踪揭示了发送给聊天模型的精确消息序列、生成的工具调用、延迟、令牌计数以及其他元数据。

有关使用参考示例进行提取工作流的更多详细信息,包括如何合并提示模板和自定义示例消息的生成,请参阅本指南

下一步

现在您已经了解了 LangChain 提取的基础知识,您可以继续学习其余的操作指南

  • 添加示例:关于使用参考示例提高性能的更多细节。
  • 处理长文本:如果文本不适合 LLM 的上下文窗口,您该怎么办?
  • 使用解析方法:使用基于提示的方法,通过不支持工具/函数调用的模型进行提取。