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-18 08:35:52 +00:00
|
|
|
use App\Infrastructure\Search\WebSearchInterface;
|
2026-05-17 22:43:47 +00:00
|
|
|
|
|
|
|
|
final class SpecsResearchAgent
|
|
|
|
|
{
|
|
|
|
|
public function __construct(
|
2026-05-18 07:19:02 +00:00
|
|
|
private readonly OllamaClientInterface $client,
|
|
|
|
|
private readonly PromptTemplateService $prompts,
|
2026-05-18 08:35:52 +00:00
|
|
|
private readonly WebSearchInterface $search,
|
2026-05-17 22:43:47 +00:00
|
|
|
private readonly string $model,
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-18 11:00:41 +00:00
|
|
|
/**
|
|
|
|
|
* @return array{specsText: string, correctedModelNumber: string}
|
|
|
|
|
*/
|
|
|
|
|
public function research(string $modelName, string $articleTypeName, string $manufacturer = ''): array
|
2026-05-17 22:43:47 +00:00
|
|
|
{
|
2026-05-18 07:19:02 +00:00
|
|
|
$subject = trim(($manufacturer !== '' ? $manufacturer.' ' : '').$modelName);
|
2026-05-17 22:43:47 +00:00
|
|
|
|
2026-05-18 08:35:52 +00:00
|
|
|
$searchResults = $this->search->search("{$subject} {$articleTypeName} specifications");
|
|
|
|
|
|
2026-05-18 07:19:02 +00:00
|
|
|
$prompt = $this->prompts->render('specs_research', [
|
|
|
|
|
'articleType' => $articleTypeName,
|
|
|
|
|
'subject' => $subject,
|
2026-05-18 08:35:52 +00:00
|
|
|
'searchResults' => $searchResults !== '' ? $searchResults : 'No web results available.',
|
2026-05-18 07:19:02 +00:00
|
|
|
]);
|
2026-05-17 22:43:47 +00:00
|
|
|
|
2026-05-18 08:35:52 +00:00
|
|
|
$result = $this->client->generate($this->model, $prompt);
|
2026-05-17 22:43:47 +00:00
|
|
|
|
2026-05-18 07:19:02 +00:00
|
|
|
if ('' === trim($result)) {
|
|
|
|
|
throw new \RuntimeException("No specifications found for model: {$modelName}");
|
|
|
|
|
}
|
2026-05-17 22:43:47 +00:00
|
|
|
|
2026-05-18 11:00:41 +00:00
|
|
|
return $this->parseResponse($result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return array{specsText: string, correctedModelNumber: string} */
|
|
|
|
|
private function parseResponse(string $raw): array
|
|
|
|
|
{
|
|
|
|
|
$correctedModelNumber = '';
|
|
|
|
|
|
|
|
|
|
if (preg_match('/^CORRECTED_MODEL_NUMBER:\s*(\S+)/m', $raw, $matches)) {
|
|
|
|
|
$correctedModelNumber = trim($matches[1]);
|
|
|
|
|
// Strip the line from the specs text
|
|
|
|
|
$raw = preg_replace('/^CORRECTED_MODEL_NUMBER:[^\n]*\n?/m', '', $raw) ?? $raw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'specsText' => trim($raw),
|
|
|
|
|
'correctedModelNumber' => $correctedModelNumber,
|
|
|
|
|
];
|
2026-05-17 22:43:47 +00:00
|
|
|
}
|
|
|
|
|
}
|