Skip to content
LlamaParse 文档版本 跟随上游 源快照 48d15ff5 更新于 2026-10-16

自定义提示词 让你像给 LLM 下指令一样引导 Parse 的智能体模型。

这些提示词在下列场景中很有用:提升解析器在复杂版式上的表现、按特定格式抽取数据,或对文档做其他形式的转换。

本例演示如何给 Parse 提供附加指令(提示词)来塑造 LLM 解析非结构化文档的方式。我们拿一张麦当劳小票,展示如何忽略文档的某些部分,只解析每样商品的单价与最终应付金额。

设置

把 LlamaCloud API key 设好,SDK 会自动读取:

终端窗口

export LLAMA_CLOUD_API_KEY="llx-..."

不加指令地解析小票

这个例子用的麦当劳小票如下:

把它下载为 mcdonalds_receipt.png:

终端窗口

curl -fL -o mcdonalds_receipt.png https://raw.githubusercontent.com/run-llama/llama_cloud_services/main/examples/parse/parsing_instructions/mcdonalds_receipt.png

先在不加任何特殊指令的情况下解析这张小票 —— 就一次默认的智能体解析:

  • Python
  • TypeScript
  • Go
  • Java
  • CLI

终端窗口

pip install "llama-cloud>=2.8"
from llama_cloud import LlamaCloud

client = LlamaCloud()  # reads LLAMA_CLOUD_API_KEY from the environment

# Upload the receipt
file = client.files.create(file="mcdonalds_receipt.png", purpose="parse")

# Parse with no special instructions
vanilla_result = client.parsing.parse(
    file_id=file.id,
    tier="agentic",
    version="latest",
    expand=["markdown"],
    output_options={
        "markdown": {"tables": {"output_tables_as_markdown": True}},
    },
)

print(vanilla_result.markdown.pages[0].markdown)

终端窗口

npm install @llamaindex/llama-cloud
import LlamaCloud from '@llamaindex/llama-cloud';
import fs from 'fs';

const client = new LlamaCloud(); // reads LLAMA_CLOUD_API_KEY from the environment

// Upload the receipt
const file = await client.files.create({
  file: fs.createReadStream('mcdonalds_receipt.png'),
  purpose: 'parse',
});

// Parse with no special instructions
const vanillaResult = await client.parsing.parse({
  file_id: file.id,
  tier: 'agentic',
  version: 'latest',
  expand: ['markdown'],
  output_options: {
    markdown: { tables: { output_tables_as_markdown: true } },
  },
});

console.log(vanillaResult.markdown.pages[0].markdown);

终端窗口

go get github.com/run-llama/llama-parse-go
package main

import (
  "context"
  "fmt"
  "log"
  "os"
  "time"

  llamacloud "github.com/run-llama/llama-parse-go"
)

