跳转到内容

使用附加提示进行解析

解析提示词 允许您以指导大型语言模型的相同方式,来引导我们的解析模型。

这些提示可用于提升解析器在复杂文档布局上的性能、以特定格式提取数据,或以其他方式转换文档。

在这个示例中,我们展示了如何通过向LlamaParse提供额外指令(提示)来塑造大型语言模型从非结构化文档中解析信息的方式。 使用麦当劳收据作为示例,我们演示了如何忽略文档的某些部分,仅解析每个订单的价格以及最终应付金额。

首先,我们设置环境并连接到LlamaCloud:

Terminal window
!pip install llama-cloud-services
import os
from getpass import getpass
os.environ["LLAMA_CLOUD_API_KEY"] = getpass("Llama Cloud API Key: ")

对于这个示例,我们使用以下麦当劳收据。请下载并将其保存为 mcdonalds_receipt.png

我们首先初始化 LlamaParse,不包含任何指令:

from llama_cloud_services import LlamaParse
parser = LlamaParse(
parse_mode="parse_page_with_agent",
model="openai-gpt-4-1-mini",
high_res_ocr=True,
outlined_table_extraction=True,
output_tables_as_HTML=True,
)

我们得到的结果如下:

vanilla_result = await parser.aparse("mcdonalds_receipt.png")
print(vanilla_result.pages[0].md)
Started parsing the file under job_id fa0c25a4-999f-439a-81a2-54f024ce2809
> 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

现在让我们看看通过提供额外提示词来改变输出的方式:

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."""
parser = LlamaParse(
parse_mode="parse_page_with_agent",
model="openai-gpt-4-1-mini",
high_res_ocr=True,
outlined_table_extraction=True,
output_tables_as_HTML=True,
# Inject the parsing instruction into the user prompt
user_prompt=parsing_instruction,
)
result_with_prompt = await parser.aparse("mcdonalds_receipt.png")
print(result_with_prompt.pages[0].md)

结果如下:

Started parsing the file under job_id 4c6a6443-0590-4384-b84d-65e4455f5e48
* Happy Meal 6 Pc 4.89
* Snack Oreo McFlurry 2.69
Take-Out Total 8.10