dolibarr 25.0.0-alpha
ajaxuploadpage.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2005-2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This file is a modified version of datepicker.php from phpBSM to fix some
7 * bugs, to add new features and to dramatically increase speed.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
28if (!defined('NOTOKENRENEWAL')) {
29 define('NOTOKENRENEWAL', 1); // Disables token renewal
30}
31if (!defined('NOREQUIREMENU')) {
32 define('NOREQUIREMENU', '1');
33}
34if (!defined('NOREQUIREHTML')) {
35 define('NOREQUIREHTML', '1');
36}
37if (!defined('NOREQUIREAJAX')) {
38 define('NOREQUIREAJAX', '1');
39}
40if (!defined('NOHEADERNOFOOTER')) {
41 define('NOHEADERNOFOOTER', '1');
42}
43
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';
55
56
57if (GETPOST('lang', 'aZ09')) {
58 $langs->setDefaultLang(GETPOST('lang', 'aZ09')); // If language was forced on URL by the main.inc.php
59}
60
61$langs->loadLangs(array("main", "other", "exports"));
62
63$action = GETPOST('action', 'aZ09');
64$modulepart = GETPOST('modulepart', 'aZ09');
65$step = GETPOST('step', 'aZ09'); // '' = all steps, 'converttotext' = conversion of the file into text only, 'analyze' = analysis of the text only
66
67$upload_dir = $conf->user->dir_temp.'/import';
68
69// Delete the temporary files that are used when uploading files
70//dol_delete_file($upload_dir.'/upload_page-by'.$user->id.'-*');
71
72$file = GETPOST('file');
73
74$reg = array();
75if (preg_match('/^upload_page-([a-z_]+)-uid(\d+)-/', $file, $reg)) {
76 $modulepart = $reg[1];
77
78 if ($reg[2] != $user->id) {
79 accessforbidden('User id found in filename to process does not match current user id');
80 }
81} else {
82 accessforbidden('Bad value for file parameter');
83}
84
85$error = 0;
86$errors = array();
87
88$ai = new Ai($db);
89
90
91/*
92 * Actions
93 */
94
95//
96
97
98
99/*
100 * View
101 */
102
103top_httphead('application/json');
104
105$originalfilename = $file;
106$uid = $thiid = $pid = $erid = $salid = 0;
107if (preg_match('/-uid([\d+])/', $file, $reg)) {
108 $uid = $reg[1];
109 $originalfilename = preg_replace('/-uid\d+/', '', $originalfilename);
110}
111if (preg_match('/-thiid([\d+])/', $file, $reg)) {
112 $thiid = $reg[1];
113 $originalfilename = preg_replace('/-thiid\d+/', '', $originalfilename);
114}
115if (preg_match('/-pid([\d+])/', $file, $reg)) {
116 $pid = $reg[1];
117 $originalfilename = preg_replace('/-pid\d+/', '', $originalfilename);
118}
119if (preg_match('/-erid([\d+])/', $file, $reg)) {
120 $erid = $reg[1];
121 $originalfilename = preg_replace('/-erid\d+/', '', $originalfilename);
122}
123if (preg_match('/-salid([\d+])/', $file, $reg)) {
124 $salid = $reg[1];
125 $originalfilename = preg_replace('/-salid\d+/', '', $originalfilename);
126}
127$originalfilename = preg_replace('/^upload_page-[a-z_]+-/', '', $originalfilename);
128
129
130
131
132//$METHOD = 'thread'; // For ChatGPT.
133$METHOD = 'converttotext'; // For Mistral and most others
134
135
136$docformat = $doctypelabel = $prompt = '';
137$fullpathoffile = $upload_dir.'/'.$file;
138$answer = null;
139$fileContent = '';
140
141// Set the prompting
142if ($modulepart == 'invoice_supplier') {
143 $docformat = 'pdf';
144 $doctypelabel = 'invoice';
145
146 // Call IA to get information on PDF
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:
148
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.
163
164 **Example JSON Structure:**
165
166 {
167 "document_info": {
168 "invoice_number": "<invoice ref or number>",
169 "issue_date": "<YYYY-MM-DD>",
170 "due_date": "<YYYY-MM-DD>",
171 "title": "<title>",
172 "currency_code": "<ISO 4217 currency code>"
173 },
174 "vendor": {
175 "name": "<name>",
176 "address": "<address>",
177 "phone": "<phone number>",
178 "email": "<email>",
179 "vatnumber": "<vat number>",
180 "profid": "<professional id>"
181 },
182 "recipient": {
183 "name": "<name>",
184 "address": "<address>",
185 "phone": "<phone number>",
186 "email": "<email>",
187 "vatnumber": "<vat number>",
188 "profid": "<professional id>"
189 },
190 "items": [
191 {
192 "no": "<number>",
193 "ref": "<ref>",
194 "label": "<label>",
195 "description": "<description>",
196 "quantity": "<quantity>",
197 "vatrate": "<vat rate>",
198 "totalinctax": "<total including tax>"
199 "totalexcltax": "<total excluding tax>"
200 }
201 ],
202 "summary": {
203 "subtotal": "<subtotal>",
204 "tax": "<amount of tax>",
205 "total": "<total to pay>"
206 },
207 "payment_methods": [
208 {
209 "method": "<check or cash or card or direct_debit or credit_transfer or other>",
210 "details": "Detail of the payment mode"
211 }
212 ],
213 "payments_done": [
214 {
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>"
218 }
219 ],
220 "notes": "<optional text>"
221 }
222 ';
223} elseif ($modulepart == 'salaries') {
224 $docformat = 'pdf';
225 $doctypelabel = 'payslip';
226
227 // Call IA to get information on the payslip. We ask only the data needed to record the salary (no social security number or bank account).
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:
229
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.
235
236 **Example JSON Structure:**
237
238 {
239 "document_info": {
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>"
245 },
246 "employer": {
247 "name": "<name>"
248 },
249 "employee": {
250 "name": "<firstname and lastname>",
251 "job_title": "<job title>"
252 },
253 "summary": {
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>"
259 }
260 }
261 ';
262} elseif ($modulepart == 'chargesociales') {
263 $docformat = 'pdf';
264 $doctypelabel = 'social or fiscal tax notice';
265
266 // Call IA to get information on the social or fiscal tax (social contributions, VAT, property tax, ...)
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:
268
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.
274
275 **Example JSON Structure:**
276
277 {
278 "document_info": {
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>"
286 },
287 "employee": {
288 "name": "<firstname and lastname of the employee if the document is related to only one employee, else null>"
289 },
290 "summary": {
291 "amount_to_pay": "<total amount to pay>"
292 }
293 }
294 ';
295} elseif ($modulepart == 'expensereport') {
296 $docformat = 'pdf';
297 $doctypelabel = 'receipt';
298
299 // Call IA to get information on the receipt of an expense (restaurant, hotel, transport, fuel, ...)
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:
301
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.
307
308 **Example JSON Structure:**
309
310 {
311 "document_info": {
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>"
317 },
318 "summary": {
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>"
323 }
324 }
325 ';
326}
327
328
329// TODO Move the convert code into an AJAX service and just output the JS code to call the ajax service to start
330
331if ($METHOD == 'converttotext') { // @phpstan-ignore-line
332 $result = array('error' => 0, 'message' => '', 'keywords' => '', 'content' => '', 'cmd' => '');
333 $fileContent = '';
334
335 // Step 'converttotext': conversion of the file into text (done by the AI service with the option
336 // AI_USED_FOR_FILE_TO_TEXT_CONVERTION, or with a local tool like pdftotext or docling)
337 if (empty($step) || $step == 'converttotext') {
338 $useAIForFileToTextConversion = isModEnabled("ai") && getDolGlobalString('AI_USED_FOR_FILE_TO_TEXT_CONVERTION');
339
340 if ($useAIForFileToTextConversion) {
341 // Option AI module is on, the conversion of the raw binary file into text is done
342 // by the AI service with a first call (with the model AI_API_<SERVICE>_MODEL_DOCPARSING or its default value).
343 // A service or a file type not supported returns an error, there is no fall back on a local conversion.
344 $rawfile = $ai->getPayloadToConvertRawFileIntoText($fullpathoffile);
345 if (!empty($rawfile['error'])) {
346 $error++;
347 $errors[] = $rawfile['message'];
348 } else {
349 if (getDolGlobalString("AI_DEBUG")) {
350 dol_syslog("ajaxuploadpage.php Call AI service for step 'convert raw file into text' (file=".$file.")", LOG_DEBUG, 0, '_ai');
351 }
352 $result = $ai->generateContent($rawfile['payload'], 'auto', $rawfile['function'], '');
353 if (is_array($result)) { // If array, there is an error
354 if (getDolGlobalString("AI_DEBUG")) {
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');
356 }
357 $error++;
358 $errors[] = getAIErrorMessage($result);
359 } else {
360 // Remove the spaces used to align columns of tables, so more useful content is kept in the limit of chars sent to the AI
361 $fileContent = preg_replace('/[ \t]{2,}/', ' ', (string) $result);
362 }
363 }
364 } else {
365 // Call the tool able to convert the file into text: pdftotext for a PDF file, docling for an image (docling includes an OCR)
366 $methodtotext = '';
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';
371 }
372
373 $result = array('error' => 0, 'message' => '', 'keywords' => '', 'content' => '', 'cmd' => '');
374 if (!empty($methodtotext)) {
375 $result = dolDocToText($fullpathoffile, $methodtotext, 'fulltext');
376 }
377 if (empty($result['error'])) {
378 // Remove the spaces used to align columns of tables, so more useful content is kept in the limit of chars sent to the AI
379 $fileContent = preg_replace('/[ \t]{2,}/', ' ', (string) $result['content']);
380 }
381 }
382
383 // Save the text into a file with the extension .txt, so it can be analyzed by a second call with the step 'analyze'
384 if (!$error && $fileContent && dol_sanitizeFileName($file) === $file) {
385 if (file_put_contents($fullpathoffile.'.txt', $fileContent) === false) {
386 dol_syslog("Failed to write the text file ".$fullpathoffile.'.txt', LOG_WARNING);
387 $error++;
388 $errors[] = 'Failed to write the text file';
389 } else {
390 dolChmod($fullpathoffile.'.txt');
391 }
392 }
393 }
394
395 // When we do only the step 'converttotext', we can return here: the analysis is done by a second call with the step 'analyze'
396 if ($step == 'converttotext') {
397 $db->close();
398
399 if (!empty($errors)) {
400 http_response_code(500);
401 print json_encode(array('errors' => $errors));
402 } elseif (dol_is_file($fullpathoffile.'.txt')) {
403 print json_encode(array('ok' => 1));
404 } else {
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'];
410 }
411 print json_encode(array('errors' => $errors));
412 }
413 exit(0);
414 }
415
416 // For the step 'analyze', load the text saved by the previous call with the step 'converttotext'
417 if ($step == 'analyze' && dol_is_file($fullpathoffile.'.txt')) {
418 $fileContent = file_get_contents($fullpathoffile.'.txt');
419 }
420
421 if (!$error && $fileContent) {
422 $prompt = 'This is the content of the document:'."\n\n".dol_substr($fileContent, 0, 12000)."\n\nQuestion: ".$prompt;
423
424 if (getDolGlobalString("AI_DEBUG")) {
425 dol_syslog("ajaxuploadpage.php Call AI service for step 'converttotext' (method=".$METHOD.", file=".$file.")", LOG_DEBUG, 0, '_ai');
426 }
427 // The analysis of the content is done with the text generation model, the model AI_API_<SERVICE>_MODEL_DOCPARSING is reserved to the OCR conversion.
428 // For the 'custom' service, the hardcoded default has no meaning for an unknown provider, so we stop with a clear error when the constant is not set.
429 $arrayofai = getListOfAIServices();
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)) {
434 $error++;
435 $errors[] = 'No AI model defined to analyze documents for the service '.$apiservice.' (check the setup of the module AI)';
436 } else {
437 $result = $ai->generateContent($prompt, $modelforanalysis, 'docparsing', '');
438 // $result is an array of error messages or a string with answer
439 if (is_array($result)) { // If array, there is an error
440 if (getDolGlobalString("AI_DEBUG")) {
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');
442 }
443 $error++;
444 $errors[] = getAIErrorMessage($result);
445 } else {
446 $answer = (string) $result;
447
448 $answer = preg_replace('/^```json/', '', $answer);
449 $answer = preg_replace('/```$/', '', $answer);
450 }
451 }
452 } elseif (!$error) {
453 $langs->load("errors");
454 $error++;
455 $errors[] = $langs->trans("ErrorFailedToConvertFileInText");
456 if (is_array($result) && !empty($result['message'])) {
457 $errors[] = $result['message'];
458 }
459 }
460}
461
462
463if ($METHOD == 'thread') { // @phpstan-ignore-line
464 $prompt = '';
465
466 $fileId = 0;
467 $assistantId = 0;
468 $threadId = 0;
469 $runId = 0;
470
471
472 // First part is to send the file
473 $payload = array(
474 "purpose" => "assistants",
475 "file" => new CURLFile($fullpathoffile)
476 );
477
478 if (getDolGlobalString("AI_DEBUG")) {
479 dol_syslog("ajaxuploadpage.php Call AI service for step 'send file' (method=".$METHOD.", file=".$file.")", LOG_DEBUG, 0, '_ai');
480 }
481 $result = $ai->generateContent($payload, 'auto', 'file', '');
482
483 if (is_array($result)) { // If array, there is an error
484 if (getDolGlobalString("AI_DEBUG")) {
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');
486 }
487 $error++;
488 $errors[] = getAIErrorMessage($result);
489 } else {
490 $fileId = json_decode($result, true)['id'];
491 }
492
493
494 // Create assistant
495 if (!$error) {
496 $payload = [
497 "name" => "PDF Analyzer",
498 "instructions" => "Analyze PDF and answer precisely",
499 "tools" => [
500 ["type" => "file_search"]
501 ]
502 ];
503
504 if (getDolGlobalString("AI_DEBUG")) {
505 dol_syslog("ajaxuploadpage.php Call AI service for step 'create assistant' (method=".$METHOD.", file=".$file.")", LOG_DEBUG, 0, '_ai');
506 }
507 $result = $ai->generateContent($payload, 'auto', 'assistant', '', array('OpenAI-Beta', 'assistants=v2'));
508
509 if (is_array($result)) { // If array, there is an error
510 if (getDolGlobalString("AI_DEBUG")) {
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');
512 }
513 $error++;
514 $errors[] = getAIErrorMessage($result);
515 } else {
516 $assistantId = json_decode($result, true)['id'];
517 }
518 }
519
520
521 // Create thread
522 if (!$error) {
523 $payload = '{}';
524
525 if (getDolGlobalString("AI_DEBUG")) {
526 dol_syslog("ajaxuploadpage.php Call AI service for step 'create thread' (method=".$METHOD.", file=".$file.")", LOG_DEBUG, 0, '_ai');
527 }
528 $result = $ai->generateContent($payload, 'auto', 'thread', '', array('OpenAI-Beta', 'assistants=v2'));
529
530 if (is_array($result)) { // If array, there is an error
531 if (getDolGlobalString("AI_DEBUG")) {
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');
533 }
534 $error++;
535 $errors[] = getAIErrorMessage($result);
536 } else {
537 $threadId = json_decode($result, true)['id'];
538 }
539 }
540
541
542 // Add file to thread
543 if (!$error) {
544 $payload = array(
545 "role" => "user",
546 "content" => [
547 [
548 "type" => "input_text",
549 "text" => $prompt
550 ]
551 ],
552 "attachments" => [
553 [
554 "file_id" => $fileId,
555 "tools" => [["type" => "file_search"]]
556 ]
557 ]
558 );
559 $moreendpoint = $threadId.'/messages';
560
561 if (getDolGlobalString("AI_DEBUG")) {
562 dol_syslog("ajaxuploadpage.php Call AI service for step 'add file to thread' (method=".$METHOD.", file=".$file.")", LOG_DEBUG, 0, '_ai');
563 }
564 $result = $ai->generateContent($payload, 'auto', 'thread', '', array('OpenAI-Beta', 'assistants=v2'), $moreendpoint);
565
566 if (is_array($result)) { // If array, there is an error
567 if (getDolGlobalString("AI_DEBUG")) {
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');
569 }
570 $error++;
571 $errors[] = getAIErrorMessage($result);
572 }
573 }
574
575
576 // Run the thread
577 if (!$error) {
578 $payload = array(
579 ["assistant_id" => $assistantId]
580 );
581 $moreendpoint = $threadId.'/runs';
582
583 if (getDolGlobalString("AI_DEBUG")) {
584 dol_syslog("ajaxuploadpage.php Call AI service for step 'run thread' (method=".$METHOD.", file=".$file.")", LOG_DEBUG, 0, '_ai');
585 }
586 $result = $ai->generateContent($payload, 'auto', 'thread', '', array('OpenAI-Beta', 'assistants=v2'), $moreendpoint);
587
588 if (is_array($result)) { // If array, there is an error
589 if (getDolGlobalString("AI_DEBUG")) {
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');
591 }
592 $error++;
593 $errors[] = getAIErrorMessage($result);
594 } else {
595 $runId = json_decode($result, true)['id'];
596 }
597 }
598
599
600 // Poll until end of thread
601 if (!$error) {
602 do {
603 sleep(1);
604
605 $payload = '';
606 $moreendpoint = $threadId.'/runs/'.$runId;
607
608 if (getDolGlobalString("AI_DEBUG")) {
609 dol_syslog("ajaxuploadpage.php Call AI service for step 'poll run status' (method=".$METHOD.", file=".$file.")", LOG_DEBUG, 0, '_ai');
610 }
611 $result = $ai->generateContent($payload, 'auto', 'thread', '', array('OpenAI-Beta', 'assistants=v2'), $moreendpoint);
612
613 if (is_array($result)) { // If array, there is an error
614 if (getDolGlobalString("AI_DEBUG")) {
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');
616 }
617 $error++;
618 $errors[] = getAIErrorMessage($result);
619
620 $status = 'completed';
621 } else {
622 $status = json_decode($result, true)['status'];
623 }
624 } while ($status !== "completed");
625 }
626
627
628 // Get answer
629 if (!$error) {
630 $payload = '';
631 $moreendpoint = $threadId.'/messages';
632
633 if (getDolGlobalString("AI_DEBUG")) {
634 dol_syslog("ajaxuploadpage.php Call AI service for step 'get answer' (method=".$METHOD.", file=".$file.")", LOG_DEBUG, 0, '_ai');
635 }
636 $result = $ai->generateContent($prompt, 'auto', 'thread', '', array('OpenAI-Beta', 'assistants=v2'));
637
638 if (is_array($result)) { // If array, there is an error
639 if (getDolGlobalString("AI_DEBUG")) {
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');
641 }
642 $error++;
643 $errors[] = getAIErrorMessage($result);
644 } else {
645 $answer = $result;
646 }
647 }
648}
649
650
651// End of page
652
653$db->close();
654
655
656if (!empty($errors)) {
657 http_response_code(500);
658
659 print json_encode(array('errors' => $errors));
660} else {
661 $data = json_decode((string) $answer, true);
662
663 if ($data == null) {
664 http_response_code(500);
665
666 if (getDolGlobalString("AI_DEBUG")) {
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');
668 }
669
670 $langs->load("errors");
671 $error++;
672 $errors[] = $langs->transnoentitiesnoconv("ErrorAIAnswerIsNotValidJSON", json_last_error_msg());
673 print json_encode(array('errors' => $errors));
674 } else {
675 // Save the JSON answer into a cache file next to the uploaded file (only if file name has no path part)
676 if (dol_sanitizeFileName($file) === $file && dol_is_file($fullpathoffile)) {
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);
680 if (getDolGlobalString("AI_DEBUG")) {
681 dol_syslog("Failed to write the json file ".$fullpathoffile.'.json', LOG_WARNING, 0, '_ai');
682 }
683 } else {
684 dolChmod($fullpathoffile.'.json');
685 }
686 }
687
688 print $answer;
689 }
690}
getListOfAIServices()
Get list of available ai services.
Definition ai.lib.php:69
getAIErrorMessage($result)
Return a user-friendly error message from the error array returned by Ai\generateContent()
Definition ai.lib.php:532
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class for AI feature.
Definition ai.class.php:37
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.