Добавлен сервис AI
Добавлен ai агент для формирования и проверки статистики Добавлен ai агент для поддержки
This commit is contained in:
58
app/Services/Ai/AiDispatcherService.php
Normal file
58
app/Services/Ai/AiDispatcherService.php
Normal 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,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user