跳到主要内容

构建提取链

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

重要

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

设置

Jupyter Notebook

本教程和其他教程最好在 Jupyter notebooks 中运行。在交互式环境中浏览指南是更好地理解它们的好方法。有关如何安装的说明,请参阅此处

安装

要安装 LangChain,请运行

pip install --upgrade langchain-core

有关更多详细信息,请参阅我们的安装指南

LangSmith

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

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

export LANGCHAIN_TRACING_V2="true"
export LANGCHAIN_API_KEY="..."

或者,如果在笔记本中,您可以使用以下方式设置它们

import getpass
import os

os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_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 typing import Optional

from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from pydantic import BaseModel, Field

# 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-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_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o-mini")
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 的上下文窗口,您应该怎么做?
  • 使用解析方法:使用基于提示的方法,使用不支持工具/函数调用的模型进行提取。

此页面有帮助吗?