func main() {
  ctx := context.Background()
  client := llamacloud.NewClient() // reads LLAMA_CLOUD_API_KEY from the environment

  f, err := os.Open("mcdonalds_receipt.png")
  if err != nil {
    log.Fatal(err)
  }
  defer f.Close()

  // Upload the receipt
  file, err := client.Files.New(ctx, llamacloud.FileNewParams{
    File:    f,
    Purpose: "parse",
  })
  if err != nil {
    log.Fatal(err)
  }

  // Parse with no special instructions
  job, err := client.Parsing.New(ctx, llamacloud.ParsingNewParams{
    FileID:  llamacloud.String(file.ID),
    Tier:    llamacloud.ParsingNewParamsTierAgentic,
    Version: llamacloud.ParsingNewParamsVersionLatest,
    OutputOptions: llamacloud.ParsingNewParamsOutputOptions{
      Markdown: llamacloud.ParsingNewParamsOutputOptionsMarkdown{
        Tables: llamacloud.ParsingNewParamsOutputOptionsMarkdownTables{
          OutputTablesAsMarkdown: llamacloud.Bool(true),
        },
      },
    },
  })
  if err != nil {
    log.Fatal(err)
  }

  // expand is a GET parameter — request markdown when you fetch the result
  getParams := llamacloud.ParsingGetParams{Expand: []string{"markdown"}}
  result, err := client.Parsing.Get(ctx, job.ID, getParams)
  if err != nil {
    log.Fatal(err)
  }
  for result.Job.Status != "COMPLETED" && result.Job.Status != "FAILED" && result.Job.Status != "CANCELLED" {
    time.Sleep(2 * time.Second)
    result, err = client.Parsing.Get(ctx, job.ID, getParams)
    if err != nil {
      log.Fatal(err)
    }
  }
  if result.Job.Status != "COMPLETED" {
    log.Fatalf("parse ended as %s", result.Job.Status)
  }

  if len(result.Markdown.Pages) == 0 {
    log.Fatal("parse completed with no markdown pages")
  }

  fmt.Println(result.Markdown.Pages[0].Markdown)
}
implementation("ai.llamaindex:llama-cloud:1.3.0")
import ai.llamaindex.llamacloud.client.LlamaCloudClient;
import ai.llamaindex.llamacloud.client.okhttp.LlamaCloudOkHttpClient;
import ai.llamaindex.llamacloud.models.files.FileCreateParams;
import ai.llamaindex.llamacloud.models.files.FileCreateResponse;
import ai.llamaindex.llamacloud.models.parsing.ParsingCreateParams;
import ai.llamaindex.llamacloud.models.parsing.ParsingCreateResponse;
import ai.llamaindex.llamacloud.models.parsing.ParsingGetParams;
import ai.llamaindex.llamacloud.models.parsing.ParsingGetResponse;
import java.nio.file.Paths;

public class ParseReceipt {
    public static void main(String[] args) throws Exception {
        // reads LLAMA_CLOUD_API_KEY from the environment
        LlamaCloudClient client = LlamaCloudOkHttpClient.fromEnv();

        // Upload the receipt
        FileCreateResponse file = client.files().create(FileCreateParams.builder()
                .file(Paths.get("mcdonalds_receipt.png"))
                .purpose("parse")
                .build());

        // Parse with no special instructions
        ParsingCreateResponse job = client.parsing().create(ParsingCreateParams.builder()
                .fileId(file.id())
                .tier(ParsingCreateParams.Tier.AGENTIC)
                .version(ParsingCreateParams.Version.LATEST)
                .outputOptions(ParsingCreateParams.OutputOptions.builder()
                        .markdown(ParsingCreateParams.OutputOptions.Markdown.builder()
                                .tables(ParsingCreateParams.OutputOptions.Markdown.Tables.builder()
                                        .outputTablesAsMarkdown(true)
                                        .build())
                                .build())
                        .build())
                .build());

        // expand is a query parameter — request markdown when you fetch the result
        ParsingGetParams getParams = ParsingGetParams.builder()
                .jobId(job.id())
                .addExpand("markdown")
                .build();

        ParsingGetResponse result = client.parsing().get(getParams);
        while (!result.job().status().equals(ParsingGetResponse.Job.Status.COMPLETED)
                && !result.job().status().equals(ParsingGetResponse.Job.Status.FAILED)
                && !result.job().status().equals(ParsingGetResponse.Job.Status.CANCELLED)) {
            Thread.sleep(2000);
            result = client.parsing().get(getParams);
        }
        if (!result.job().status().equals(ParsingGetResponse.Job.Status.COMPLETED)) {
            throw new RuntimeException("parse ended as " + result.job().status());
        }

        System.out.println(result.markdown().get().pages().get(0).asMarkdownResult().markdown());
    }
}

终端窗口

go install github.com/run-llama/llama-parse-cli/cmd/llp@latest

终端窗口

# Upload the receipt
FILE_ID=$(llp files create \
  --file mcdonalds_receipt.png \
  --purpose parse | jq -r '.id')

# Start a parse job with no special instructions
JOB_ID=$(llp parsing create \
  --file-id "$FILE_ID" \
  --tier agentic \
  --version latest \
  --output-options.markdown '{tables: {output_tables_as_markdown: true}}' | jq -r '.id')

