llamaparse
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseLlamaParse Skill
LlamaParse Skill
Parse unstructured documents (such as PDF, DOCX, PPTX, XLSX) with LlamaParse and extract their contents (text, markdown, images...).
使用LlamaParse解析非结构化文档(如PDF、DOCX、PPTX、XLSX)并提取其内容(文本、Markdown、图片等)。
Initial Setup
初始设置
When this skill is invoked, respond with:
I'm ready to use LlamaParse to parse files. Before we begin, please confirm that:
- `LLAMA_CLOUD_API_KEY` is set as environment variable within the current environment
- `@llamaindex/llama-cloud@latest` is installed and available within the current Node environment
If both of them are set, please provide:
1. One or more files to be parsed
2. Specific parsing options, such as tier, API version, custom prompt, processing options...
3. Any requests you might have regarding the parsed content of the file.
I will produce a Typescript script to run the parsing job and, once you approved its execution, I will report the results back to you based on your request.Then wait for the user's input.
当调用此Skill时,请回复以下内容:
I'm ready to use LlamaParse to parse files. Before we begin, please confirm that:
- `LLAMA_CLOUD_API_KEY` is set as environment variable within the current environment
- `@llamaindex/llama-cloud@latest` is installed and available within the current Node environment
If both of them are set, please provide:
1. One or more files to be parsed
2. Specific parsing options, such as tier, API version, custom prompt, processing options...
3. Any requests you might have regarding the parsed content of the file.
I will produce a Typescript script to run the parsing job and, once you approved its execution, I will report the results back to you based on your request.然后等待用户输入。
Step 0 — Install llama-cloud
(optional)
llama-cloud步骤0 — 安装llama-cloud
(可选)
llama-cloudIf the user does not have the package installed, add it to the current environment by running:
@llamaindex/llama-cloudbash
npm install @llamaindex/llama-cloud@latest如果用户尚未安装包,请通过运行以下命令将其添加到当前环境中:
@llamaindex/llama-cloudbash
npm install @llamaindex/llama-cloud@latestStep 1 — Produce a Typescript Script
步骤1 — 生成Typescript脚本
Once the user confirms the environment variables are set and provides the necessary details for the parsing job, produce a typescript script.
As a source of truth for the TS script, you can:
- Refer to the example.ts script, which covers most of the necessary configurations for LlamaParse
- Refer to the complete LlamaParse Documentation, fetching the page.
https://developers.llamaindex.ai/python/cloud/llamaparse/api-v2-guide/
一旦用户确认环境变量已配置完成,并提供了解析任务所需的必要信息,即可生成一个Typescript脚本。
关于TS脚本的参考标准,你可以:
- 参考example.ts脚本,它涵盖了LlamaParse的大多数必要配置
- 参考完整的LlamaParse文档,访问页面。
https://developers.llamaindex.ai/python/cloud/llamaparse/api-v2-guide/
Scripting Best Practices
脚本编写最佳实践
Follow these guidelines when generating scripts:
生成脚本时请遵循以下准则:
1. Always Use the Top-Level LlamaCloud
Client
LlamaCloud1. 始终使用顶层LlamaCloud
客户端
LlamaCloudUse (the API client) for all parsing operations:
LlamaCloudtypescript
import LlamaCloud from "@llamaindex/llama-cloud";
// Define a client
const client = new LlamaCloud({
apiKey: process.env["LLAMA_CLOUD_API_KEY"], // This is the default and can be omitted
});
所有解析操作都使用(API客户端):
LlamaCloudtypescript
import LlamaCloud from "@llamaindex/llama-cloud";
// Define a client
const client = new LlamaCloud({
apiKey: process.env["LLAMA_CLOUD_API_KEY"], // This is the default and can be omitted
});
2. Two-Step Upload → Parse Pattern
2. 两步式「上传→解析」流程
Always upload first to get a file ID, then parse using the file ID. Never pass raw file bytes directly to .
parse()typescript
import { readFile, writeFile } from "fs/promises";
import { basename } from "path";
// 1. Convert the file path into a File object
const buffer = await readFile(filePath);
const fileName = basename(filePath);
const file = new File([buffer], fileName);
// 2. Upload the file to the cloud
const fileObj = await client.files.create({
file: file,
purpose: "parse",
});
// 3. Get the file ID
const fileId = fileObj.id;
// 4. Use the file ID to parse the file
const result = await client.parsing.parse({
tier: "agentic",
version: "latest",
file_id: fileId,
...
});If the user already has a file ID (e.g. from a prior upload), skip the upload step and use it directly.
始终先上传文件获取文件ID,再使用该文件ID进行解析。切勿直接将原始文件字节传递给方法。
parse()typescript
import { readFile, writeFile } from "fs/promises";
import { basename } from "path";
// 1. Convert the file path into a File object
const buffer = await readFile(filePath);
const fileName = basename(filePath);
const file = new File([buffer], fileName);
// 2. Upload the file to the cloud
const fileObj = await client.files.create({
file: file,
purpose: "parse",
});
// 3. Get the file ID
const fileId = fileObj.id;
// 4. Use the file ID to parse the file
const result = await client.parsing.parse({
tier: "agentic",
version: "latest",
file_id: fileId,
...
});如果用户已拥有文件ID(例如来自之前的上传),则跳过上传步骤,直接使用该ID。
3. Choose the Right Tier
3. 选择合适的服务层级
| Tier | When to Use |
|---|---|
| Speed is the priority; simple documents |
| Budget-conscious; straightforward text extraction |
| Complex layouts, tables, mixed content (default recommendation) |
| Advanced analysis, highest accuracy |
Default to unless the user specifies otherwise or the document is simple.
agentic| 层级 | 使用场景 |
|---|---|
| 优先考虑速度;适用于简单文档 |
| 注重成本控制;适用于直接提取文本 |
| 复杂布局、表格、混合内容(默认推荐) |
| 高级分析、最高精度 |
除非用户指定或文档内容简单,否则默认使用层级。
agentic4. Always Include the expand
Parameter
expand4. 始终包含expand
参数
expandThe parameter controls what content is returned. Omitting it returns minimal data. Always specify exactly what you need:
expand| Value | Returns |
|---|---|
| Plain text via |
| Markdown via |
| Page-level JSON via |
| Per-page text metadata |
| Per-page markdown metadata |
| Per-page items metadata |
| Image list with presigned URLs |
| Output PDF metadata |
| Excel-specific metadata |
Only request metadata variants when you need presigned URLs or per-page detail — they increase payload size.
*_content_metadataexpand| 值 | 返回内容 |
|---|---|
| 纯文本,通过 |
| Markdown格式内容,通过 |
| 按页划分的JSON数据,通过 |
| 每页文本的元数据 |
| 每页Markdown内容的元数据 |
| 每页条目内容的元数据 |
| 包含预签名URL的图片列表 |
| 输出PDF的元数据 |
| Excel专属元数据 |
仅当你需要预签名URL或每页详细信息时,才请求类的元数据——它们会增加负载大小。
*_content_metadata5. Handle None Results Defensively
5. 防御性处理空结果
result.text_fullresult.markdown_fullresult.itemsundefinedtypescript
const text = result.text_full ?? "";
const markdown = result.markdown_full ?? "";解析失败时,、和可能为。请始终对此进行防护:
result.text_fullresult.markdown_fullresult.itemsundefinedtypescript
const text = result.text_full ?? "";
const markdown = result.markdown_full ?? "";6. Use Structured Options for Advanced Configuration
6. 使用结构化选项进行高级配置
Group options using the correct nested keys:
typescript
const result = await client.parsing.parse({
tier: "agentic",
version: "latest",
file_id: fileId,
input_options: {
presentation: {
skip_embedded_data: false,
},
},
output_options: {
images_to_save: ["screenshot"],
markdown: {
tables: { output_tables_as_markdown: true },
annotate_links: true,
},
},
processing_options: {
specialized_chart_parsing: "agentic",
ocr_parameters: { languages: ["de", "en"] },
},
agentic_options: {
custom_prompt:
"Extract text from the provided file and translate it from German to English.",
},
expand: [
"markdown_full",
"images_content_metadata",
"markdown_content_metadata",
],
});Use whenever the user wants to guide extraction (translation, summarization, structured extraction, etc.).
agentic_options.custom_prompt使用正确的嵌套键对选项进行分组:
typescript
const result = await client.parsing.parse({
tier: "agentic",
version: "latest",
file_id: fileId,
input_options: {
presentation: {
skip_embedded_data: false,
},
},
output_options: {
images_to_save: ["screenshot"],
markdown: {
tables: { output_tables_as_markdown: true },
annotate_links: true,
},
},
processing_options: {
specialized_chart_parsing: "agentic",
ocr_parameters: { languages: ["de", "en"] },
},
agentic_options: {
custom_prompt:
"Extract text from the provided file and translate it from German to English.",
},
expand: [
"markdown_full",
"images_content_metadata",
"markdown_content_metadata",
],
});当用户需要引导提取操作(翻译、摘要、结构化提取等)时,请使用。
agentic_options.custom_prompt7. Downloading Images Requires httpx
and Auth
httpx7. 下载图片需要httpx
和身份验证
httpxWhen is in , download images via presigned URLs with Bearer auth:
images_content_metadataexpandtypescript
if (result.images_content_metadata) {
for (const image of result.images_content_metadata.images) {
if (image.presigned_url) {
const response = await fetch(image.presigned_url, {
headers: {
Authorization: `Bearer ${process.env["LLAMA_CLOUD_API_KEY"]}`,
},
});
if (response.ok) {
const content = await response.bytes();
await writeFile(image.filename, content);
}
}
}
}当参数中包含时,需要使用Bearer身份验证通过预签名URL下载图片:
expandimages_content_metadatatypescript
if (result.images_content_metadata) {
for (const image of result.images_content_metadata.images) {
if (image.presigned_url) {
const response = await fetch(image.presigned_url, {
headers: {
Authorization: `Bearer ${process.env["LLAMA_CLOUD_API_KEY"]}`,
},
});
if (response.ok) {
const content = await response.bytes();
await writeFile(image.filename, content);
}
}
}
}8. Use the Node shebang
8. 添加Node Shebang
Every generated script should include the node shebang:
typescript
#!/usr/bin/env node每个生成的脚本都应包含Node shebang:
typescript
#!/usr/bin/env nodeStep 2 — Execute the Typescript Script
步骤2 — 执行Typescript脚本
Once the typescript script has been produced, you should:
- Present the script to the user and ask for permissions to run it (depending on the current permissions settings)
- Once you obtained permission to run, execute the script
- Explore the results based on the user's requests
In order to run typescript scripts, it is highly recommended to use:.npx tsx script.ts
生成Typescript脚本后,你需要:
- 将脚本呈现给用户,并请求运行权限(取决于当前的权限设置)
- 获取运行权限后,执行该脚本
- 根据用户的请求分析解析结果
为了运行Typescript脚本,强烈建议使用:。npx tsx script.ts