Files
crm.clientright.ru/aiassist/vectorizer.php

179 lines
6.0 KiB
PHP
Executable File

<?php
function getTextEmbedding($text) {
$command = "sudo -u fastuser /var/www/laws/legal/bin/python3 /var/www/laws/legal/vectorize.py";
$descriptors = [
0 => ["pipe", "r"], // stdin (передаем текст)
1 => ["pipe", "w"], // stdout (читаем результат)
2 => ["pipe", "w"] // stderr (читаем ошибки)
];
$process = proc_open($command, $descriptors, $pipes);
if (!is_resource($process)) {
error_log("Ошибка запуска Python через proc_open: $command");
return [
"embedding_1024" => array_fill(0, 1024, 0.001),
"embedding_2048" => array_fill(0, 2048, 0.001)
];
}
// Передаем текст в stdin
fwrite($pipes[0], $text);
fclose($pipes[0]);
// Читаем результат из stdout
$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);
// Читаем stderr
$errorOutput = stream_get_contents($pipes[2]);
fclose($pipes[2]);
proc_close($process);
// Логирование для отладки
file_put_contents('/var/www/fastuser/data/www/crm.clientright.ru/aiassist/logs/vectorizer.log',
date('Y-m-d H:i:s') . " - Raw output: " . $output . "\n", FILE_APPEND);
if (!empty($errorOutput)) {
error_log("Ошибка Python: " . $errorOutput);
return [
"embedding_1024" => array_fill(0, 1024, 0.001),
"embedding_2048" => array_fill(0, 2048, 0.001)
];
}
$decoded = json_decode($output, true);
return [
"embedding_1024" => $decoded["embedding_1024"] ?? array_fill(0, 1024, 0.001),
"embedding_2048" => $decoded["embedding_2048"] ?? array_fill(0, 2048, 0.001)
];
}
/*function getTextEmbedding($text) {
$text = mb_substr($text, 0, 500, 'UTF-8');
$escapedText = "'" . addslashes($text) . "'";
$command = "sudo -u fastuser /var/www/laws/legal/bin/python3 /var/www/laws/legal/vectorize.py $escapedText";
// Логируем команду
file_put_contents('/var/www/fastuser/data/www/crm.clientright.ru/aiassist/logs/debug.log',
date('Y-m-d H:i:s') . " - Command: " . $command . "\n", FILE_APPEND);
// Запускаем
$output = shell_exec($command . " 2>&1");
// Логируем ответ
file_put_contents('/var/www/fastuser/data/www/crm.clientright.ru/aiassist/logs/vectorizer.log',
date('Y-m-d H:i:s') . " - Raw output: " . $output . "\n", FILE_APPEND);
if (!$output) {
error_log("Ошибка вызова Python через shell_exec: $command");
return array_fill(0, 1024, 0.001);
}
$decoded = json_decode($output, true);
if (isset($decoded["embedding"])) {
return $decoded["embedding"];
} else {
error_log("Ошибка в JSON-ответе: " . $output);
return array_fill(0, 1024, 0.001);
}
}
*/
/*
function getTextEmbedding($text) {
// $escapedText = escapeshellarg($text);
$escapedText = "'" . addslashes($text) . "'";
$command = "echo 'yourpassword' | sudo -S -u fastuser /var/www/laws/legal/bin/python3 /var/www/laws/legal/vectorize.py 'тест'";
$output = shell_exec($command . " 2>&1");
file_put_contents('/var/www/fastuser/data/www/crm.clientright.ru/aiassist/logs/debug.log', date('Y-m-d H:i:s') . " - Whoami: " . shell_exec('whoami') . "\n", FILE_APPEND);
file_put_contents('/var/www/fastuser/data/www/crm.clientright.ru/aiassist/logs/debug.log',
date('Y-m-d H:i:s') . " - Command: " . $command . "\n", FILE_APPEND);
$output = shell_exec($command);
file_put_contents('/var/www/fastuser/data/www/crm.clientright.ru/aiassist/logs/vectorizer.log', date('Y-m-d H:i:s') . " - Raw output: " . $output . "\n", FILE_APPEND);
if (!$output) {
error_log("Ошибка вызова Python через shell_exec: $command");
return array_fill(0, 1024, 0.001);
}
$decoded = json_decode($output, true);
if (isset($decoded["embedding"])) {
return $decoded["embedding"];
} else {
error_log("Ошибка в JSON-ответе: " . $output);
return array_fill(0, 1024, 0.001);
}
/*
if (!$output) {
error_log("Ошибка вызова Python: $command");
return array_fill(0, 384, 0.001); // paraphrase-MiniLM-L6-v2 возвращает 384-мерный вектор
}
*/
// return json_decode($output, true);
//}
/*
function getTextEmbedding($text) {
$escapedText = escapeshellarg($text);
$command = "sudo sh /var/www/laws/legal/run_vectorize.sh $escapedText 2>&1";
$output = shell_exec($command);
file_put_contents('/var/www/fastuser/data/www/crm.clientright.ru/aiassist/logs/vectorizer.log', date('Y-m-d H:i:s') . " - Raw output: " . $output . "\n", FILE_APPEND);
if (!$output) {
error_log("Ошибка вызова Python через sh: $command");
return array_fill(0, 1024, 0.001);
}
return json_decode($output, true);
}
*/
/*
function getTextEmbedding($text) {
$escapedText = escapeshellarg($text);
$command = "echo 'yourpassword' | sudo -S -u fastuser /var/www/laws/legal/bin/python3 /var/www/laws/legal/vectorize.py";
$output = shell_exec($command . " 2>&1");
// $escapedText = escapeshellarg($text);
// $command = "sudo -u fastuser /var/www/laws/legal/bin/python3 /var/www/laws/legal/vectorize.py $escapedText 2>&1";
//$output = shell_exec($command);
file_put_contents('/var/www/fastuser/data/www/crm.clientright.ru/aiassist/logs/vectorizer.log',
date('Y-m-d H:i:s') . " - Raw output: " . $output . "\n", FILE_APPEND);
if (!$output) {
error_log("Ошибка вызова Python через shell_exec: $command");
return array_fill(0, 1024, 0.001);
}
$decoded = json_decode($output, true);
if (isset($decoded["embedding"])) {
return $decoded["embedding"];
} else {
error_log("Ошибка в JSON-ответе: " . $output);
return array_fill(0, 1024, 0.001);
}
}
*/
?>