Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/Responses/Chat/CreateStreamedResponseChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,38 @@ final class CreateStreamedResponseChoice
private function __construct(
public readonly int $index,
public readonly CreateStreamedResponseDelta $delta,
public readonly ?CreateResponseChoiceLogprobs $logprobs,
public readonly ?string $finishReason,
) {}

/**
* @param array{index: int, delta?: array{role?: string, content?: string}, finish_reason: string|null} $attributes
* @param array{index: int, delta?: array{role?: string, content?: string}, logprobs?: ?array{content: ?array<int, array{token: string, logprob: float, bytes: ?array<int, int>}>}, finish_reason: string|null} $attributes
*/
public static function from(array $attributes): self
{
return new self(
$attributes['index'],
CreateStreamedResponseDelta::from($attributes['delta'] ?? []),
isset($attributes['logprobs']) ? CreateResponseChoiceLogprobs::from($attributes['logprobs']) : null,
$attributes['finish_reason'] ?? null,
);
}

/**
* @return array{index: int, delta: array{role?: string, content?: string}|array{role?: string, content: null, function_call: array{name?: string, arguments?: string}}, finish_reason: string|null}
* @return array{index: int, delta: array{role?: string, content?: string}|array{role?: string, content: null, function_call: array{name?: string, arguments?: string}}, logprobs?: array{content: ?array<int, array{token: string, logprob: float, bytes: ?array<int, int>}>}, finish_reason: string|null}
*/
public function toArray(): array
{
return [
$result = [
'index' => $this->index,
'delta' => $this->delta->toArray(),
'finish_reason' => $this->finishReason,
];

if ($this->logprobs !== null) {
$result['logprobs'] = $this->logprobs->toArray();
}

return $result;
}
}
39 changes: 39 additions & 0 deletions tests/Responses/Chat/CreateStreamedResponseChoice.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use OpenAI\Responses\Chat\CreateResponseChoiceLogprobs;
use OpenAI\Responses\Chat\CreateStreamedResponseChoice;
use OpenAI\Responses\Chat\CreateStreamedResponseDelta;

Expand Down Expand Up @@ -39,3 +40,41 @@
expect($result->toArray())
->toBe(chatCompletionStreamFirstChunk()['choices'][0]);
});

test('from with logprobs', function () {
$data = chatCompletionStreamContentChunk()['choices'][0];
$data['logprobs'] = [
'content' => [
['token' => 'Hello', 'logprob' => -0.31, 'bytes' => [72, 101, 108, 108, 111], 'top_logprobs' => []],
],
'refusal' => null,
];

$result = CreateStreamedResponseChoice::from($data);

expect($result->logprobs)
->toBeInstanceOf(CreateResponseChoiceLogprobs::class)
->content->toHaveCount(1)
->content->{0}->token->toBe('Hello')
->content->{0}->logprob->toBe(-0.31);
});

test('from without logprobs', function () {
$result = CreateStreamedResponseChoice::from(chatCompletionStreamContentChunk()['choices'][0]);

expect($result->logprobs)->toBeNull();
});

test('to array with logprobs', function () {
$data = chatCompletionStreamContentChunk()['choices'][0];
$data['logprobs'] = [
'content' => [
['token' => 'Hello', 'logprob' => -0.31, 'bytes' => [72, 101, 108, 108, 111], 'top_logprobs' => []],
],
];

$array = CreateStreamedResponseChoice::from($data)->toArray();

expect($array['logprobs']['content'][0])
->toMatchArray(['token' => 'Hello', 'logprob' => -0.31]);
});
Loading