
Tuana Çelik • 2025-05-07
在 LlamaExtract 中获取提取数据的引用和推理
基于代理的数据提取工作流程在简化从复杂数据源中提取重要和相关信息的任务方面做得非常出色。LlamaExtract 允许您定义要从文档中提取的字段,并使用先进的文档解析和 LLM 来提取与这些字段相关的数据。
此外,在许多情况下,包含某个字段为何被提取以及特定信息是从给定文档的哪个部分提取的原因和引用可能是有意义的。因此,作为我们对 LlamaExtract 最新改进的一部分,我们为每个提取的字段引入了可选的引用和推理功能。
现在,对于我们从文档中提取的每一个字段,我们都会提供提取该信息的精确位置,以及代理进行提取的推理。
在本文中,我们将通过一个简单的例子来讲解如何从 SEC 备案报告中提取信息。
创建提取代理
LlamaCloud 用户可以通过 LlamaCloud UI 以及开源 SDK 创建提取代理。
在 LlamaCloud 中,从“创建提取代理”开始

定义提取模式
通过 LlamaCloud 配置提取代理时,您可以从预定义的模式集合中选择。在本文中,我们将从 NVIDIA 2025 年 10k SEC 备案文件中提取信息,因此我们可以选择预构建的 10K 备案模式。

我们还可以选择定义自己的自定义模式,可以在 UI 中构建,也可以通过编程方式创建。例如,通过 Python SDK 创建提取代理时,我们可以使用 Pydantic 初始化我们自己的模式
from pydantic import BaseModel, Field
from enum import Enum
class FilingType(str, Enum):
ten_k = '10 K'
ten_q = '10-Q'
ten_ka = '10-K/A'
ten_qa = '10-Q/A'
class FinancialReport(BaseModel):
company_name: str = Field(description="The name of the company")
description: str = Field(description="Basic information about the filing")
filing_type: FilingType = Field(description="Type of SEC filing")
filing_date: str = Field(description="Date when filing was submitted to SEC")
fiscal_year: int = Field(description="Fiscal year")
unit: str = Field(description="Unit of financial figures (thousands, millions, etc.)")
revenue: int = Field(description="Total revenue for period")
每个字段的描述为代理提供了额外的上下文,解释了底层 LLM 应该从 SEC 备案文档中提取什么。
请求引用和推理
在配置提取代理时,我们还可以开启一些高级设置。在这里,我们可以要求代理为每个字段包含引用,并为每个引用提供推理。

这也可以通过提供我们自己的 ExtractConfig
并将 use_reasoning
和 cite_resources
设置为 True 来实现。
from llama_cloud.types import ExtractConfig, ExtractMode
config = ExtractConfig(use_reasoning=True,
cite_sources=True,
extraction_mode=ExtractMode.MULTIMODAL)
agent = llama_extract.create_agent(name="filing-parser",
data_schema=FinancialReport,
config=config)
从 SEC 备案文件中提取数据的示例
例如,对于关于 NVIDIA SEC 备案文件的 PDF 示例文档,使用我们上面提供的自定义模式,我们得到了以下数据

现在,我们还可以在 extraction_metadata
中检查每个已提取字段的引用

让我们更详细地看看引用和推理。推理字段解释了为什么提取了某个字段。虽然此字段可能包含更多解释,但您也可能注意到其中一些只会简单地引用 "VERBATIM EXTRACTION"
,这意味着提取该字段是因为它在文本中直接提及。
在某些字段未提取或留空的情况下,您也可能注意到推理字段提及 "INSUFFICIENT DATA"
,这表明源文档中没有支持性数据。
例如,我们的提取代理提取了 company_name
的 NVIDIA Corporation
,支持此提取的推理和引用如下
"company_name": {
"reasoning": "VERBATIM EXTRACTION",
"citation": [
{
"page": 1,
"matching_text": "NVIDIA CORPORATION"
},
{
"page": 2,
"matching_text": "NVIDIA Corporation"
},
{
"page": 3,
"matching_text": "All references to \\"NVIDIA,\\" \\"we,\\" \\"us,\\" \\"our,\\" or the \\"Company\\" mean NVIDIA Corporation and its subsidiaries."
},
...
}
然而,对于 fiscal_year
的提取,我们在推理中也获得了更多解释
"fiscal_year": {
"reasoning": "The fiscal year ended January 26, 2025, indicates the fiscal year is 2025. Additionally, multiple references throughout the text confirm the fiscal year 2025 in various contexts.",
"citation": [
{
"page": 1,
"matching_text": "For the fiscal year ended January 26, 2025"
},
{
"page": 6,
"matching_text": "In fiscal year 2025, we launched the NVIDIA Blackwell architecture"
},
{
"page": 12,
"matching_text": "fiscal year 2025"
},
{
"page": 17,
"matching_text": "our gross margins in the second quarter of fiscal year 2025 were negatively impacted"
},
...
}
下一步?
LlamaExtract 简化了从复杂文档中提取结构化数据的过程,使其既高效又透明。它允许您定义自定义提取模式并配置推理和引用设置。无论通过 LlamaCloud UI 还是开源 SDK,您都可以快速部署根据您的特定需求量身定制的强大提取代理。通过添加引用和推理功能,LlamaExtract 允许您验证提取的信息——为自动化文档处理带来清晰度和信心。
在本文中,我们简要介绍了 LlamaExtract 中的新推理和引用功能。下一步,您可以按照完整教程 “从财务报告中提取数据 - 带引用和推理”。