Добавлен сервис AI

Добавлен ai агент для формирования и проверки статистики
Добавлен ai агент для поддержки
This commit is contained in:
brusnitsyn
2026-07-20 16:58:21 +09:00
parent 6350159943
commit a9e032f658
21 changed files with 1546 additions and 4 deletions

View File

@@ -0,0 +1,58 @@
<?php
namespace App\Services\Ai;
use App\Ai\Agents\RouterAgent;
use App\Ai\Agents\StatisticsAnalyst;
use App\Ai\Agents\SupportAgent;
use App\Services\DutyReportService;
class AiDispatcherService
{
public function __construct(
private readonly DutyReportService $reportService
) {}
public function dispatch(string $question, string $role = 'guest'): array
{
// 1. Маршрутизация
$router = new RouterAgent();
$routerResponse = $router->prompt($question)->text; // получаем текстовый ответ
// Парсим JSON из ответа роутера
$routeData = json_decode($routerResponse, true);
$route = $routeData['route'] ?? 'other';
// 2. Выбор агента
$agent = match ($route) {
'stats' => new StatisticsAnalyst($this->reportService, $role),
'support' => new SupportAgent(),
default => null,
};
if (!$agent) {
return [
'answer' => 'Извините, я не могу ответить на этот вопрос. Пожалуйста, уточните, что вы имеете в виду.',
'structured' => null,
];
}
// 3. Запрос к специалисту
$responseText = $agent->prompt($question)->text;
// Пытаемся распарсить JSON (если агент вернул структурированный ответ)
$parsed = json_decode($responseText, true);
if (json_last_error() === JSON_ERROR_NONE && isset($parsed['text'])) {
return [
'answer' => $parsed['text'],
'structured' => $parsed['data'] ?? null,
];
}
// Если не JSON возвращаем как есть
return [
'answer' => $responseText,
'structured' => null,
];
}
}