2026-05-17 22:43:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Infrastructure\AI\Agent;
|
|
|
|
|
|
|
|
|
|
use App\Infrastructure\AI\OllamaClientInterface;
|
2026-05-18 07:19:02 +00:00
|
|
|
use App\Infrastructure\AI\PromptTemplateService;
|
2026-05-17 22:43:47 +00:00
|
|
|
|
|
|
|
|
final class OllamaVisionAgent
|
|
|
|
|
{
|
|
|
|
|
public function __construct(
|
|
|
|
|
private readonly OllamaClientInterface $ollama,
|
2026-05-18 07:19:02 +00:00
|
|
|
private readonly PromptTemplateService $prompts,
|
2026-05-17 22:43:47 +00:00
|
|
|
private readonly string $model,
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-18 10:09:26 +00:00
|
|
|
/** @return array{manufacturer: string, modelName: string, modelNumber: string, serial: string} */
|
2026-05-17 22:43:47 +00:00
|
|
|
public function analyze(string $imagePath): array
|
|
|
|
|
{
|
2026-05-18 07:19:02 +00:00
|
|
|
$prompt = $this->prompts->render('vision_analyze');
|
2026-05-17 22:43:47 +00:00
|
|
|
|
|
|
|
|
$response = $this->ollama->generateWithImage($this->model, $prompt, $imagePath);
|
|
|
|
|
|
|
|
|
|
return [
|
2026-05-18 07:19:02 +00:00
|
|
|
'manufacturer' => $this->extractField($response, 'MANUFACTURER'),
|
2026-05-18 10:09:26 +00:00
|
|
|
'modelName' => $this->extractField($response, 'MODEL_NAME'),
|
|
|
|
|
'modelNumber' => $this->extractField($response, 'MODEL_NUMBER'),
|
2026-05-17 22:43:47 +00:00
|
|
|
'serial' => $this->extractField($response, 'SERIAL'),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function extractField(string $response, string $field): string
|
|
|
|
|
{
|
|
|
|
|
if (preg_match('/^'.$field.':\s*(.+)$/m', $response, $matches)) {
|
|
|
|
|
return trim($matches[1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
}
|