主题
已弃用
本页记录的是已弃用的 Extract v1 API。新的接入请使用 Extract v2;改造已有的 v1 代码请见v1 到 v2 迁移指南。
面对财报、合同、发票这类复杂文件,Llama Extract 可以借助大模型,按你要的结构化格式抽出与你相关的信息。
本例用 LlamaExtract 从一份 SEC 文件(具体是 Nvidia 的 2025 财年申报)里抽取结构化数据。
在基础的数据抽取之上,我们还会要求抽取智能体为每个抽取字段给出引用与推理。这让我们能够:
- 确认抽出的字段是否准确
- 理解大模型为什么抽出这条信息
- 最后这一点也给了我们调整系统提示词或字段描述、必要时改进结果的机会。
下面走的这个例子在 Llama Cloud 里也能复现,而且在那里你还可以从若干预置 schema 中挑一个,不必自己从零构建。
- python
- typescript
- python-legacy
!pip install "llama-cloud<2.0"npm install @llamaindex/llama-cloud zodpip install llama-cloud-services连接 Llama Cloud
开始前,请先提供你的 Llama Cloud API key。
- python
- typescript
- python-legacy
import os
from llama_cloud import LlamaCloud, AsyncLlamaCloud
# Could use async or sync clients, this page will use the sync client
client = LlamaCloud(api_key=os.environ["LLAMA_CLOUD_API_KEY"])import LlamaCloud from "@llamaindex/llama-cloud";
const client = new LlamaCloud({
apiKey: process.env.LLAMA_CLOUD_API_KEY!,
});import os
from getpass import getpass
if "LLAMA_CLOUD_API_KEY" not in os.environ:
os.environ["LLAMA_CLOUD_API_KEY"] = getpass("Enter your Llama Cloud API Key: ")
from llama_cloud_services import LlamaExtract
# Optionally, provide your project id, if not, it will use the 'Default' project
llama_extract = LlamaExtract()用 Llama Extract Agent 抽取数据
提供你自己的 schema
通过 API 使用 LlamaExtract 时,你要提供一份描述「想从交给智能体的文件与数据里抽出什么」的 schema。这里我们实际上是在做一个 SEC 申报文件的抽取智能体。
- python
- typescript
- python-legacy
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="Short description of the filing and what it contains")
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")import { z } from "zod";
const FilingType = z.enum(['10 K', '10-Q', '10-K/A', '10-Q/A']);
const FinancialReportSchema = z.object({
company_name: z.string().describe("The name of the company"),
description: z.string().describe("Short description of the filing and what it contains"),
filing_type: FilingType.describe("Type of SEC filing"),
filing_date: z.string().describe("Date when filing was submitted to SEC"),
fiscal_year: z.number().describe("Fiscal year"),
unit: z.string().describe("Unit of financial figures (thousands, millions, etc.)"),
revenue: z.number().describe("Total revenue for period"),
});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="Short description of the filing and what it contains")
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")设置引用与推理
可选地,我们能在 ExtractConfig 里设置智能体为它抽取的每个字段都给出引用。这些引用会指明该字段是从文件的哪一页、哪一节抽出来的。
把 use_reasoning 设为 True,还会要求智能体多做一步推理,解释为什么抽出该字段。
- python
- typescript
- python-legacy
agent = client.extraction.extraction_agents.create(
config={
"use_reasoning": True,
"cite_sources": True,
"extraction_mode": "MULTIMODAL",
},
data_schema=FinancialReport.model_json_schema(),
name="filing-parser",
)const agent = await client.extraction.extractionAgents.create({
name: "filing-parser",
data_schema: z.toJSONSchema(FinancialReportSchema),
config: {
use_reasoning: true,
cite_sources: true,
extraction_mode: "MULTIMODAL",
},
});from llama_cloud 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
)演示 —— 下载 PDF 并带引用抽取数据
- python
- typescript
- python-legacy
import requests
url = 'https://raw.githubusercontent.com/run-llama/llama_cloud_services/refs/heads/main/examples/extract/data/sec_filings/nvda_10k.pdf'
response = requests.get(url)
if response.status_code == 200:
with open('/content/nvda_10k.pdf', 'wb') as f:
f.write(response.content)
print("PDF downloaded successfully.")
else:
print(f"Failed to download. Status code: {response.status_code}")
file_obj = client.files.create(
file="/content/nvda_10k.pdf",
purpose="extract",
)
file_id = file_obj.id
result = client.extraction.jobs.extract(
extraction_agent_id=agent.id,
file_id=file_id,
)
print(result.data)const url = "https://raw.githubusercontent.com/run-llama/llama_cloud_services/refs/heads/main/examples/extract/data/sec_filings/nvda_10k.pdf"
const response = await fetch(url);
// Upload the file first
const fileObj = await client.files.create({
file: response,
purpose: "extract",
});
const fileId = fileObj.id;
const result = await client.extraction.jobs.extract({
extraction_agent_id: agent.id,
file_id: fileId,
});
console.log(result.data);import requests
url = 'https://raw.githubusercontent.com/run-llama/llama_cloud_services/refs/heads/main/examples/extract/data/sec_filings/nvda_10k.pdf'
response = requests.get(url)
if response.status_code == 200:
with open('/content/nvda_10k.pdf', 'wb') as f:
f.write(response.content)
print("PDF downloaded successfully.")
else:
print(f"Failed to download. Status code: {response.status_code}")
filing_info = agent.extract("/content/nvda_10k.pdf")
print(filing_info.data)你得到的输出应类似于:
{
'company_name': 'NVIDIA Corporation',
'description': "The filing provides a detailed overview of NVIDIA's business as a full-stack computing infrastructure company, discusses various technologies including digital avatars and autonomous vehicles, outlines numerous risk factors affecting operations such as supply chain issues and geopolitical tensions, and describes employee stock purchase plans and related compliance requirements.",
'filing_type': '10 K',
'filing_date': 'February 26, 2025',
'fiscal_year': 2025,
'unit': 'millions',
'revenue': 130497
}查看引用与推理
- python
- typescript
- python-legacy
print(result.extraction_metadata)console.log(result.extraction_metadata);print(filing_info.extraction_metadata)你看到的结构应类似于:
{'field_metadata': {'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.'},
{'page': 35,
'matching_text': 'Comparison of 5 Year Cumulative Total Return* Among NVIDIA Corporation'},
{'page': 49,
'matching_text': 'To the Board of Directors and Shareholders of NVIDIA Corporation'},
{'page': 90, 'matching_text': 'NVIDIA Corporation'},
{'page': 119,
'matching_text': '*"Company"* means NVIDIA Corporation, a Delaware corporation.'},
{'page': 126,
'matching_text': 'Annual Report on Form 10-K of NVIDIA Corporation'}]},
'filing_type': {'reasoning': "VERBATIM EXTRACTION from multiple sources confirming the filing type as '10 K'.",
'citation': [{'page': 1, 'matching_text': 'FORM 10-K'},
{'page': 2, 'matching_text': 'Item 16. | Form 10-K Summary'},
{'page': 3,
'matching_text': 'This Annual Report on Form 10-K contains forward-looking statements...'},
{'page': 13, 'matching_text': 'this Annual Report on Form 10-K'},
{'page': 15, 'matching_text': 'this Annual Report on Form 10-K'},
{'page': 32,
'matching_text': 'Annual Report on Form 10-K, which information is hereby incorporated by reference.'},
{'page': 36, 'matching_text': 'this Annual Report on Form 10-K'},
{'page': 43,
'matching_text': 'Annual Report on Form 10-K for additional information'},
{'page': 45, 'matching_text': 'Annual Report on Form 10-K'},
{'page': 46, 'matching_text': 'this Annual Report on Form 10-K'},
{'page': 62, 'matching_text': 'Annual Report on Form 10-K'},
{'page': 83,
'matching_text': 'Restated Certificate of Incorporation | 10-K'},
{'page': 84, 'matching_text': 'Item 16. Form 10-K Summary'},
{'page': 126, 'matching_text': 'which appears in this Form 10-K'},
{'page': 127, 'matching_text': 'Annual Report on Form 10-K'},
{'page': 128, 'matching_text': 'Annual Report on Form 10-K'},
{'page': 129, 'matching_text': "The Company's Annual Report on Form 10-K"},
{'page': 130,
'matching_text': "The Company's Annual Report on Form 10-K for the year ended January 26, 2025"}]},
'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'},
{'page': 20,
'matching_text': 'we generated 53% of our revenue in fiscal year 2025 from sales outside the United States.'},
{'page': 23,
'matching_text': 'For fiscal year 2025, an indirect customer which primarily purchases our products through system integrators...'},
{'page': 33,
'matching_text': 'In fiscal year 2025, we repurchased 310 million shares of our common stock for $34.0 billion.'},
{'page': 37,
'matching_text': 'Our Data Center revenue in China grew in fiscal year 2025.'},
{'page': 44,
'matching_text': 'Cash provided by operating activities increased in fiscal year 2025 compared to fiscal year 2024'},
{'page': 57,
'matching_text': 'Fiscal years 2025, 2024 and 2023 were all 52-week years.'},
{'page': 65,
'matching_text': 'Beginning in the second quarter of fiscal year 2025'},
{'page': 69, 'matching_text': 'In the fourth quarter of fiscal year 2025'},
{'page': 78,
'matching_text': 'Depreciation and amortization expense attributable to our Compute and Networking segment for fiscal years 2025'},
{'page': 129, 'matching_text': 'for the year ended January 26, 2025'}]},
'description': {'reasoning': 'The extracted data combines multiple descriptions from the source text, ensuring no duplication while maintaining the order and context of the information. Each section of the filing is summarized to reflect the key points without losing the essence of the original text.',
'citation': [{'page': 4,
'matching_text': 'NVIDIA is now a full-stack computing infrastructure company with data-center-scale offerings that are reshaping industry.'},
{'page': 8,
'matching_text': 'a suite of technologies that help developers bring digital avatars to life with generative Al...autonomous vehicles, or AV, and electric vehicles, or EV, is revolutionizing the transportation industry...Our worldwide sales and marketing strategy is key to achieving our objective of providing markets with our high-performance and efficient computing platforms and software.'},
{'page': 14, 'matching_text': 'Risk Factors Summary'},
{'page': 16,
'matching_text': 'Risks Related to Demand, Supply, and Manufacturing\n\nLong manufacturing lead times and uncertain supply and component availability...'},
{'page': 18,
'matching_text': 'cryptocurrency mining, on demand for our products. Volatility in the cryptocurrency market, including new compute technologies...'},
{'page': 21,
'matching_text': 'supply-chain attacks or other business disruptions. We cannot guarantee that third parties and infrastructure in our supply chain...'},
{'page': 22,
'matching_text': 'We are monitoring the impact of the geopolitical conflict in and around Israel on our operations... Climate change may have a long-term impact on our business.'},
{'page': 25,
'matching_text': 'We are subject to complex laws, rules, regulations, and political and other actions, including restrictions on the export of our products, which may adversely impact our business.'},
{'page': 28,
'matching_text': 'Our competitive position has been harmed by the existing export controls, and our competitive position and future results may be further harmed'},
{'page': 29,
'matching_text': 'restrictions imposed by the Chinese government on the duration of gaming activities and access to games may adversely affect our Gaming revenue'},
{'page': 29,
'matching_text': 'our business depends on our ability to receive consistent and reliable supply from our overseas partners, especially in Taiwan and South Korea'},
{'page': 29,
'matching_text': 'Increased scrutiny from shareholders, regulators and others regarding our corporate sustainability practices could result in additional costs'},
{'page': 29,
'matching_text': 'Concerns relating to the responsible use of new and evolving technologies, such as Al, in our products and services may result in reputational or financial harm'},
{'page': 31,
'matching_text': 'Data protection laws around the world are quickly changing and may be interpreted and applied in an increasingly stringent fashion...'}]},
'filing_date': {'reasoning': 'The filing date is consistently mentioned as February 26, 2025 across multiple entries, making it the most reliable date for the filing.',
'citation': [{'page': 51, 'matching_text': 'February 26, 2025'},
{'page': 86, 'matching_text': 'on February 26, 2025.'},
{'page': 87, 'matching_text': 'February 26, 2025'},
{'page': 126, 'matching_text': 'our report dated February 26, 2025'},
{'page': 127, 'matching_text': 'Date: February 26, 2025'},
{'page': 128, 'matching_text': 'Date: February 26, 2025'},
{'page': 129, 'matching_text': 'Date: February 26, 2025'},
{'page': 130, 'matching_text': 'Date: February 26, 2025'}]},
'unit': {'reasoning': "The unit of financial figures is explicitly mentioned multiple times in the text as 'millions', including in table headers and notes. This is confirmed by various citations from pages 38, 42, 43, 52, 53, 54, 56, 65, 71, 72, 73, 75, 77, 79, 80, and 82.",
'citation': [{'page': 38,
'matching_text': '($ in millions, except per share data)'},
{'page': 42, 'matching_text': '($ in millions)'},
{'page': 43, 'matching_text': '($ in millions)'},
{'page': 52, 'matching_text': '(In millions, except per share data)'},
{'page': 53,
'matching_text': 'Consolidated Statements of Comprehensive Income (In millions)'},
{'page': 54,
'matching_text': 'Consolidated Balance Sheets (In millions, except par value)'},
{'page': 55, 'matching_text': '(In millions, except per share data)'},
{'page': 56,
'matching_text': 'Consolidated Statements of Cash Flows (In millions)'},
{'page': 65,
'matching_text': 'Year Ended<br/>Jan 26, 2025<br/>(In millions, except per share data)'},
{'page': 71, 'matching_text': '(In millions) | (In millions)'},
{'page': 72, 'matching_text': '(In millions)'}]},
'revenue': {'reasoning': 'The total revenue for fiscal year 2025 is extracted from multiple sources within the text, all confirming the same figure of $130,497 million. The revenue recognized for fiscal year 2025 is also noted as $4,607 million, which is a separate figure. However, the primary focus is on the total revenue figure, which is consistently cited.',
'citation': [{'page': 38,
'matching_text': 'Revenue for fiscal year 2025 was $130.5 billion'},
{'page': 41,
'matching_text': 'Total | $ 130,497 | $ | 60,922'},
{'page': 52, 'matching_text': 'Revenue | $ 130,497'},
{'page': 78,
'matching_text': 'Revenue | $ 116,193 | $ 14,304 | $ - | $ 130,497'},
{'page': 79, 'matching_text': 'Total revenue | $ 130,497'},
{'page': 80, 'matching_text': 'Total revenue | $ 130,497'}]}},
'usage': {'num_pages_extracted': 130,
'num_document_tokens': 105932,
'num_output_tokens': 31306}}下一步做什么?
本例我们搭建了一个抽取智能体,它能在抽取时标出自己所依据的文档位置,并对回答作出推理。想进一步定制并改善结果,你还可以定制 ExtractConfig 里的 system_prompt。