构建提取链
在本教程中,我们将使用聊天模型的工具调用功能从非结构化文本中提取结构化信息。我们还将演示如何在此上下文中使用 few-shot 提示来提高性能。
本教程要求langchain-core>=0.3.20并且仅适用于支持工具调用的模型。
设置
Jupyter 笔记本
本教程和其他教程可能在 Jupyter 笔记本中运行最方便。在交互式环境中浏览指南是更好地了解它们的好方法。有关如何安装的说明,请参阅此处。
安装
要安装 LangChain,请运行:
- 果仁
- 康达
pip install --upgrade langchain-core
conda install langchain-core -c conda-forge
有关更多详细信息,请参阅我们的安装指南。
LangSmith
您使用 LangChain 构建的许多应用程序将包含多个步骤,其中包含多次调用 LLM 调用。 随着这些应用程序变得越来越复杂,能够检查您的链条或代理内部到底发生了什么变得至关重要。 最好的方法是使用 LangSmith。
在上面的链接中注册后,请确保设置环境变量以开始记录跟踪:
export LANGSMITH_TRACING="true"
export LANGSMITH_API_KEY="..."
或者,如果在笔记本中,您可以通过以下方式设置它们:
import getpass
import os
os.environ["LANGSMITH_TRACING"] = "true"
os.environ["LANGSMITH_API_KEY"] = getpass.getpass()
架构
首先,我们需要描述我们想要从文本中提取什么信息。
我们将使用 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 时,有两种最佳实践:
- 记录属性和架构本身:此信息将发送到 LLM 并用于提高信息提取的质量。
- 不要强迫 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[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 是生成模型,因此它们可以做一些非常酷的事情,例如正确提取以米为单位的人的身高 即使它是以 Feet 提供的!
我们可以在这里看到 LangSmith 跟踪。请注意,跟踪的 chat model 部分揭示了发送到模型的消息、调用的工具和其他元数据的确切顺序。
多个实体
在大多数情况下,您应该提取实体列表,而不是单个实体。
这可以通过使用 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 应用程序的行为可以使用 few-shot 提示进行控制。对于聊天模型,这可以采用一系列输入和响应消息对的形式,用于演示所需的行为。
例如,我们可以通过交替来传达符号的含义user和assistant 消息:
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 消息
- 包含结果的工具消息
其他 API 需要包含某种响应的最终 AI 消息。
LangChain 包含一个实用程序函数 tool_example_to_messages,它将为大多数模型提供程序生成有效的序列。它简化了结构化 few-shot 示例的生成,只需 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))
检查结果,我们看到这两个示例对生成了 8 条消息:
for message in messages:
message.pretty_print()
================================[1m Human Message [0m=================================
The ocean is vast and blue. It's more than 20,000 feet deep.
==================================[1m Ai Message [0m==================================
Tool Calls:
Data (d8f2e054-7fb9-417f-b28f-0447a775b2c3)
Call ID: d8f2e054-7fb9-417f-b28f-0447a775b2c3
Args:
people: []
=================================[1m Tool Message [0m=================================
You have correctly called this tool.
==================================[1m Ai Message [0m==================================
Detected no people.
================================[1m Human Message [0m=================================
Fiona traveled far from France to Spain.
==================================[1m Ai Message [0m==================================
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}]
=================================[1m Tool Message [0m=================================
You have correctly called this tool.
==================================[1m Ai Message [0m==================================
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')])
在此示例中,该模型可能会错误地生成 people 的记录。
因为我们的 few-shot 示例包含 “negatives” 示例,所以我们鼓励模型在这种情况下正确运行:
structured_llm.invoke(messages + [message_no_extraction])
Data(people=[])
运行的 LangSmith 跟踪揭示了发送到聊天模型的消息的确切顺序、生成的工具调用、延迟、令牌计数和其他元数据。
有关提取工作流的更多详细信息以及参考示例,包括如何合并提示模板和自定义示例消息的生成,请参阅本指南。
后续步骤
现在,您已经了解了使用 LangChain 进行提取的基础知识,您可以继续学习其余的操作指南: