28if (!defined(
'NOTOKENRENEWAL')) {
29 define(
'NOTOKENRENEWAL', 1);
31if (!defined(
'NOREQUIREMENU')) {
32 define(
'NOREQUIREMENU',
'1');
34if (!defined(
'NOREQUIREHTML')) {
35 define(
'NOREQUIREHTML',
'1');
37if (!defined(
'NOREQUIREAJAX')) {
38 define(
'NOREQUIREAJAX',
'1');
40if (!defined(
'NOHEADERNOFOOTER')) {
41 define(
'NOHEADERNOFOOTER',
'1');
44require_once
'../../main.inc.php';
52require_once DOL_DOCUMENT_ROOT.
'/core/class/html.form.class.php';
53require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
54require_once DOL_DOCUMENT_ROOT.
'/ai/class/ai.class.php';
58 $langs->setDefaultLang(
GETPOST(
'lang',
'aZ09'));
61$langs->loadLangs(array(
"main",
"other",
"exports"));
63$action =
GETPOST(
'action',
'aZ09');
64$modulepart =
GETPOST(
'modulepart',
'aZ09');
67$upload_dir =
$conf->user->dir_temp.
'/import';
75if (preg_match(
'/^upload_page-([a-z_]+)-uid(\d+)-/', $file, $reg)) {
76 $modulepart = $reg[1];
78 if ($reg[2] != $user->id) {
79 accessforbidden(
'User id found in filename to process does not match current user id');
105$originalfilename = $file;
106$uid = $thiid = $pid = $erid = $salid = 0;
107if (preg_match(
'/-uid([\d+])/', $file, $reg)) {
109 $originalfilename = preg_replace(
'/-uid\d+/',
'', $originalfilename);
111if (preg_match(
'/-thiid([\d+])/', $file, $reg)) {
113 $originalfilename = preg_replace(
'/-thiid\d+/',
'', $originalfilename);
115if (preg_match(
'/-pid([\d+])/', $file, $reg)) {
117 $originalfilename = preg_replace(
'/-pid\d+/',
'', $originalfilename);
119if (preg_match(
'/-erid([\d+])/', $file, $reg)) {
121 $originalfilename = preg_replace(
'/-erid\d+/',
'', $originalfilename);
123if (preg_match(
'/-salid([\d+])/', $file, $reg)) {
125 $originalfilename = preg_replace(
'/-salid\d+/',
'', $originalfilename);
127$originalfilename = preg_replace(
'/^upload_page-[a-z_]+-/',
'', $originalfilename);
133$METHOD =
'converttotext';
136$docformat = $doctypelabel = $prompt =
'';
137$fullpathoffile = $upload_dir.
'/'.$file;
142if ($modulepart ==
'invoice_supplier') {
144 $doctypelabel =
'invoice';
147 $prompt =
'Analyze the contents of this '.$docformat.
' document of an '.$doctypelabel.
' and convert all readable text and numerical information into a structured JSON format. Follow these guidelines:
149 1. **Hierarchical Structure:** Group related information into logical categories like "document_info", "vendor or issuer", "recipient", "items", "payment_info" and "other" depending on the content.
150 2. **Flexible Field Identification:** Identify and organize common fields that are found such as:
151 - "invoice number", "invoice reference"
152 - "date", "issue date", "due date", "transaction or invoice date"
153 - "vendor name", "vendor vat number", "vendor professional id (siret, siren, ...), "vendor address", "vendor phone", "vendor email",
154 - "items", "products", "services", "product or service label", "product or service ref", "product or service ref"
155 - "totals", "summary", "amounts", "balance"
156 - "notes," "comments," "messages," "terms"
157 3. **Handle Tables and Lists:** If the document contains tables, represent each row as an object in a list with fields like "no", "ref", "description", "quantity", "vat rate", "unit price", "total excluding tax", "total including tax".
158 4. **Normalize Dates:** If dates are present, format them in ISO format (YYYY-MM-DD) whenever possible.
159 5. **Ignore Background Noise:** Exclude background noise, decorative elements, and irrelevant symbols that do not contribute to the data content.
160 6. **Preserve Context:** If the image contains sections, headings, or grouping indicators, use them to create logical hierarchies in the JSON structure.
161 7. **General Usability:** Format the text to be suitable for further processing, analysis, or database import.
162 8. **Key information:** The invoice number, the invoice date, the due date, the title and the currency must always be inside "document_info" with exactly the key names of the example below. If a value is not found in the document, use null. Never guess or calculate a date that is not written in the document.
164 **Example JSON Structure:**
168 "invoice_number": "<invoice ref or number>",
169 "issue_date": "<YYYY-MM-DD>",
170 "due_date": "<YYYY-MM-DD>",
172 "currency_code": "<ISO 4217 currency code>"
176 "address": "<address>",
177 "phone": "<phone number>",
179 "vatnumber": "<vat number>",
180 "profid": "<professional id>"
184 "address": "<address>",
185 "phone": "<phone number>",
187 "vatnumber": "<vat number>",
188 "profid": "<professional id>"
195 "description": "<description>",
196 "quantity": "<quantity>",
197 "vatrate": "<vat rate>",
198 "totalinctax": "<total including tax>"
199 "totalexcltax": "<total excluding tax>"
203 "subtotal": "<subtotal>",
204 "tax": "<amount of tax>",
205 "total": "<total to pay>"
209 "method": "<check or cash or card or direct_debit or credit_transfer or other>",
210 "details": "Detail of the payment mode"
215 "method": "<check or cash or card or direct_debit or credit_transfer or other>",
216 "amount": "<detail of the payment>",
217 "note":"<other information on payment done>"
220 "notes": "<optional text>"
223} elseif ($modulepart ==
'salaries') {
225 $doctypelabel =
'payslip';
228 $prompt =
'Analyze the contents of this '.$docformat.
' document of a '.$doctypelabel.
' (salary slip) and convert the information into a structured JSON format. Follow these guidelines:
230 1. **Structure:** Use exactly the keys of the example JSON structure below. Use null when a value is not found.
231 2. **Normalize Dates:** Format dates in ISO format (YYYY-MM-DD). The period is the first and last day of the paid period.
232 3. **Normalize Amounts:** Amounts are numbers with a dot as decimal separator, without currency symbol and without thousand separator.
233 4. **Net to pay:** "net_to_pay" is the final amount paid to the employee (after all deductions and income tax withholding).
234 5. **Privacy:** Do not return the social security number, the bank account or the address of the employee.
236 **Example JSON Structure:**
240 "title": "<title of document>",
241 "period_start": "<YYYY-MM-DD>",
242 "period_end": "<YYYY-MM-DD>",
243 "payment_date": "<YYYY-MM-DD>",
244 "currency_code": "<ISO 4217 currency code>"
250 "name": "<firstname and lastname>",
251 "job_title": "<job title>"
254 "gross_salary": "<gross salary>",
255 "total_employee_contributions": "<total of employee contributions>",
256 "net_before_income_tax": "<net amount before income tax>",
257 "income_tax": "<income tax withholding>",
258 "net_to_pay": "<net amount paid to the employee>"
262} elseif ($modulepart ==
'chargesociales') {
264 $doctypelabel =
'social or fiscal tax notice';
267 $prompt =
'Analyze the contents of this '.$docformat.
' document of a '.$doctypelabel.
' (a request for payment of social contributions or of a tax) and convert the information into a structured JSON format. Follow these guidelines:
269 1. **Structure:** Use exactly the keys of the example JSON structure below. Use null when a value is not found.
270 2. **Normalize Dates:** Format dates in ISO format (YYYY-MM-DD). The period is the first and last day of the period the tax is related to. The due date is the last date to pay.
271 3. **Normalize Amounts:** Amounts are numbers with a dot as decimal separator, without currency symbol and without thousand separator.
272 4. **Amount to pay:** "amount_to_pay" is the total amount that must be paid with this document.
273 5. **Privacy:** Do not return bank account numbers or social security numbers.
275 **Example JSON Structure:**
279 "title": "<short label of the tax or contribution, for example URSSAF contributions or VAT>",
280 "organization": "<name of the organization asking the payment>",
281 "reference": "<reference of the document or contract>",
282 "period_start": "<YYYY-MM-DD>",
283 "period_end": "<YYYY-MM-DD>",
284 "due_date": "<YYYY-MM-DD>",
285 "currency_code": "<ISO 4217 currency code>"
288 "name": "<firstname and lastname of the employee if the document is related to only one employee, else null>"
291 "amount_to_pay": "<total amount to pay>"
295} elseif ($modulepart ==
'expensereport') {
297 $doctypelabel =
'receipt';
300 $prompt =
'Analyze the contents of this '.$docformat.
' document of a '.$doctypelabel.
' (a receipt or an invoice of an expense paid by an employee) and convert the information into a structured JSON format. Follow these guidelines:
302 1. **Structure:** Use exactly the keys of the example JSON structure below. Use null when a value is not found.
303 2. **Normalize Dates:** Format dates in ISO format (YYYY-MM-DD). The date is the date of the expense.
304 3. **Normalize Amounts:** Amounts are numbers with a dot as decimal separator, without currency symbol and without thousand separator.
305 4. **VAT:** If there are several VAT rates, set "vat_rate" to null and give the total amounts.
306 5. **Privacy:** Do not return card numbers or bank account numbers.
308 **Example JSON Structure:**
312 "merchant": "<name of the shop, restaurant, hotel or company>",
313 "title": "<short description of the expense>",
314 "category": "<one of: meal, hotel, transport, fuel, parking, toll, other>",
315 "date": "<YYYY-MM-DD>",
316 "currency_code": "<ISO 4217 currency code>"
319 "total_excluding_tax": "<total amount excluding tax>",
320 "vat_rate": "<VAT rate in percent>",
321 "vat_amount": "<total amount of VAT>",
322 "total_including_tax": "<total amount paid including tax>"
331if ($METHOD ==
'converttotext') {
332 $result = array(
'error' => 0,
'message' =>
'',
'keywords' =>
'',
'content' =>
'',
'cmd' =>
'');
337 if (empty($step) || $step ==
'converttotext') {
340 if ($useAIForFileToTextConversion) {
344 $rawfile = $ai->getPayloadToConvertRawFileIntoText($fullpathoffile);
345 if (!empty($rawfile[
'error'])) {
347 $errors[] = $rawfile[
'message'];
350 dol_syslog(
"ajaxuploadpage.php Call AI service for step 'convert raw file into text' (file=".$file.
")", LOG_DEBUG, 0,
'_ai');
352 $result = $ai->generateContent($rawfile[
'payload'],
'auto', $rawfile[
'function'],
'');
353 if (is_array($result)) {
355 dol_syslog(
"ajaxuploadpage.php AI service returned an error for step 'convert raw file into text': ".(empty($result[
'error']) ?
'' : $result[
'error']).(empty($result[
'curl_error_no']) ?
'' :
' curl_error_no='.$result[
'curl_error_no']), LOG_WARNING, 0,
'_ai');
361 $fileContent = preg_replace(
'/[ \t]{2,}/',
' ', (
string) $result);
367 if (preg_match(
'/\.pdf$/i', $fullpathoffile)) {
368 $methodtotext =
'pdftotext';
369 } elseif (preg_match(
'/\.(png|jpe?g|gif|bmp|tiff?|webp)$/i', $fullpathoffile)) {
370 $methodtotext =
'docling';
373 $result = array(
'error' => 0,
'message' =>
'',
'keywords' =>
'',
'content' =>
'',
'cmd' =>
'');
374 if (!empty($methodtotext)) {
375 $result =
dolDocToText($fullpathoffile, $methodtotext,
'fulltext');
377 if (empty($result[
'error'])) {
379 $fileContent = preg_replace(
'/[ \t]{2,}/',
' ', (
string) $result[
'content']);
385 if (file_put_contents($fullpathoffile.
'.txt', $fileContent) ===
false) {
386 dol_syslog(
"Failed to write the text file ".$fullpathoffile.
'.txt', LOG_WARNING);
388 $errors[] =
'Failed to write the text file';
396 if ($step ==
'converttotext') {
399 if (!empty($errors)) {
400 http_response_code(500);
401 print json_encode(array(
'errors' => $errors));
403 print json_encode(array(
'ok' => 1));
405 $langs->load(
"errors");
406 http_response_code(500);
407 $errors[] = $langs->trans(
"ErrorFailedToConvertFileInText");
408 if (is_array($result) && !empty($result[
'message'])) {
409 $errors[] = $result[
'message'];
411 print json_encode(array(
'errors' => $errors));
417 if ($step ==
'analyze' &&
dol_is_file($fullpathoffile.
'.txt')) {
418 $fileContent = file_get_contents($fullpathoffile.
'.txt');
421 if (!$error && $fileContent) {
422 $prompt =
'This is the content of the document:'.
"\n\n".
dol_substr($fileContent, 0, 12000).
"\n\nQuestion: ".$prompt;
425 dol_syslog(
"ajaxuploadpage.php Call AI service for step 'converttotext' (method=".$METHOD.
", file=".$file.
")", LOG_DEBUG, 0,
'_ai');
430 $apiservice = $ai->getApiService();
431 $defaultmodelfortext = (($apiservice !=
'custom' && !empty($arrayofai[$apiservice][
'textgeneration'][
'default'])) ? $arrayofai[$apiservice][
'textgeneration'][
'default'] :
'');
432 $modelforanalysis =
getDolGlobalString(
'AI_API_'.strtoupper($apiservice).
'_MODEL_TEXT', $defaultmodelfortext);
433 if (empty($modelforanalysis)) {
435 $errors[] =
'No AI model defined to analyze documents for the service '.$apiservice.
' (check the setup of the module AI)';
437 $result = $ai->generateContent($prompt, $modelforanalysis,
'docparsing',
'');
439 if (is_array($result)) {
441 dol_syslog(
"ajaxuploadpage.php AI service returned an error for step 'converttotext': ".(empty($result[
'error']) ?
'' : $result[
'error']).(empty($result[
'curl_error_no']) ?
'' :
' curl_error_no='.$result[
'curl_error_no']), LOG_WARNING, 0,
'_ai');
446 $answer = (string) $result;
448 $answer = preg_replace(
'/^```json/',
'', $answer);
449 $answer = preg_replace(
'/```$/',
'', $answer);
453 $langs->load(
"errors");
455 $errors[] = $langs->trans(
"ErrorFailedToConvertFileInText");
456 if (is_array($result) && !empty($result[
'message'])) {
457 $errors[] = $result[
'message'];
463if ($METHOD ==
'thread') {
474 "purpose" =>
"assistants",
475 "file" =>
new CURLFile($fullpathoffile)
479 dol_syslog(
"ajaxuploadpage.php Call AI service for step 'send file' (method=".$METHOD.
", file=".$file.
")", LOG_DEBUG, 0,
'_ai');
481 $result = $ai->generateContent($payload,
'auto',
'file',
'');
483 if (is_array($result)) {
485 dol_syslog(
"ajaxuploadpage.php AI service returned an error for step 'send file': ".(empty($result[
'error']) ?
'' : $result[
'error']).(empty($result[
'curl_error_no']) ?
'' :
' curl_error_no='.$result[
'curl_error_no']), LOG_WARNING, 0,
'_ai');
490 $fileId = json_decode($result,
true)[
'id'];
497 "name" =>
"PDF Analyzer",
498 "instructions" =>
"Analyze PDF and answer precisely",
500 [
"type" =>
"file_search"]
505 dol_syslog(
"ajaxuploadpage.php Call AI service for step 'create assistant' (method=".$METHOD.
", file=".$file.
")", LOG_DEBUG, 0,
'_ai');
507 $result = $ai->generateContent($payload,
'auto',
'assistant',
'', array(
'OpenAI-Beta',
'assistants=v2'));
509 if (is_array($result)) {
511 dol_syslog(
"ajaxuploadpage.php AI service returned an error for step 'create assistant': ".(empty($result[
'error']) ?
'' : $result[
'error']).(empty($result[
'curl_error_no']) ?
'' :
' curl_error_no='.$result[
'curl_error_no']), LOG_WARNING, 0,
'_ai');
516 $assistantId = json_decode($result,
true)[
'id'];
526 dol_syslog(
"ajaxuploadpage.php Call AI service for step 'create thread' (method=".$METHOD.
", file=".$file.
")", LOG_DEBUG, 0,
'_ai');
528 $result = $ai->generateContent($payload,
'auto',
'thread',
'', array(
'OpenAI-Beta',
'assistants=v2'));
530 if (is_array($result)) {
532 dol_syslog(
"ajaxuploadpage.php AI service returned an error for step 'create thread': ".(empty($result[
'error']) ?
'' : $result[
'error']).(empty($result[
'curl_error_no']) ?
'' :
' curl_error_no='.$result[
'curl_error_no']), LOG_WARNING, 0,
'_ai');
537 $threadId = json_decode($result,
true)[
'id'];
548 "type" =>
"input_text",
554 "file_id" => $fileId,
555 "tools" => [[
"type" =>
"file_search"]]
559 $moreendpoint = $threadId.
'/messages';
562 dol_syslog(
"ajaxuploadpage.php Call AI service for step 'add file to thread' (method=".$METHOD.
", file=".$file.
")", LOG_DEBUG, 0,
'_ai');
564 $result = $ai->generateContent($payload,
'auto',
'thread',
'', array(
'OpenAI-Beta',
'assistants=v2'), $moreendpoint);
566 if (is_array($result)) {
568 dol_syslog(
"ajaxuploadpage.php AI service returned an error for step 'add file to thread': ".(empty($result[
'error']) ?
'' : $result[
'error']).(empty($result[
'curl_error_no']) ?
'' :
' curl_error_no='.$result[
'curl_error_no']), LOG_WARNING, 0,
'_ai');
579 [
"assistant_id" => $assistantId]
581 $moreendpoint = $threadId.
'/runs';
584 dol_syslog(
"ajaxuploadpage.php Call AI service for step 'run thread' (method=".$METHOD.
", file=".$file.
")", LOG_DEBUG, 0,
'_ai');
586 $result = $ai->generateContent($payload,
'auto',
'thread',
'', array(
'OpenAI-Beta',
'assistants=v2'), $moreendpoint);
588 if (is_array($result)) {
590 dol_syslog(
"ajaxuploadpage.php AI service returned an error for step 'run thread': ".(empty($result[
'error']) ?
'' : $result[
'error']).(empty($result[
'curl_error_no']) ?
'' :
' curl_error_no='.$result[
'curl_error_no']), LOG_WARNING, 0,
'_ai');
595 $runId = json_decode($result,
true)[
'id'];
606 $moreendpoint = $threadId.
'/runs/'.$runId;
609 dol_syslog(
"ajaxuploadpage.php Call AI service for step 'poll run status' (method=".$METHOD.
", file=".$file.
")", LOG_DEBUG, 0,
'_ai');
611 $result = $ai->generateContent($payload,
'auto',
'thread',
'', array(
'OpenAI-Beta',
'assistants=v2'), $moreendpoint);
613 if (is_array($result)) {
615 dol_syslog(
"ajaxuploadpage.php AI service returned an error for step 'poll run status': ".(empty($result[
'error']) ?
'' : $result[
'error']).(empty($result[
'curl_error_no']) ?
'' :
' curl_error_no='.$result[
'curl_error_no']), LOG_WARNING, 0,
'_ai');
620 $status =
'completed';
622 $status = json_decode($result,
true)[
'status'];
624 }
while ($status !==
"completed");
631 $moreendpoint = $threadId.
'/messages';
634 dol_syslog(
"ajaxuploadpage.php Call AI service for step 'get answer' (method=".$METHOD.
", file=".$file.
")", LOG_DEBUG, 0,
'_ai');
636 $result = $ai->generateContent($prompt,
'auto',
'thread',
'', array(
'OpenAI-Beta',
'assistants=v2'));
638 if (is_array($result)) {
640 dol_syslog(
"ajaxuploadpage.php AI service returned an error for step 'get answer': ".(empty($result[
'error']) ?
'' : $result[
'error']).(empty($result[
'curl_error_no']) ?
'' :
' curl_error_no='.$result[
'curl_error_no']), LOG_WARNING, 0,
'_ai');
656if (!empty($errors)) {
657 http_response_code(500);
659 print json_encode(array(
'errors' => $errors));
661 $data = json_decode((
string) $answer,
true);
664 http_response_code(500);
667 dol_syslog(
"ajaxuploadpage.php Failed to decode the answer of the AI service as JSON (".json_last_error_msg().
"): ".
dol_trunc((
string) $answer, 500), LOG_WARNING, 0,
'_ai');
670 $langs->load(
"errors");
672 $errors[] = $langs->transnoentitiesnoconv(
"ErrorAIAnswerIsNotValidJSON", json_last_error_msg());
673 print json_encode(array(
'errors' => $errors));
677 $result = file_put_contents($fullpathoffile.
'.json', $answer);
678 if ($result ===
false) {
679 dol_syslog(
"Failed to write the json file ".$fullpathoffile.
'.json', LOG_WARNING);
681 dol_syslog(
"Failed to write the json file ".$fullpathoffile.
'.json', LOG_WARNING, 0,
'_ai');
getListOfAIServices()
Get list of available ai services.
getAIErrorMessage($result)
Return a user-friendly error message from the error array returned by Ai\generateContent()
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_is_file($pathoffile)
Return if path is a file.
dolDocToText($filetoprocess, $useFullTextIndexation='pdftotext', $options='html')
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
dolChmod($filepath, $newmask='')
Change mod of a file.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
dol_substr($string, $start, $length=null, $stringencoding='', $trunconbytes=0)
Make a substring.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '...' if string larger than length.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.