Neuron Langkah Streaming
Alirkan satu permintaan provider dengan cakupan terbatas dan simpan potongan-potongannya sebagai keluaran sementara untuk klaim saat ini. Generasi model yang terputus tidak dapat dilanjutkan dari offset token sembarang. Percobaan ulang memulai upaya baru; potongan parsial lama bukan draf yang selesai. Ini mengikuti kontrak integrasi yang ditetapkan.
Alirkan generator provider
namespace App\Neuron;
use NeuronAI\Chat\Messages\Stream\Chunks\ReasoningChunk;
use NeuronAI\Chat\Messages\Stream\Chunks\TextChunk;
use NeuronAI\Chat\Messages\ToolCallMessage;
use NeuronAI\Chat\Messages\UserMessage;
use NeuronAI\Providers\AIProviderInterface;
use Queuety\ExecutionContext;
final class ProviderStream
{
public static function text(
AIProviderInterface $provider,
string $prompt,
array $existing_chunks = [],
): \Generator {
if ($existing_chunks !== []) {
throw new \RuntimeException('Partial output requires an explicit new attempt.');
}
ExecutionContext::assert_active();
$stream = $provider->stream(new UserMessage($prompt));
foreach ($stream as $chunk) {
ExecutionContext::assert_active();
if ($chunk instanceof TextChunk && !$chunk instanceof ReasoningChunk) {
yield ['type' => 'text', 'text' => $chunk->content];
}
}
ExecutionContext::assert_active();
$message = $stream->getReturn()->message();
if ($message instanceof ToolCallMessage) {
throw new \RuntimeException('This text-only stream does not accept tools.');
}
yield ['type' => 'complete'];
}
public static function complete(array $chunks): array
{
$last = $chunks === [] ? null : $chunks[array_key_last($chunks)];
if (!is_array($last) || ($last['type'] ?? null) !== 'complete') {
throw new \RuntimeException('The stream has no completion marker.');
}
$text = '';
foreach ($chunks as $chunk) {
if (is_array($chunk) && ($chunk['type'] ?? null) === 'text') {
$text .= $chunk['text'];
}
}
return ['draft_markdown' => $text];
}
}
Nilai yang dikembalikan generator pada akhirnya adalah sebuah ProviderResponse. Hanya akhir normal dengan respons teks final yang menghasilkan penanda penyelesaian. Potongan penalaran dan argumen alat tidak dipublikasikan sebagai teks draf. Queuety tetap memvalidasi klaim aktif sebelum melakukan commit pada state alur kerja final.
Bungkus dalam langkah Queuety
namespace App\Workflow\Steps;
use App\Ai\Credentials;
use App\Neuron\ProviderFactory;
use App\Neuron\ProviderStream;
use Queuety\Contracts\StreamingStep;
final class StreamMemoDraftStep implements StreamingStep
{
public function stream(array $state, array $existing_chunks = []): \Generator
{
$credential = Credentials::forRun($state['credential_reference']);
$provider = ProviderFactory::make(
$credential->provider,
$credential->key,
$credential->model,
);
yield from ProviderStream::text($provider, $state['prompt'], $existing_chunks);
}
public function on_complete(array $chunks, array $state): array
{
return ProviderStream::complete($chunks);
}
public function config(): array
{
return ['max_attempts' => 1];
}
}
Langkah ini menggunakan klien HTTP dengan sisa anggaran yang sama seperti Planner / Executor. Heartbeat atau callback potongan tidak dapat menginterupsi pembacaan jaringan yang terblokir; batas waktu koneksi dan total klien membatasi waktu tunggu tersebut secara kooperatif. Permintaan harus muat dalam anggaran satu tindakan. Streaming bersifat opsional untuk eksekusi di hosting bersama.
Tampilkan potongan sementara beserta identitas upayanya dan jadikan potongan tersebut final hanya setelah tugas selesai. Jika worker berhenti, aplikasi dapat secara eksplisit mengizinkan generasi baru setelah merekonsiliasi catatan permintaannya; hal ini dapat menimbulkan biaya provider tambahan. Jangan pernah mengembalikan hasil lebih awal hanya karena ada potongan sebelumnya.
Lihat langkah streaming dan orkestrasi agen.