-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from outl1ne/wip-streaming-support
wip-streaming-support
- Loading branch information
Showing
18 changed files
with
404 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Outl1ne\NovaOpenAI\Capabilities\Chat\Responses; | ||
|
||
use Outl1ne\NovaOpenAI\Capabilities\Responses\StreamChunk; | ||
|
||
|
||
class StreamedChatChunk extends StreamChunk | ||
{ | ||
public array $choices; | ||
|
||
public function __construct($data) | ||
{ | ||
parent::__construct($data); | ||
|
||
$this->model = $this->data['model']; | ||
$this->choices = $this->data['choices']; | ||
$this->appendMeta('id', $this->data['id']); | ||
$this->appendMeta('object', $this->data['object']); | ||
$this->appendMeta('created', $this->data['created']); | ||
$this->appendMeta('system_fingerprint', $this->data['system_fingerprint']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Outl1ne\NovaOpenAI\Capabilities\Chat\Responses; | ||
|
||
use Illuminate\Http\Client\Response; | ||
use Outl1ne\NovaOpenAI\Capabilities\Responses\StreamChunk; | ||
use Outl1ne\NovaOpenAI\Capabilities\Responses\StreamResponse; | ||
|
||
class StreamedChatResponse extends StreamResponse | ||
{ | ||
public array $choices; | ||
|
||
public function __construct(Response $response, StreamChunk ...$streamChunks) | ||
{ | ||
parent::__construct($response, ...$streamChunks); | ||
|
||
$lastChunk = end($this->streamChunks); | ||
$this->model = $lastChunk->model; | ||
$this->appendMeta('id', $lastChunk->meta['id']); | ||
$this->appendMeta('object', $lastChunk->meta['object']); | ||
$this->appendMeta('system_fingerprint', $lastChunk->meta['system_fingerprint'] ?? null); | ||
|
||
$this->choices = $this->createChoices(); | ||
} | ||
|
||
protected function createChoices(): array | ||
{ | ||
$choices = []; | ||
|
||
foreach ($this->streamChunks as $chunk) { | ||
foreach ($chunk->choices as $choice) { | ||
$choices[$choice['index']]['index'] = $choice['index']; | ||
$choices[$choice['index']]['message']['role'] = $this->handleDelta($choices[$choice['index']]['message']['role'] ?? null, $choice['delta']['role'] ?? null); | ||
$choices[$choice['index']]['message']['content'] = $this->handleDelta($choices[$choice['index']]['message']['content'] ?? null, $choice['delta']['content'] ?? null); | ||
$choices[$choice['index']]['logprobs'] = $choice['logprobs']; | ||
$choices[$choice['index']]['finish_reason'] = $choice['finish_reason']; | ||
} | ||
} | ||
|
||
return $choices; | ||
} | ||
|
||
protected function handleDelta($carry, $chunk): ?string | ||
{ | ||
if ($carry === null && $chunk === null) { | ||
return null; | ||
} | ||
|
||
if ($carry === null) { | ||
return $chunk; | ||
} | ||
|
||
return $carry . $chunk; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Outl1ne\NovaOpenAI\Capabilities\Responses; | ||
|
||
class StreamChunk | ||
{ | ||
use AppendsMeta; | ||
|
||
public ?Usage $usage; | ||
|
||
public ?string $model = null; | ||
|
||
public function __construct( | ||
protected readonly array $data, | ||
) { | ||
|
||
$this->usage = $this->createUsage(); | ||
} | ||
|
||
protected function createUsage() | ||
{ | ||
$promptTokens = $this->data['usage']['prompt_tokens'] ?? null; | ||
$completionTokens = $this->data['usage']['completion_tokens'] ?? null; | ||
$totalTokens = $this->data['usage']['total_tokens'] ?? null; | ||
|
||
if ($totalTokens === null) return null; | ||
|
||
return new Usage( | ||
$promptTokens, | ||
$completionTokens, | ||
$totalTokens, | ||
); | ||
} | ||
} |
Oops, something went wrong.