Skip to content

Commit 42f31a6

Browse files
Mistral Large 3 sample notebook and Document AI updates (#395)
1 parent 2037eea commit 42f31a6

2 files changed

Lines changed: 772 additions & 10 deletions

File tree

samples/mistral/python/mistral-docai-basics.ipynb

Lines changed: 150 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@
4141
"## 0. Setup"
4242
]
4343
},
44+
{
45+
"cell_type": "markdown",
46+
"id": "39a9ad0d",
47+
"metadata": {},
48+
"source": [
49+
"From Azure AI Foundry, after clicking on \"Use this model\" and going through standard Foundry setup you will need the provided Endpoint, Key and Deployment Name.\n",
50+
"\n",
51+
"The Endpoint is used as the url in the REST request, the Key is used for authentication in the header, and the Deployment Name is used in the body of the request."
52+
]
53+
},
4454
{
4555
"cell_type": "code",
4656
"execution_count": null,
@@ -50,6 +60,7 @@
5060
"source": [
5161
"AZURE_MISTRAL_DOCUMENT_AI_ENDPOINT = \"\"\n",
5262
"AZURE_MISTRAL_DOCUMENT_AI_KEY = \"\"\n",
63+
"AZURE_AI_DEPLOYMENT_NAME = \"\"\n",
5364
"REQUEST_HEADERS = {\n",
5465
" \"Content-Type\": \"application/json\",\n",
5566
" \"Authorization\": f\"Bearer {AZURE_MISTRAL_DOCUMENT_AI_KEY}\",\n",
@@ -71,7 +82,7 @@
7182
"metadata": {},
7283
"outputs": [],
7384
"source": [
74-
"!wget https://raw.githubusercontent.com/mistralai/cookbook/refs/heads/main/mistral/ocr/mistral7b.pdf"
85+
"!wget -P samples https://raw.githubusercontent.com/mistralai/cookbook/refs/heads/main/mistral/ocr/mistral7b.pdf"
7586
]
7687
},
7788
{
@@ -89,12 +100,12 @@
89100
"metadata": {},
90101
"outputs": [],
91102
"source": [
92-
"def encode_image(image_path: str) -> str:\n",
103+
"def encode_file(file_path: str) -> str:\n",
93104
" try:\n",
94-
" with open(image_path, \"rb\") as image_file:\n",
105+
" with open(file_path, \"rb\") as image_file:\n",
95106
" return base64.b64encode(image_file.read()).decode(\"utf-8\")\n",
96107
" except FileNotFoundError:\n",
97-
" print(f\"Error: The file {image_path} was not found.\")\n",
108+
" print(f\"Error: The file {file_path} was not found.\")\n",
98109
" return None\n",
99110
"\n",
100111
"\n",
@@ -139,7 +150,7 @@
139150
"metadata": {},
140151
"outputs": [],
141152
"source": [
142-
"encodedDocument = encode_image(\"mistral7b.pdf\")"
153+
"encodedDocument = encode_file(\"samples/mistral7b.pdf\")"
143154
]
144155
},
145156
{
@@ -158,7 +169,7 @@
158169
"outputs": [],
159170
"source": [
160171
"documentPayload = {\n",
161-
" \"model\": \"mistral-document-ai-2505\",\n",
172+
" \"model\": f\"{AZURE_AI_DEPLOYMENT_NAME}\",\n",
162173
" \"document\": {\n",
163174
" \"type\": \"document_url\",\n",
164175
" \"document_url\": f\"data:application/pdf;base64,{encodedDocument}\",\n",
@@ -258,7 +269,7 @@
258269
"outputs": [],
259270
"source": [
260271
"documentPayloadandImages = {\n",
261-
" \"model\": \"mistral-document-ai-2505\",\n",
272+
" \"model\": f\"{AZURE_AI_DEPLOYMENT_NAME}\",\n",
262273
" \"document\": {\n",
263274
" \"type\": \"document_url\",\n",
264275
" \"document_url\": f\"data:application/pdf;base64,{encodedDocument}\",\n",
@@ -322,7 +333,7 @@
322333
"metadata": {},
323334
"outputs": [],
324335
"source": [
325-
"ms8kDocument = encode_image(\"0000950170-25-100226.pdf\")"
336+
"ms8kDocument = encode_file(\"samples/0000950170-25-100226.pdf\")"
326337
]
327338
},
328339
{
@@ -333,7 +344,7 @@
333344
"outputs": [],
334345
"source": [
335346
"msRequestPayload = {\n",
336-
" \"model\": \"mistral-document-ai-2505\",\n",
347+
" \"model\": f\"{AZURE_AI_DEPLOYMENT_NAME}\",\n",
337348
" \"document\": {\n",
338349
" \"type\": \"document_url\",\n",
339350
" \"document_url\": f\"data:application/pdf;base64,{ms8kDocument}\",\n",
@@ -381,12 +392,141 @@
381392
"As observed, the extracted text in the table is accurate and true to the original tabular representation."
382393
]
383394
},
395+
{
396+
"cell_type": "markdown",
397+
"id": "876805a1",
398+
"metadata": {},
399+
"source": [
400+
"# 5. Microsoft Word Documents"
401+
]
402+
},
403+
{
404+
"cell_type": "code",
405+
"execution_count": null,
406+
"id": "5a679e09",
407+
"metadata": {},
408+
"outputs": [],
409+
"source": [
410+
"wordDocument = encode_file(\"samples/TranscriptFY25q4.docx\")"
411+
]
412+
},
413+
{
414+
"cell_type": "code",
415+
"execution_count": null,
416+
"id": "b3b2b474",
417+
"metadata": {},
418+
"outputs": [],
419+
"source": [
420+
"wordPayload = {\n",
421+
" \"model\": f\"{AZURE_AI_DEPLOYMENT_NAME}\",\n",
422+
" \"document\": {\n",
423+
" \"type\": \"document_url\",\n",
424+
" \"document_url\": f\"data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,{wordDocument}\",\n",
425+
" },\n",
426+
"}"
427+
]
428+
},
429+
{
430+
"cell_type": "code",
431+
"execution_count": null,
432+
"id": "2961fb92",
433+
"metadata": {},
434+
"outputs": [],
435+
"source": [
436+
"wordResponse = requests.post(\n",
437+
" url=AZURE_MISTRAL_DOCUMENT_AI_ENDPOINT,\n",
438+
" json=wordPayload,\n",
439+
" headers=REQUEST_HEADERS,\n",
440+
")"
441+
]
442+
},
443+
{
444+
"cell_type": "code",
445+
"execution_count": null,
446+
"id": "0d1938c5",
447+
"metadata": {},
448+
"outputs": [],
449+
"source": [
450+
"# limiting to 1000 characters for easier reading\n",
451+
"display(Markdown(simple_combined_markdown(wordResponse.json()[\"pages\"][0])[:1000]))"
452+
]
453+
},
454+
{
455+
"cell_type": "markdown",
456+
"id": "31701ebe",
457+
"metadata": {},
458+
"source": [
459+
"# 6. Microsoft Powerpoint Presentations"
460+
]
461+
},
462+
{
463+
"cell_type": "code",
464+
"execution_count": null,
465+
"id": "c2ae57bd",
466+
"metadata": {},
467+
"outputs": [],
468+
"source": [
469+
"!wget -P samples https://github.com/mistralai/cookbook/raw/52ed72962a4d2be2c922919d7c25016b71b900df/data/sample.pptx"
470+
]
471+
},
472+
{
473+
"cell_type": "code",
474+
"execution_count": null,
475+
"id": "3c34a751",
476+
"metadata": {},
477+
"outputs": [],
478+
"source": [
479+
"powerpointDocument = encode_file(\"samples/sample.pptx\")"
480+
]
481+
},
482+
{
483+
"cell_type": "code",
484+
"execution_count": null,
485+
"id": "bfc3b09b",
486+
"metadata": {},
487+
"outputs": [],
488+
"source": [
489+
"pptxPayload = {\n",
490+
" \"model\": f\"{AZURE_AI_DEPLOYMENT_NAME}\",\n",
491+
" \"document\": {\n",
492+
" \"type\": \"document_url\",\n",
493+
" \"document_url\": f\"data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,{powerpointDocument}\",\n",
494+
" },\n",
495+
" \"include_image_base64\": \"false\",\n",
496+
" \"image_limit\": 0,\n",
497+
"}"
498+
]
499+
},
500+
{
501+
"cell_type": "code",
502+
"execution_count": null,
503+
"id": "b058975d",
504+
"metadata": {},
505+
"outputs": [],
506+
"source": [
507+
"pptxResponse = requests.post(\n",
508+
" url=AZURE_MISTRAL_DOCUMENT_AI_ENDPOINT,\n",
509+
" json=pptxPayload,\n",
510+
" headers=REQUEST_HEADERS,\n",
511+
")"
512+
]
513+
},
514+
{
515+
"cell_type": "code",
516+
"execution_count": null,
517+
"id": "0f5addc6",
518+
"metadata": {},
519+
"outputs": [],
520+
"source": [
521+
"print(json.dumps(pptxResponse.json(), indent=4))"
522+
]
523+
},
384524
{
385525
"cell_type": "markdown",
386526
"id": "188408f8",
387527
"metadata": {},
388528
"source": [
389-
"# 4. Wrap-up"
529+
"# 7. Wrap-up"
390530
]
391531
},
392532
{

0 commit comments

Comments
 (0)