optimize ui
add new ki endpoint params
This commit is contained in:
@@ -4,69 +4,54 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Infrastructure;
|
||||
|
||||
use App\Entity\ModelGenerationConfig;
|
||||
use App\Service\ModelGenerationConfigProvider;
|
||||
use Generator;
|
||||
use JsonException;
|
||||
use RuntimeException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* OllamaClient
|
||||
*
|
||||
* Production-ready streaming client for Ollama-compatible LLM backends.
|
||||
*
|
||||
* Key properties:
|
||||
* - True live streaming (tokens are yielded while the request is running)
|
||||
* - PHP-safe (no yield inside cURL callbacks)
|
||||
* - Works for both HTTP streaming and CLI usage
|
||||
* - Deterministic and resource-safe
|
||||
*
|
||||
* Implementation strategy:
|
||||
* - Use curl_multi_* to keep control of the execution loop
|
||||
* - Accumulate partial chunks into a rolling buffer
|
||||
* - Extract JSON lines incrementally
|
||||
* - Yield tokens immediately when they arrive
|
||||
*/
|
||||
final class OllamaClient
|
||||
{
|
||||
private string $apiUrl;
|
||||
private string $model;
|
||||
private int $timeoutSeconds;
|
||||
private ?ModelGenerationConfig $cachedConfig = null;
|
||||
|
||||
public function __construct(
|
||||
string $apiUrl,
|
||||
string $model,
|
||||
int $timeoutSeconds,
|
||||
)
|
||||
{
|
||||
$this->apiUrl = $apiUrl;
|
||||
$this->model = $model;
|
||||
$this->timeoutSeconds = $timeoutSeconds;
|
||||
}
|
||||
private string $apiUrl,
|
||||
private string $model,
|
||||
private int $timeoutSeconds,
|
||||
private ModelGenerationConfigProvider $configProvider
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Streams tokens from the LLM backend in real time.
|
||||
*
|
||||
* @param string $prompt Fully constructed prompt
|
||||
*
|
||||
* @return Generator<string>
|
||||
* @throws JsonException
|
||||
* Public Streaming API
|
||||
*/
|
||||
public function stream(string $prompt): Generator
|
||||
{
|
||||
$json = [];
|
||||
$config = $this->getConfig();
|
||||
|
||||
$payload = json_encode([
|
||||
'model' => $this->model,
|
||||
'prompt' => $prompt,
|
||||
'stream' => true,
|
||||
'options' => [
|
||||
"temperature" => 0.9,
|
||||
"top_k" => 35,
|
||||
"top_p" => 0.9,
|
||||
"repeat_penalty" => 1.1,
|
||||
"num_ctx" => 8192
|
||||
]
|
||||
], JSON_THROW_ON_ERROR);
|
||||
if ($config->isStream()) {
|
||||
yield from $this->streamInternal($prompt);
|
||||
return;
|
||||
}
|
||||
|
||||
// Fallback: Blocking generate → Generator-kompatibel ausgeben
|
||||
yield $this->generateInternal($prompt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Public Blocking API
|
||||
*/
|
||||
public function generate(string $prompt): string
|
||||
{
|
||||
return $this->generateInternal($prompt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal streaming transport
|
||||
*/
|
||||
private function streamInternal(string $prompt): Generator
|
||||
{
|
||||
$payload = $this->buildPayload($prompt, true);
|
||||
|
||||
$buffer = '';
|
||||
$done = false;
|
||||
@@ -82,7 +67,7 @@ final class OllamaClient
|
||||
CURLOPT_POSTFIELDS => $payload,
|
||||
CURLOPT_RETURNTRANSFER => false,
|
||||
CURLOPT_TIMEOUT => $this->timeoutSeconds,
|
||||
CURLOPT_WRITEFUNCTION => function ($curl, string $data) use (&$buffer, &$done): int {
|
||||
CURLOPT_WRITEFUNCTION => function ($curl, string $data) use (&$buffer): int {
|
||||
$buffer .= $data;
|
||||
return strlen($data);
|
||||
},
|
||||
@@ -98,12 +83,10 @@ final class OllamaClient
|
||||
|
||||
try {
|
||||
do {
|
||||
// Execute the multi handle
|
||||
do {
|
||||
$status = curl_multi_exec($mh, $running);
|
||||
} while ($status === CURLM_CALL_MULTI_PERFORM);
|
||||
|
||||
// Read incoming data from the buffer
|
||||
while (($pos = strpos($buffer, "\n")) !== false) {
|
||||
$line = trim(substr($buffer, 0, $pos));
|
||||
$buffer = substr($buffer, $pos + 1);
|
||||
@@ -127,37 +110,94 @@ final class OllamaClient
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for network activity
|
||||
if ($running) {
|
||||
curl_multi_select($mh, 0.2);
|
||||
}
|
||||
|
||||
} while ($running && !$done);
|
||||
|
||||
// Flush remaining buffer (edge case)
|
||||
if (!$done && trim($buffer) !== '') {
|
||||
try {
|
||||
$json = json_decode(trim($buffer), true, flags: JSON_THROW_ON_ERROR);
|
||||
if (isset($json['response'])) {
|
||||
yield $json['response'];
|
||||
}
|
||||
} catch (Throwable) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset($json['response'])) {
|
||||
yield $json;
|
||||
return;
|
||||
}
|
||||
|
||||
if (curl_errno($ch)) {
|
||||
$error = curl_error($ch);
|
||||
throw new RuntimeException('LLM connection error: ' . $error);
|
||||
throw new RuntimeException('LLM connection error: ' . curl_error($ch));
|
||||
}
|
||||
|
||||
} finally {
|
||||
curl_multi_remove_handle($mh, $ch);
|
||||
curl_multi_close($mh);
|
||||
curl_close($ch);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal blocking transport
|
||||
*/
|
||||
private function generateInternal(string $prompt): string
|
||||
{
|
||||
$payload = $this->buildPayload($prompt, false);
|
||||
|
||||
$ch = curl_init($this->apiUrl);
|
||||
if ($ch === false) {
|
||||
throw new RuntimeException('Failed to initialize cURL');
|
||||
}
|
||||
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
|
||||
CURLOPT_POSTFIELDS => $payload,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_TIMEOUT => $this->timeoutSeconds,
|
||||
]);
|
||||
|
||||
$response = curl_exec($ch);
|
||||
|
||||
if ($response === false) {
|
||||
throw new RuntimeException('LLM error: ' . curl_error($ch));
|
||||
}
|
||||
|
||||
curl_close($ch);
|
||||
|
||||
$json = json_decode($response, true, flags: JSON_THROW_ON_ERROR);
|
||||
|
||||
return $json['response'] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Central Payload Builder (DRY)
|
||||
*/
|
||||
private function buildPayload(string $prompt, bool $stream): string
|
||||
{
|
||||
return json_encode([
|
||||
'model' => $this->model,
|
||||
'prompt' => $prompt,
|
||||
'stream' => $stream,
|
||||
'options' => $this->buildOptions()
|
||||
], JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Central Options Builder (DRY)
|
||||
*/
|
||||
private function buildOptions(): array
|
||||
{
|
||||
$config = $this->getConfig();
|
||||
|
||||
return [
|
||||
'temperature' => $config->getTemperature(),
|
||||
'top_k' => $config->getTopK(),
|
||||
'top_p' => $config->getTopP(),
|
||||
'repeat_penalty' => $config->getRepeatPenalty(),
|
||||
'num_ctx' => $config->getNumCtx(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Config caching per request
|
||||
*/
|
||||
private function getConfig(): ModelGenerationConfig
|
||||
{
|
||||
if ($this->cachedConfig === null) {
|
||||
$this->cachedConfig = $this->configProvider->getActiveForModel($this->model);
|
||||
}
|
||||
|
||||
return $this->cachedConfig;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user