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

46 lines
1.3 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace App\Infrastructure\AI\Agent;
use App\Infrastructure\AI\OllamaClientInterface;
final class OllamaVisionAgent
{
public function __construct(
private readonly OllamaClientInterface $ollama,
private readonly string $model,
) {
}
/** @return array{model: string, serial: string} */
public function analyze(string $imagePath): array
{
$prompt = <<<'PROMPT'
Look at this nameplate/label photo of IT hardware.
Extract ONLY the model name/designation and serial number that are visible on the label.
Do not guess or add information not on the label.
Respond in exactly this format (use empty string if not visible):
MODEL: <model name>
SERIAL: <serial number>
PROMPT;
$response = $this->ollama->generateWithImage($this->model, $prompt, $imagePath);
return [
'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 '';
}
}