SuperSeller3000/src/Infrastructure/AI/Agent/OllamaVisionAgent.php

42 lines
1.1 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace App\Infrastructure\AI\Agent;
use App\Infrastructure\AI\OllamaClientInterface;
use App\Infrastructure\AI\PromptTemplateService;
final class OllamaVisionAgent
{
public function __construct(
private readonly OllamaClientInterface $ollama,
private readonly PromptTemplateService $prompts,
private readonly string $model,
) {
}
/** @return array{manufacturer: string, model: string, serial: string} */
public function analyze(string $imagePath): array
{
$prompt = $this->prompts->render('vision_analyze');
$response = $this->ollama->generateWithImage($this->model, $prompt, $imagePath);
return [
'manufacturer' => $this->extractField($response, 'MANUFACTURER'),
'model' => $this->extractField($response, 'MODEL'),
'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 '';
}
}