Skip to content

Commit 4eafde9

Browse files
clarify strengths and expectations in the sample
1 parent 7e0cf93 commit 4eafde9

File tree

1 file changed

+92
-22
lines changed

1 file changed

+92
-22
lines changed

notebooks/field_extraction_pro_mode.ipynb

Lines changed: 92 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
"## Prerequisites\n",
3030
"1. Ensure Azure AI service is configured following [steps](../README.md#configure-azure-ai-service-resource)\n",
3131
"1. If using reference documents, please follow [Set env for reference doc](../docs/set_env_for_training_data_and_reference_doc.md) to set up `REFERENCE_DOC_SAS_URL` and `REFERENCE_DOC_PATH` in the [.env](./.env) file.\n",
32-
" - `REFERENCE_DOC_SAS_URL`: SAS URL for your Azure Blob container. \n",
33-
" - `REFERENCE_DOC_PATH`: Folder path within the container for uploading reference docs. \n",
32+
" - `REFERENCE_DOC_SAS_URL`: SAS URL for your Azure Blob container.\n",
33+
" - `REFERENCE_DOC_PATH`: Folder path within the container for uploading reference docs.\n",
34+
" > ⚠️ Note: Reference documents are optional in Pro mode. You can run Pro mode using just input documents. For example, the service can reason across two or more input files even without any reference data.\n",
3435
"1. Install the required packages to run the sample."
3536
]
3637
},
@@ -50,7 +51,7 @@
5051
"## Analyzer template and local files setup\n",
5152
"- **analyzer_template**: In this sample we define an analyzer template for invoice-contract verification.\n",
5253
"- **input_docs**: We can have multiple input document files in one folder or designate a single document file location. \n",
53-
"- **reference_docs**: During analyzer creation, we can provide documents that can aid in providing context that the analyzer references at inference time. We will get OCR results for these files if needed, generate a reference JSONL file, and upload these files to a designated Azure blob storage.\n",
54+
"- **reference_docs(Optional)**: During analyzer creation, we can provide documents that can aid in providing context that the analyzer references at inference time. We will get OCR results for these files if needed, generate a reference JSONL file, and upload these files to a designated Azure blob storage.\n",
5455
"\n",
5556
"> For example, if you're looking to analyze invoices to ensure they're consistent with a contractual agreement, you can supply the invoice and other relevant documents (for example, a purchase order) as inputs, and supply the contract files as reference data. The service applies reasoning to validate the input documents according to your schema, which might be to identify discrepancies to flag for further review."
5657
]
@@ -64,6 +65,8 @@
6465
"# Define paths for analyzer template, input documents, and reference documents\n",
6566
"analyzer_template = \"../analyzer_templates/invoice_contract_verification_pro_mode.json\"\n",
6667
"input_docs = \"../data/field_extraction_pro_mode/invoice_contract_verification/input_docs\"\n",
68+
"\n",
69+
"# NOTE: Reference documents are optional in Pro mode. Can comment out below line if not using reference documents.\n",
6770
"reference_docs = \"../data/field_extraction_pro_mode/invoice_contract_verification/reference_docs\""
6871
]
6972
},
@@ -158,7 +161,8 @@
158161
"- Generate a reference `.jsonl` file.\n",
159162
"- Upload these files to the designated Azure blob storage.\n",
160163
"\n",
161-
"We use **REFERENCE_DOC_SAS_URL** and **REFERENCE_DOC_PATH** that's set in the Prerequisites step."
164+
"We use **REFERENCE_DOC_SAS_URL** and **REFERENCE_DOC_PATH** that's set in the Prerequisites step.\n",
165+
"\n"
162166
]
163167
},
164168
{
@@ -169,11 +173,26 @@
169173
"source": [
170174
"# Load reference storage configuration from environment\n",
171175
"REFERENCE_DOC_SAS_URL = os.getenv(\"REFERENCE_DOC_SAS_URL\")\n",
172-
"REFERENCE_DOC_PATH = os.getenv(\"REFERENCE_DOC_PATH\")\n",
173-
"\n",
176+
"REFERENCE_DOC_PATH = os.getenv(\"REFERENCE_DOC_PATH\")"
177+
]
178+
},
179+
{
180+
"cell_type": "markdown",
181+
"metadata": {},
182+
"source": [
183+
"> ⚠️ Note: Reference documents are optional in Pro mode. You can run Pro mode using just input documents. For example, the service can reason across two or more input files even without any reference data. Please skip or comment out below section to skip the preparation of reference documents."
184+
]
185+
},
186+
{
187+
"cell_type": "code",
188+
"execution_count": null,
189+
"metadata": {},
190+
"outputs": [],
191+
"source": [
174192
"# Set skip_analyze to True if you already have OCR results for the documents in the reference_docs folder\n",
175193
"# Please name the OCR result files with the same name as the original document files including its extension, and add the suffix \".result.json\"\n",
176194
"# For example, if the original document is \"invoice.pdf\", the OCR result file should be named \"invoice.pdf.result.json\"\n",
195+
"# NOTE: Please comment out the follwing line if you don't have any reference documents.\n",
177196
"await client.generate_knowledge_base_on_blob(reference_docs, REFERENCE_DOC_SAS_URL, REFERENCE_DOC_PATH, skip_analyze=False)"
178197
]
179198
},
@@ -291,7 +310,14 @@
291310
"metadata": {},
292311
"source": [
293312
"## Bonus sample\n",
294-
"We would like to introduce another sample with multiple inputs."
313+
"We would like to introduce another sample to highlight how Pro mode supports multi-document input and advanced reasoning. Unlike Document Standard Mode, which processes one document at a time, Pro mode can analyze multiple documents within a single analysis call. With Pro mode, the service not only processes each document independently, but also cross-references the documents to perform reasoning across them, enabling deeper insights and validation."
314+
]
315+
},
316+
{
317+
"cell_type": "markdown",
318+
"metadata": {},
319+
"source": [
320+
"### First, we need to set up variables for the second sample"
295321
]
296322
},
297323
{
@@ -300,8 +326,6 @@
300326
"metadata": {},
301327
"outputs": [],
302328
"source": [
303-
"# First, we need to set up variables for the second sample\n",
304-
"\n",
305329
"# Define paths for analyzer template, input documents, and reference documents of the second sample\n",
306330
"analyzer_template_2 = \"../analyzer_templates/insurance_claims_review_pro_mode.json\"\n",
307331
"input_docs_2 = \"../data/field_extraction_pro_mode/insurance_claims_review/input_docs\"\n",
@@ -310,14 +334,41 @@
310334
"# Load reference storage configuration from environment\n",
311335
"REFERENCE_DOC_SAS_URL_2 = os.getenv(\"REFERENCE_DOC_SAS_URL\") # Reuse the same blob container\n",
312336
"REFERENCE_DOC_PATH_2 = os.getenv(\"REFERENCE_DOC_PATH\").rstrip(\"/\") + \"_2/\" # NOTE: Use a different path for the second sample\n",
313-
"CUSTOM_ANALYZER_ID_2 = \"pro-mode-sample-\" + str(uuid.uuid4())\n",
314-
"\n",
315-
"# Let's try reference docuemnts with existing OCR results for the second sample\n",
337+
"CUSTOM_ANALYZER_ID_2 = \"pro-mode-sample-\" + str(uuid.uuid4())"
338+
]
339+
},
340+
{
341+
"cell_type": "markdown",
342+
"metadata": {},
343+
"source": [
344+
"### Generate knowledge base for the second sample\n",
345+
"Let's try reference docuemnts with existing OCR results for the second sample"
346+
]
347+
},
348+
{
349+
"cell_type": "code",
350+
"execution_count": null,
351+
"metadata": {},
352+
"outputs": [],
353+
"source": [
316354
"logging.info(\"Start generating knowledge base for the second sample...\")\n",
317-
"await client.generate_knowledge_base_on_blob(reference_docs_2, REFERENCE_DOC_SAS_URL_2, REFERENCE_DOC_PATH_2, skip_analyze=True)\n",
318-
"\n",
319-
"# We can reuse previous AzureContentUnderstandingClient\n",
320-
"logging.info(\"Start creating analyzer for the second sample...\")\n",
355+
"await client.generate_knowledge_base_on_blob(reference_docs_2, REFERENCE_DOC_SAS_URL_2, REFERENCE_DOC_PATH_2, skip_analyze=True)"
356+
]
357+
},
358+
{
359+
"cell_type": "markdown",
360+
"metadata": {},
361+
"source": [
362+
"### Create analyzer for the second sample\n",
363+
"We can reuse previous AzureContentUnderstandingClient"
364+
]
365+
},
366+
{
367+
"cell_type": "code",
368+
"execution_count": null,
369+
"metadata": {},
370+
"outputs": [],
371+
"source": [
321372
"response = client.begin_create_analyzer(\n",
322373
" CUSTOM_ANALYZER_ID_2,\n",
323374
" analyzer_template_path=analyzer_template_2,\n",
@@ -332,9 +383,22 @@
332383
" logging.warning(\n",
333384
" \"An issue was encountered when trying to create the analyzer. \"\n",
334385
" \"Please double-check your deployment and configurations for potential problems.\"\n",
335-
" )\n",
336-
"\n",
337-
"# Analyze the multiple input documents with the second analyzer\n",
386+
" )"
387+
]
388+
},
389+
{
390+
"cell_type": "markdown",
391+
"metadata": {},
392+
"source": [
393+
"### Analyze the multiple input documents with the second analyzer"
394+
]
395+
},
396+
{
397+
"cell_type": "code",
398+
"execution_count": null,
399+
"metadata": {},
400+
"outputs": [],
401+
"source": [
338402
"logging.info(\"Start analyzing input documents for the second sample...\")\n",
339403
"response = client.begin_analyze(CUSTOM_ANALYZER_ID_2, file_location=input_docs_2)\n",
340404
"result_json = client.poll_result(response, timeout_seconds=600) # set a longer timeout for pro mode\n",
@@ -348,14 +412,14 @@
348412
" json.dump(result_json, file, indent=2)\n",
349413
"\n",
350414
"logging.info(f\"Full analyzer result saved to: {output_path}\")\n",
351-
"display(FileLink(output_path))\n"
415+
"display(FileLink(output_path))"
352416
]
353417
},
354418
{
355419
"cell_type": "markdown",
356420
"metadata": {},
357421
"source": [
358-
"> Let's take a deeper look at `LineItemCorroboration` field in the result"
422+
"### Let's take a deeper look at `LineItemCorroboration` field in the result"
359423
]
360424
},
361425
{
@@ -375,13 +439,19 @@
375439
"> In the LineItemCorroboration field, we see that each line item is extracted with its corresponding information, claim status, and evidence. Items that are not covered by the policy, such as the Starbucks drink and hotel stay, are not confirmed, while damage repairs that are supported by the supplied documents in the claim and are permitted by the policy are confirmed."
376440
]
377441
},
442+
{
443+
"cell_type": "markdown",
444+
"metadata": {},
445+
"source": [
446+
"### [Optional] Delete the analyzer for second sample after use"
447+
]
448+
},
378449
{
379450
"cell_type": "code",
380451
"execution_count": null,
381452
"metadata": {},
382453
"outputs": [],
383454
"source": [
384-
"# [Optional] Delete the analyzer for second sample after use\n",
385455
"client.delete_analyzer(CUSTOM_ANALYZER_ID_2)"
386456
]
387457
}

0 commit comments

Comments
 (0)