OllamaClient.php
- Pfad:
src/Infrastructure/AI/OllamaClient.php - Namespace: Infrastructure\AI
- Zeilen: 158 | Größe: 4,571 Bytes
- Geändert: 2025-12-27 23:48:20 | Gescannt: 2025-12-31 10:22:15
Code Hygiene Score: 100
- Dependencies: 100 (25%)
- LOC: 100 (20%)
- Methods: 100 (20%)
- Secrets: 100 (15%)
- Classes: 100 (10%)
- Magic Numbers: 100 (10%)
Keine Issues gefunden.
Dependencies 3
- implements Infrastructure\AI\AIClientInterface
- use Domain\Constants
- use Infrastructure\Config\CredentialService
Klassen 1
-
OllamaClientclass Zeile 12
Funktionen 7
-
__construct()public Zeile 18 -
execute()public Zeile 28 -
isAvailable()Zeile 99 -
getClientName()Zeile 118 -
getModelName()Zeile 123 -
listModels()Zeile 128 -
estimateTokens()Zeile 153
Verwendet von 1
Versionen 6
-
v6
2025-12-27 23:48 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v5
2025-12-27 23:47 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v4
2025-12-23 08:00 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v3
2025-12-22 08:54 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v2
2025-12-22 08:53 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v1
2025-12-22 08:26 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
Code
<?php
declare(strict_types=1);
namespace Infrastructure\AI;
// @responsibility: Ollama-Client für LLM-Ausführung (Task-System)
use Domain\Constants;
use Infrastructure\Config\CredentialService;
class OllamaClient implements AIClientInterface
{
private string $baseUrl;
private string $model;
private int $timeout;
public function __construct(
?string $baseUrl = null,
string $model = 'mistral',
int $timeout = 120
) {
$this->baseUrl = rtrim($baseUrl ?? CredentialService::getOllamaHost(), '/');
$this->model = $model;
$this->timeout = $timeout;
}
public function execute(string $prompt, array $options = []): AIResponse
{
$model = $options['model'] ?? $this->model;
$startTime = microtime(true);
try {
$ch = curl_init($this->baseUrl . '/api/generate');
$payload = [
'model' => $model,
'prompt' => $prompt,
'stream' => false,
];
if (isset($options['system'])) {
$payload['system'] = $options['system'];
}
if (isset($options['temperature'])) {
$payload['options']['temperature'] = $options['temperature'];
}
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_TIMEOUT => $this->timeout,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
$durationMs = (int) ((microtime(true) - $startTime) * Constants::MS_PER_SECOND);
if ($response === false || $error !== '') {
return AIResponse::error("cURL Error: {$error}", $model);
}
if ($httpCode !== 200) {
return AIResponse::error("HTTP Error: {$httpCode}", $model);
}
$data = json_decode($response, true);
if (!isset($data['response'])) {
return AIResponse::error('Invalid response format', $model);
}
$tokensInput = $data['prompt_eval_count'] ?? $this->estimateTokens($prompt);
$tokensOutput = $data['eval_count'] ?? $this->estimateTokens($data['response']);
return AIResponse::success(
$data['response'],
$tokensInput,
$tokensOutput,
$durationMs,
$model,
[
'total_duration' => $data['total_duration'] ?? null,
'load_duration' => $data['load_duration'] ?? null,
'eval_duration' => $data['eval_duration'] ?? null,
]
);
} catch (\Exception $e) {
return AIResponse::error($e->getMessage(), $model);
}
}
public function isAvailable(): bool
{
try {
$ch = curl_init($this->baseUrl . '/api/tags');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 5,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $httpCode === 200;
} catch (\Exception $e) {
return false;
}
}
public function getClientName(): string
{
return 'ollama';
}
public function getModelName(): string
{
return $this->model;
}
public function listModels(): array
{
try {
$ch = curl_init($this->baseUrl . '/api/tags');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($response === false || $httpCode !== 200) {
return [];
}
$data = json_decode($response, true);
return array_column($data['models'] ?? [], 'name');
} catch (\Exception $e) {
return [];
}
}
private function estimateTokens(string $text): int
{
return max(1, (int) (strlen($text) / 4));
}
}