Files
html/api/wevia-async-client.php
2026-04-14 16:37:36 +02:00

29 lines
888 B
PHP

<?php
function wevia_async_llm($prompt, $max_tokens = 150) {
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$job_id = uniqid('llm_', true);
$redis->lPush('wevia:llm:queue', json_encode([
'id' => $job_id, 'prompt' => $prompt,
'max_tokens' => $max_tokens, 'timestamp' => microtime(true)
]));
return $job_id;
}
function wevia_async_result($job_id, $timeout = 15) {
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$start = time();
while (time() - $start < $timeout) {
$result = $redis->get("wevia:llm:results:$job_id");
if ($result) return json_decode($result, true);
usleep(100000);
}
return null;
}
function wevia_async_blocking($prompt, $max_tokens = 150, $timeout = 15) {
$job_id = wevia_async_llm($prompt, $max_tokens);
return wevia_async_result($job_id, $timeout);
}