跳到主要内容
Open In ColabOpen on GitHub

构建提取链

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

重要提示

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

设置

Jupyter Notebook

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

安装

要安装 LangChain,请运行

pip install --upgrade langchain-core

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

LangSmith

您使用 LangChain 构建的许多应用程序将包含多个步骤,其中包含多次 LLM 调用。随着这些应用程序变得越来越复杂,能够检查您的链或 Agent 内部到底发生了什么是至关重要的。最好的方法是使用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()

Schema

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

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

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"
)

定义 schema 时有两个最佳实践

  1. 记录 属性schema 本身:此信息将发送到 LLM,并用于提高信息提取的质量。
  2. 不要强迫 LLM 编造信息!上面我们在属性中使用了 Optional,允许 LLM 在不知道答案时输出 None
重要提示

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

提取器

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

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.chat_models import init_chat_model

llm = init_chat_model("gpt-4o-mini", model_provider="openai")
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)])
提示

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

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

我们可以在此处查看 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 的上下文窗口,您应该怎么做?
  • 使用解析方法:使用基于提示的方法,以便在使用不支持工具/函数调用的模型进行提取。

此页面是否对您有所帮助?