Neuron Passaggio in streaming
Questo esempio mostra una chiara suddivisione. Neuron trasmette la risposta dell’agente un segmento alla volta, mentre Queuety rende persistente ogni segmento tramite StreamingStep. In questo modo UI può ricevere una bozza in tempo reale, mentre il workflow continua a resistere agli arresti anomali dei worker o ai nuovi tentativi.
Quando è utile questo schema
Usatelo quando una bozza o una risposta è abbastanza lunga da volerla trasmettere in tempo reale, quando non volete perdere l’output parziale in caso di arresto del worker e quando l’artefatto finale deve comunque essere salvato nello stato del workflow al termine dello streaming.
L’agente Neuron
namespace App\Neuron;
use NeuronAI\Agent\Agent;
use NeuronAI\Providers\AIProviderInterface;
use NeuronAI\Providers\OpenAI\Responses\OpenAIResponses;
use NeuronAI\SystemPrompt;
final class DraftMemoAgent extends Agent
{
protected function provider(): AIProviderInterface
{
return new OpenAIResponses(
key: $_ENV['OPENAI_API_KEY'],
model: $_ENV['OPENAI_MODEL'],
);
}
public function instructions(): string
{
return (string) new SystemPrompt(
background: [
'You write crisp internal strategy memos.',
'Prefer short sections and operationally useful language.',
],
);
}
}
Il passaggio di streaming Queuety
namespace App\Workflow\Steps;
use App\Neuron\DraftMemoAgent;
use NeuronAI\Chat\Messages\UserMessage;
use Queuety\Contracts\StreamingStep;
use Queuety\Heartbeat;
final class StreamMemoDraftStep implements StreamingStep
{
public function stream(array $state, array $existing_chunks = []): \Generator
{
if (! empty($existing_chunks)) {
return;
}
$handler = (new DraftMemoAgent())->stream(
new UserMessage(sprintf(
"Write a strategy memo about %s for account %d.",
$state['topic'],
$state['account_id'],
))
);
foreach ($handler->events() as $i => $chunk) {
Heartbeat::beat(['chunks' => $i + 1]);
// This example assumes the agent has no tools attached, so
// Neuron will stream text/reasoning chunks only.
yield $chunk->content;
}
}
public function on_complete(array $chunks, array $state): array
{
return [
'draft_markdown' => implode('', $chunks),
];
}
public function config(): array
{
return [
'max_attempts' => 3,
];
}
}
Usarlo in un workflow
use Queuety\Queuety;
Queuety::workflow('memo_drafting')
->then(BuildDraftBriefStep::class)
->then(StreamMemoDraftStep::class)
->then(StoreMemoStep::class)
->dispatch([
'account_id' => 10,
'topic' => 'pricing changes for enterprise accounts',
]);
Perché usare Queuety invece dei soli adattatori di streaming Neuron
Gli adattatori di streaming di Neuron sono utili per i protocolli e i trasporti frontend. Queuety aggiunge qualcosa di diverso: ogni segmento prodotto viene reso persistente, i nuovi tentativi possono riprendere dai segmenti salvati in precedenza, la bozza completata viene reintegrata nello stato del workflow e lo streaming diventa un singolo passaggio duraturo in un’orchestrazione più ampia.
Questo schema è quindi adatto quando lo streaming fa parte di un processo aziendale articolato in più passaggi, non solo di una richiesta/risposta UI.
Un’avvertenza
Se il vostro agente Neuron usa strumenti durante lo streaming, gestite esplicitamente i tipi dei segmenti trasmessi. La documentazione di streaming aggiornata di Neuron descrive TextChunk, ReasoningChunk, ToolCallChunk e ToolResultChunk. Per un workflow di redazione in background, di solito la soluzione più semplice è mantenere l’agente di streaming limitato al solo testo.