# Poll until the job reaches a terminal status
while true; do
  STATUS=$(llp parsing get --job-id "$JOB_ID" | jq -er '.job.status') || break
  case "$STATUS" in COMPLETED|FAILED|CANCELLED) break ;; esac
  sleep 2
done

# Print the markdown for the first page
llp parsing get --job-id "$JOB_ID" --expand markdown \
  | jq -r 'if .job.status == "COMPLETED" then .markdown.pages[0].markdown
    else "Parse job failed: \(.job.error_message // .)" end'

结果是 Parse 从小票还原出的完整 markdown:

> Rate us HIGHLY SATISFIED and
> Receive ONE FREE ITEM
> Purchase any sandwich and receive an item of equal or lesser value
> Go to www.mcdvoice.com within 7 days and tell us about your visit.
> Validation Code:
> Expires 30 days after receipt date.
> Valid at participating US McDonald's.
> Survey Code:
> 31278-01121-21018-20481-00081-0

## McDonald's Restaurant #31278
2378 PINE RD NW
RICE, MN 56367-9740
TEL# 320 393 4600

| KS# 1           | 12/08/2022 08:48 PM |
|-----------------|---------------------|
| Side1           | Order 12            |

| Item                     | Price |
|--------------------------|-------|
| 1 Happy Meal 6 Pc        | 4.89  |
| - 1 Creamy Ranch Cup     |       |
| - 1 Extra Kids Fry       |       |
| - 1 Wreck It Ralph 2     |       |
| - 1 S Coke               |       |
| 1 Snack Oreo McFlurry    | 2.69  |

| Subtotal                 | 7.58  |
| Tax                      | 0.52  |
| Take-Out Total           | 8.10  |

| Cash Tendered            | 10.00 |
| Change                   | 1.90  |

> McDonalds Restaurant Rice
> ***NOW ACCEPTING APPLICATIONS***
> text to #36453
> apply31278

加指令解析小票

现在我们通过提供一段附加提示词来改变输出。自定义提示词放在 agentic_options 里 —— 在 cost_effective、agentic 与 agentic_plus 三个档位可用。下面每个标签页都复用上一页步骤里的 client 与已上传文件:

  • Python
  • TypeScript
  • Go
  • Java
  • CLI
parsing_instruction = (
    "The provided document is a McDonald's receipt. "
    "Provide ONLY each line item (item name and price) and the final amount to be paid."
)

result_with_prompt = client.parsing.parse(
    file_id=file.id,
    tier="agentic",
    version="latest",
    expand=["markdown"],
    output_options={
        "markdown": {"tables": {"output_tables_as_markdown": True}},
    },
    agentic_options={"custom_prompt": parsing_instruction},
)

print(result_with_prompt.markdown.pages[0].markdown)
const parsingInstruction =
  "The provided document is a McDonald's receipt. " +
  'Provide ONLY each line item (item name and price) and the final amount to be paid.';

const resultWithPrompt = await client.parsing.parse({
  file_id: file.id,
  tier: 'agentic',
  version: 'latest',
  expand: ['markdown'],
  output_options: {
    markdown: { tables: { output_tables_as_markdown: true } },
  },
  agentic_options: { custom_prompt: parsingInstruction },
});

console.log(resultWithPrompt.markdown.pages[0].markdown);
// Reuses ctx, client, and the uploaded file from above.
instruction := "The provided document is a McDonald's receipt. " +
  "Provide ONLY each line item (item name and price) and the final amount to be paid."

job, err := client.Parsing.New(ctx, llamacloud.ParsingNewParams{
  FileID:  llamacloud.String(file.ID),
  Tier:    llamacloud.ParsingNewParamsTierAgentic,
  Version: llamacloud.ParsingNewParamsVersionLatest,
  OutputOptions: llamacloud.ParsingNewParamsOutputOptions{
    Markdown: llamacloud.ParsingNewParamsOutputOptionsMarkdown{
      Tables: llamacloud.ParsingNewParamsOutputOptionsMarkdownTables{
        OutputTablesAsMarkdown: llamacloud.Bool(true),
      },
    },
  },
  AgenticOptions: llamacloud.ParsingNewParamsAgenticOptions{
    CustomPrompt: llamacloud.String(instruction),
  },
})
if err != nil {
  log.Fatal(err)
}

