コンテンツへスキップ

Neuronのプランナー/エグゼキューター

1つのQueuetyワークフローで、1つの専門推論を実行します。親ワークフローはタスクを計画し、子ワークフローを開始して、永続化された結果を待ちます。Neuronは、2つ目のエージェントループを実行せずにプロバダーアダプターを提供します。この例では、固定済みの統合契約を使用します。

上限付きのHTTPクライアントを使うプロバイダー

PHP
namespace App\Neuron;

use NeuronAI\Agent\SystemPrompt;
use NeuronAI\HttpClient\Curl\CurlHttpClient;
use NeuronAI\HttpClient\HttpClientInterface;
use NeuronAI\Providers\AIProviderInterface;
use NeuronAI\Providers\Anthropic\Anthropic;
use NeuronAI\Providers\Gemini\Gemini;
use NeuronAI\Providers\OpenAI\Responses\OpenAIResponses;
use Queuety\ExecutionContext;

final class ProviderFactory
{
    public static function make(
        string $name,
        string $key,
        string $model,
        ?HttpClientInterface $http = null,
    ): AIProviderInterface {
        $remaining = ExecutionContext::remaining_seconds() ?? 20.0;
        $timeout = min(12.0, $remaining - 2.0);
        if ($timeout <= 0.0) {
            throw new \RuntimeException('No provider budget remains.');
        }
        $http ??= new CurlHttpClient(
            timeout: $timeout,
            connectTimeout: min(2.0, $timeout),
        );
        $provider = match ($name) {
            'anthropic' => new Anthropic($key, $model, httpClient: $http),
            'gemini' => new Gemini($key, $model, httpClient: $http),
            'openai' => new OpenAIResponses($key, $model, httpClient: $http),
            default => throw new \InvalidArgumentException('Unknown provider.'),
        };
        return $provider->systemPrompt((string) new SystemPrompt(
            background: ['Return a concise summary of the assigned topic.'],
        ));
    }
}

アクションごとに新しいプロバイダーを作成します。本番環境では、注入するクライアントが残りの予算にも同じ上限を適用する必要があります。ツールを要求するレスポンスは保存し、Queuetyの次のアクションに引き渡します。プロバイダーアダプターはツールを実行しません。

推論ステップを1回実行

PHP
namespace App\Workflow\Steps;

use App\Ai\Credentials;
use App\Neuron\ProviderFactory;
use NeuronAI\Chat\Messages\ToolCallMessage;
use NeuronAI\Chat\Messages\UserMessage;
use Queuety\ExecutionContext;
use Queuety\Step;

final class ResearchTopicStep implements Step
{
    public function handle(array $state): array
    {
        $credential = Credentials::forRun($state['credential_reference']);
        $provider = ProviderFactory::make(
            $credential->provider,
            $credential->key,
            $credential->model,
        );
        ExecutionContext::assert_active();
        $response = $provider->chat(new UserMessage(
            sprintf('Research %s for brief %d.', $state['topic'], $state['brief_id'])
        ));
        ExecutionContext::assert_active();
        $message = $response->message();
        if ($message instanceof ToolCallMessage) {
            throw new \RuntimeException('This text-only step does not accept tools.');
        }
        return ['summary' => $message->getContent()];
    }

    public function config(): array
    {
        return ['max_attempts' => 1];
    }
}

試行を1回にすることで、結果が不明な有料リクエストを暗黙に再送信するのを防げます。リクエスト記録を照合した後、アプリケーション側で明示的に再試行できます。呼び出しの前後で要求の検証を行い、永続化された結果を保護できますが、すでに送信したプロバイダーリクエストを取り消すことはできません。

独立した専門ワークフローをディスパッチ

PHP
use Queuety\Enums\WaitMode;
use Queuety\Queuety;

$specialist = Queuety::workflow('research_agent_run')
    ->then(ResearchTopicStep::class);

Queuety::workflow('brief_research')
    ->version('brief-research.v2')
    ->then(PlanResearchTasksStep::class)
    ->start_agents('agent_tasks', $specialist)
    ->wait_for_agents(mode: WaitMode::All, result_key: 'agent_results')
    ->then(SynthesizeBriefStep::class)
    ->dispatch([
        'brief_id' => 42,
        'credential_reference' => 'research-service',
    ]);

PlanResearchTasksStepは、たとえば[['topic' => 'pricing'], ['topic' => 'customer reviews']]のようなagent_tasksを提供します。各タスクは個別に調査できるワークフローになります。親はagent_resultsのもとで、完了した子の状態を引き継いで再開します。計画と統合を担うクラスはアプリケーションのステップです。これらでモデルを呼び出す場合も、同じく1リクエストのルールに従います。

エージェントのオーケストレーションと非同期の引き継ぎを参照してください。