Goal 1 : Use Mistral AI Document Understanding and OCR (Optical Character Recognition) REST API with your Magic xpi Plaform
Prerequisites :
- Mistral AI API Key
- Poppler utility installed (https://poppler.freedesktop.org/)
1/ Transform your document (Invoice PDF file) to PNG image.
Assume that you have this invoice below in PDF format (testfact.pdf) and want to extract information from it.

Use poppler utility in a command line in the first Magic xpi step to convert your pdf to png file. (use FileManagement connector)

2/ Upload your image file using the Mistal IA REST API (https://api.mistral.ai/v1/files)
You can create your API KEY on your Mistral console (La Plateforme – Mistral AI) like below

Define your Mistral AI REST Client resource with your Authorization header (API KEY) and 3 paths

Add 2 parameters (purpose, file) to Request (/v1/files)

/Configure your REST Client connector like below

Retrieve the Uploaded Id File from the DataMapper using JSON Source in a Flow variable (F.FileId)

3/ Retrieve your uploaded Image URL by passing the id (https://api.mistral.ai/v1/files/:file_id/url)

Click OK and pass your F.FileId in the mapper
Retrieve the URL from the JSON URL with a Datamapper
4/ Populate the prompt body and post it to the URL : https://api.mistral.ai/v1/chat/completions
Build your prompt JSON Body using Magic xpi Template (use the <!$MG_xxx> Tags in the template file) or a JSON schema

Use the Datamapper to populate the tags with values

The text prompt could be like below ( we use the model (mistral-small-latest) with Mistral Vision capabilities)
‘From this json, extract the Date, Address To, Ship To and Model name and color and associated product code, unitprice and total price and return it as a string in a Json object‘
Your prompt Body should be like below

(*) if your first step is generating several png files, you need to iterate on the « image_url » node.
In this case, you have use the <!$MGREPEAT>and <!$MGENDREPEAT>tags or Multiple JSON node
(**) you can add a role « system » node with any content to enrich your prompt with the user text
5/ Extract the JSON Content part from the result and parse it to send data to any backend system
You can use Strtoken function to extract the Content part : (StrToken (StrToken (C.UserBlob,2,' »content »: »‘),1,' »}, »finish_reason »: »stop »}]’))
And then replace the \n with empty space and \ » with » (RepStr (RepStr (C.UserBlob,’\n’, »),’\ »‘,' »‘))


(*) OCR and Document Understanding | Mistral AI Large Language Models