// expand is a GET parameter — request markdown when you fetch the result
getParams := llamacloud.ParsingGetParams{Expand: []string{"markdown"}}
result, err := client.Parsing.Get(ctx, job.ID, getParams)
if err != nil {
  log.Fatal(err)
}
for result.Job.Status != "COMPLETED" && result.Job.Status != "FAILED" && result.Job.Status != "CANCELLED" {
  time.Sleep(2 * time.Second)
  result, err = client.Parsing.Get(ctx, job.ID, getParams)
  if err != nil {
    log.Fatal(err)
  }
}
if result.Job.Status != "COMPLETED" {
  log.Fatalf("parse ended as %s", result.Job.Status)
}

if len(result.Markdown.Pages) == 0 {
  log.Fatal("parse completed with no markdown pages")
}

fmt.Println(result.Markdown.Pages[0].Markdown)
// Reuses client and the uploaded file from above.
String instruction = "The provided document is a McDonald's receipt. "
        + "Provide ONLY each line item (item name and price) and the final amount to be paid.";

ParsingCreateResponse job = client.parsing().create(ParsingCreateParams.builder()
        .fileId(file.id())
        .tier(ParsingCreateParams.Tier.AGENTIC)
        .version(ParsingCreateParams.Version.LATEST)
        .outputOptions(ParsingCreateParams.OutputOptions.builder()
                .markdown(ParsingCreateParams.OutputOptions.Markdown.builder()
                        .tables(ParsingCreateParams.OutputOptions.Markdown.Tables.builder()
                                .outputTablesAsMarkdown(true)
                                .build())
                        .build())
                .build())
        .agenticOptions(ParsingCreateParams.AgenticOptions.builder()
                .customPrompt(instruction)
                .build())
        .build());

// expand is a query parameter — request markdown when you fetch the result
ParsingGetParams getParams = ParsingGetParams.builder()
        .jobId(job.id())
        .addExpand("markdown")
        .build();

ParsingGetResponse result = client.parsing().get(getParams);
while (!result.job().status().equals(ParsingGetResponse.Job.Status.COMPLETED)
        && !result.job().status().equals(ParsingGetResponse.Job.Status.FAILED)
        && !result.job().status().equals(ParsingGetResponse.Job.Status.CANCELLED)) {
    Thread.sleep(2000);
    result = client.parsing().get(getParams);
}
if (!result.job().status().equals(ParsingGetResponse.Job.Status.COMPLETED)) {
    throw new RuntimeException("parse ended as " + result.job().status());
}

System.out.println(result.markdown().get().pages().get(0).asMarkdownResult().markdown());

终端窗口

# Reuses $FILE_ID from above. The custom prompt goes on --agentic-options.custom-prompt.
JOB_ID=$(llp parsing create \
  --file-id "$FILE_ID" \
  --tier agentic \
  --version latest \
  --output-options.markdown '{tables: {output_tables_as_markdown: true}}' \
  --agentic-options.custom-prompt "The provided document is a McDonald's receipt. Provide ONLY each line item (item name and price) and the final amount to be paid." \
  | jq -r '.id')

# Poll until the job reaches a terminal status
while true; do
  STATUS=$(llp parsing get --job-id "$JOB_ID" | jq -er '.job.status') || break
  case "$STATUS" in COMPLETED|FAILED|CANCELLED) break ;; esac
  sleep 2
done

# Print the markdown for the first page
llp parsing get --job-id "$JOB_ID" --expand markdown \
  | jq -r 'if .job.status == "COMPLETED" then .markdown.pages[0].markdown
    else "Parse job failed: \(.job.error_message // .)" end'

这段提示词把输出收窄到只剩逐项明细与总额:

* Happy Meal 6 Pc 4.89
* Snack Oreo McFlurry 2.69

Take-Out Total 8.10

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。