Урок 03

Routing: классификация и выбор

Классификатор определяет тип запроса и выбирает специалиста.

Пример: поддержка

use function Laravel\Ai\agent;

$classification = agent(
    instructions: 'Classify customer support queries.',
    schema: fn (JsonSchema $schema) => [
        'type' => $schema->string()->required(),       // general | refund | technical
        'complexity' => $schema->string()->required(), // simple | complex
    ],
)->prompt("Classify: {$query}");

$instructions = match ($classification['type']) {
    'refund' => 'Customer service agent specialising in refund requests...',
    'technical' => 'Technical support specialist with deep product knowledge...',
    default => 'Friendly customer service agent...',
};

$agent = match ($classification['complexity']) {
    'complex' => new AdvancedSupportAgent($instructions),
    default => new StandardSupportAgent($instructions), // #[UseCheapestModel]
};

return $agent->prompt($query)->text;

UseCheapestModel

use Laravel\Ai\Attributes\UseCheapestModel;

#[UseCheapestModel]
class StandardSupportAgent implements Agent {}

Простые запросы — дешёвая модель, сложные — полная.

Когда использовать

  • Типы запросов сильно различаются
  • Сложность варьируется
  • Один prompt не покрывает всё

Итоги

  • Классификатор определяет тип и сложность
  • match выбирает инструкции и агента
  • #[UseCheapestModel] для простых
  • Экономия без потери качества