使用 LlamaParse API (LlamaCloud) 解析、提取和分析文档。当用户要求解析 PDF、图片、电子表格或其他文档时使用。
LlamaParse. 帕塞文档(PDFs,图像,电子表格,演示文稿——130+格式),以LLM备妥文本,标记,以及使用LlamaParse API的结构数据.是一项面向实际任务的技能,主要用于Prerequisites.;Python 包。
从功能定位来看,该技能强调把分散的操作要求整理成清晰、可复用的处理流程,使用户能够围绕既定目标快速准备输入、选择执行方式并获得结构化结果。实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;
若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;涉及批量任务时,还应保存进度,避免中断后重复操作。
使用 LlamaParse API 将文档(PDF、图像、电子表格、演示文稿等,支持 130+ 种格式)解析为面向大语言模型(LLM)优化的文本、Markdown 和结构化数据。
llama-cloud>=1.0(执行 pip install llama-cloud 安装)LLAMA_CLOUD_API_KEY。密钥可在 https://cloud.llamaindex.ai 获取验证配置是否就绪:
pip install llama-cloud>=1.0 export LLAMA_CLOUD_API_KEY=llx-...
from llama_cloud import AsyncLlamaCloud
import asyncio
async def parse_document(file_path: str):
client = AsyncLlamaCloud() # 自动读取 LLAMA_CLOUD_API_KEY 环境变量
file = await client.files.create(file=file_path, purpose="parse")
result = await client.parsing.parse(
file_id=file.id,
tier="agentic",
version="latest",
expand=["markdown", "text"],
)
return result
result = asyncio.run(parse_document("document.pdf"))
print(result.markdown.pages[0].markdown)
| 层级 | 适用场景 | 成本 |
|---|---|---|
agentic_plus |
最高精度,适用于复杂版式、图表等高难度内容 | 最高 |
agentic |
采用智能 Agent 的高级解析能力 | 中高 |
cost_effective |
性能与成本的平衡方案 | 中等 |
fast |
最快解析速度,基础级解析能力 | 最低 |
必须同时指定 tier 和 version 参数。version="latest" 适用于开发阶段;生产环境中建议使用具体日期字符串(例如 "2026-01-08"),以确保结果可复现。
expand 参数)可在 expand 列表中请求一个或多个输出视图:
markdown —— 结构化 Markdown,含标题、列表、表格等,适用于 RAG/LLM 流水线。text —— 每页清洗后的扁平化纯文本,适用于搜索与检索任务。items —— 页面元素(标题、段落、表格、图表等)构成的结构化树形对象,并附带每个元素的边界框(bounding box)。适用于需感知版式布局的处理场景。metadata —— 文档元数据。images_content_metadata —— 图像/截图元数据,包含预签名 URL(presigned URLs)。访问结果方式示例:result.markdown.pages[i].markdown、result.text.pages[i].text、result.items.pages[i].items
控制 Markdown 渲染行为:
output_options={
"markdown": {
"tables": {
"output_tables_as_markdown": True, # 或设为 False 以输出 HTML 表格
},
},
"images_to_save": ["screenshot"], # 保存页面截图
}
processing_options={
"ignore": {"ignore_diagonal_text": True},
"ocr_parameters": {"languages": ["en"]}, # OCR 语言提示
"specialized_chart_parsing": "agentic_plus", # 将图表解析为结构化数据
}
以类似向 LLM 提示的方式引导解析器,适用于提取特定字段或转换输出格式:
from llama_cloud.types.parsing_create_params import (
ProcessingOptions, ProcessingOptionsAutoModeConfiguration,
ProcessingOptionsAutoModeConfigurationParsingConf
)
result = await client.parsing.parse(
file_id=file.id,
tier="agentic",
version="latest",
expand=["markdown"],
processing_options=ProcessingOptions(
auto_mode_configuration=[ProcessingOptionsAutoModeConfiguration(
parsing_conf=ProcessingOptionsAutoModeConfigurationParsingConf(
custom_prompt="仅从此收据中提取价格和总计金额。"
)
)]
),
)
使用 scripts/parse_document.py 脚本:
python scripts/parse_document.py document.pdf --tier agentic --output markdown,text
使用 scripts/batch_parse.py 脚本:
python scripts/batch_parse.py ./documents/ --tier agentic --max-concurrent 5
在 expand 中请求 items,然后筛选出表格类元素:
for page in result.items.pages:
for item in page.items:
if hasattr(item, 'rows'): # 表格元素
print(f"第 {page.page_number} 页表格:共 {len(item.rows)} 行")
# 可通过 item.csv、item.html、item.md 获取不同格式的表格内容
启用专用图表解析功能,并从图表所在页面中提取表格行数据:
result = await client.parsing.parse(
file_id=file.id,
tier="agentic_plus",
version="latest",
processing_options={"specialized_chart_parsing": "agentic_plus"},
expand=["items"],
)
import httpx, re
result = await client.parsing.parse(
file_id=file.id, tier="agentic", version="latest",
output_options={"images_to_save": ["screenshot"]},
expand=["images_content_metadata"],
)
for img in result.images_content_metadata.images:
if img.presigned_url and re.match(r"^page_d+.jpg$", img.filename):
async with httpx.AsyncClient() as http:
resp = await http.get(img.presigned_url)
with open(img.filename, "wb") as f:
f.write(resp.content)
完整 API 接口详情请参阅 references/api-reference.md。
本功能依赖 LlamaParse API(https://cloud.llamaindex.ai),该服务由 LlamaIndex 提供,是一款云端文档解析服务。
LLAMA_CLOUD_API_KEY,密钥请在 https://cloud.llamaindex.ai 获取。api.cloud.llamaindex.ai 通信;截图下载使用同一服务提供的预签名 URL。scripts/parse_document.py 和 scripts/batch_parse.py 是辅助脚本,仅供用户手动执行,不会被本功能自动调用。expand 视图 —— 请求越多,响应体积越大、延迟越高。agentic_plus 层级并启用 specialized_chart_parsing。version(如 "2026-01-08"),避免使用 "latest"。items 视图为每个元素提供边界框坐标(b_box),适用于空间分析等高级用途。相关专题
热门下载
相关下载
精品课程
共6课时 | 54.6万人学习
共89课时 | 133.2万人学习
共49课时 | 82.1万人学习