Skip to content

Commit ad7743d

Browse files
authored
fix: chat completion choices to allow responses without logprobs field (#554)
1 parent 6403060 commit ad7743d

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

src/Responses/Chat/CreateResponseChoice.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ private function __construct(
1414
) {}
1515

1616
/**
17-
* @param array{index: int, message: array{role: string, content: ?string, function_call: ?array{name: string, arguments: string}, tool_calls: ?array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}, logprobs: ?array{content: ?array<int, array{token: string, logprob: float, bytes: ?array<int, int>}>}, finish_reason: string|null} $attributes
17+
* @param array{index: int, message: array{role: string, content: ?string, function_call: ?array{name: string, arguments: string}, tool_calls: ?array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}, logprobs?: ?array{content: ?array<int, array{token: string, logprob: float, bytes: ?array<int, int>}>}, finish_reason: string|null} $attributes
1818
*/
1919
public static function from(array $attributes): self
2020
{
2121
return new self(
2222
$attributes['index'],
2323
CreateResponseMessage::from($attributes['message']),
24-
$attributes['logprobs'] ? CreateResponseChoiceLogprobs::from($attributes['logprobs']) : null,
24+
isset($attributes['logprobs']) ? CreateResponseChoiceLogprobs::from($attributes['logprobs']) : null,
2525
$attributes['finish_reason'] ?? null,
2626
);
2727
}

tests/Fixtures/Chat.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,42 @@ function chatCompletionWithoutUsage(): array
9595
];
9696
}
9797

98+
/**
99+
* @return array<string, mixed>
100+
*/
101+
function chatCompletionWithoutLogprobs(): array
102+
{
103+
return [
104+
'id' => 'chatcmpl-123',
105+
'object' => 'chat.completion',
106+
'created' => 1677652288,
107+
'model' => 'gpt-3.5-turbo',
108+
'choices' => [
109+
[
110+
'index' => 0,
111+
'message' => [
112+
'role' => 'assistant',
113+
'content' => "\n\nHello there, how may I assist you today?",
114+
],
115+
'finish_reason' => 'stop',
116+
],
117+
],
118+
'usage' => [
119+
'prompt_tokens' => 9,
120+
'completion_tokens' => 12,
121+
'total_tokens' => 21,
122+
'prompt_tokens_details' => [
123+
'cached_tokens' => 5,
124+
],
125+
'completion_tokens_details' => [
126+
'reasoning_tokens' => 0,
127+
'accepted_prediction_tokens' => 0,
128+
'rejected_prediction_tokens' => 0,
129+
],
130+
],
131+
];
132+
}
133+
98134
/**
99135
* @return array<string, mixed>
100136
*/

tests/Responses/Chat/CreateResponseChoice.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414
->finishReason->toBeIn(['stop', null]);
1515
});
1616

17+
test('from without logprobs', function () {
18+
$result = CreateResponseChoice::from(chatCompletionWithoutLogprobs()['choices'][0]);
19+
20+
expect($result)
21+
->index->toBe(0)
22+
->message->toBeInstanceOf(CreateResponseMessage::class)
23+
->logprobs->toBeNull()
24+
->finishReason->toBeIn(['stop', null]);
25+
});
26+
1727
test('from with logprobs', function () {
1828
$result = CreateResponseChoice::from(chatCompletionWithLogprobs()['choices'][0]);
1929

0 commit comments

Comments
 (0)