Skip to content

Commit

Permalink
Merge pull request #47 from outl1ne/update-streamhandler-chunks-filte…
Browse files Browse the repository at this point in the history
…r-join

refactor: Improve filtering logic for null values in stream handler.
  • Loading branch information
allantatter authored Jul 19, 2024
2 parents f87902b + 0845e77 commit e5116b1
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/StreamHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ public function handle()

$rawResult = $lastLine;

$newChunks = collect($lines)->map(fn ($line) => $this->convertLineToArray($line))->filter()->map(fn ($line) => new StreamedChatChunk($line));
$newChunks = collect($lines)->map(fn ($line) => $this->convertLineToArray($line))->filter(fn ($value) => $value !== null)->map(fn ($line) => new StreamedChatChunk($line));
$allChunks = $allChunks->merge($newChunks);

if ($firstLoop) {
($this->firstChunkCallback)($newChunks->first());
}

($this->streamCallback)(
$newChunks->map(fn ($streamChunk) => $streamChunk->choices[0]['delta']['content'] ?? null)->filter()->join(''),
$allChunks->map(fn ($streamChunk) => $streamChunk->choices[0]['delta']['content'] ?? null)->filter()->join(''),
$newChunks->map(fn ($streamChunk) => $streamChunk->choices[0]['delta']['content'] ?? null)->filter(fn ($value) => $value !== null)->join(''),
$allChunks->map(fn ($streamChunk) => $streamChunk->choices[0]['delta']['content'] ?? null)->filter(fn ($value) => $value !== null)->join(''),
);

$firstLoop = false;
Expand Down

0 comments on commit e5116b1

Please sign in to comment.