Neuron 스트리밍 단계
범위가 제한된 공급자 요청을 한 번 스트리밍하고, 그 청크를 현재 클레임의 임시 출력으로 저장합니다. 중단된 모델 생성은 임의의 토큰 위치에서 재개할 수 없습니다. 재시도하면 새 시도가 시작되며, 이전의 부분 청크는 완성된 초안이 아닙니다. 이는 고정된 통합 계약을 따릅니다.
공급자 생성기 스트리밍
PHP
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];
}
}
생성기의 최종 반환 값은 ProviderResponse입니다. 최종 텍스트 응답과 함께 정상적으로 끝난 경우에만 완료 마커를 내보냅니다. 추론 청크와 도구 인수 청크는 초안 텍스트로 게시하지 않습니다. Queuety은 최종 워크플로 상태를 커밋하기 전에 활성 클레임도 검증합니다.
Queuety 단계로 감싸기
PHP
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];
}
}
이 단계는 Planner / Executor와 동일한 잔여 예산 HTTP 클라이언트를 사용합니다. 하트비트나 청크 콜백은 차단된 네트워크 읽기를 중단할 수 없습니다. 클라이언트 연결 및 전체 시간 제한으로 해당 대기 시간을 협력적으로 제한합니다. 요청은 단일 액션 예산 안에 들어가야 합니다. 공유 호스팅 실행에서는 스트리밍이 선택 사항입니다.
임시 청크는 시도 ID와 함께 표시하고 작업이 완료된 후에만 최종 결과로 전환하세요. 작업자가 종료되면 애플리케이션은 요청 기록을 조정한 뒤 새 생성을 명시적으로 승인할 수 있으며, 이 경우 공급자 요금이 추가로 발생할 수 있습니다. 이전 청크가 일부 있다는 이유만으로 조기에 반환하지 마세요.
스트리밍 단계와 에이전트 오케스트레이션을 참조하세요.