- Регистрация
- 9 Май 2015
- Сообщения
- 1,483
- Баллы
- 155

In this short example, we want to show you how you can add with a minimal amount of effort automatic invoice data extraction to a Delphi app.
This is realized with the TTMSMCPCloudAI component and in this example, taking advantage of 3 possible LLMs: OpenAI, Gemini or Claude.
How it works?
It is actually quite simple how to achieve this. We use the TTMSMCPCloudAI to send the PDF invoice and a prompt and we get in return a JSON object with the data we wanted to extract!
An essential part is providing a good prompt. We have seen good results from the following prompt setup:
TMSMCPCloudAI1.Context.Text := 'Extract information from the PDF invoice and return as a JSON object';
TMSMCPCloudAI1.SystemRole.Text := 'You collect information from the PDF invoice and return it strictly as a JSON object with this exact structure: JSON object has following key-value pairs: "InvoiceDate","InvoiceNumber","VendorName","VendorAddress","VendorVATID","CustomerName","CustomerAddress","CustomerVATID", "ListofItems","ListOfPrices", "NetTotal", "Tax", "Total";'
You can see that we use the system role description where we specify what the expected output format is of the JSON object containing the critical invoice data. But combining everything in the context also turned out to be working. The main prompt is then just to "Extract information from the PDF invoice and return as a JSON object". The system role is used to specify what exact information we want from the invoice and as what key-value pairs it should be returned in the invoice.
Sending the PDF invoice along with the prompt to the LLM is slightly different for Gemini and OpenAI as for Claude. With OpenAI and Gemini, we make use of the UploadFile() method so we first upload the PDF file with:
begin
TMSMCPCloudAI1.UploadFile(INVOICEPDF, aiftPDF);
end;
We the upload is complete, i.e. after the event OnFileUpload is triggered, we can send the prompt with
begin
TMSMCPCloudAI1.Execute('process_invoice');
end;
For Claude, we can attach the PDF invoice along with the prompt as Claude accepts base64 encoded PDF files together with the main prompt message, i.e.
begin
TMSMCPCloudAI1.AddFile(INVOICEPDF, aiftPDF);
TMSMCPCloudAI1.Execute('process_invoice');
end;
To select the LLM to be used for this operation is as sample as:
TMSMCPCloudAI.Service := aiClaude
The resulting output of the LLM is for this PDF invoice:

{
"InvoiceDate": "05/09/2022",
"InvoiceNumber": "FA2022-0001",
"VendorName": "BV CRE8",
"VendorAddress": "Diestersteenweg 462, 3680 Maaseik, Belgium",
"VendorVATID": "BE0631922138",
"CustomerName": "Comanage business user",
"CustomerAddress": "",
"CustomerVATID": "BE0631922138",
"ListofItems": ["comanage business pakket"],
"ListOfPrices": [150.00],
"NetTotal": 150.00,
"Tax": 31.50,
"Total": 181.50
}
We tested this with a couple of sample invoice downloaded from the internet and the result was satisfying.
This is available as a VCL application. Note that it uses the TAdvPDFViewer component to show a preview of the PDF file selected. The TAdvPDFViewer component is part of The sample project download also includes 3 sample PDF files for your testing.

You can download the test project here for uise with the latest release of TMS AI Studio.
Conclusion
For the task of automatic invoice data extraction, what required very complex code and specific PDF processing libraries or OCR modules a couple of years ago, becomes today just a couple of lines of code to invoke an LLM with the invoice fed to the LLM and write a prompt to extract the information you need in your application. We surely live in amazing times! And with TMS AI Studio, this is accomplished in a couple of minutes from your Delphi IDE!
Источник: