Skip to content

Commit 1feb4f9

Browse files
committed
Chat response: add logprobs to CreateResponse
Now that we have our classes for logprobs (CreateResponseChoiceLogprobs and CreateResponseChoiceLogbropsContent), let's actually add logprobs in the CreateResponseChoice / CreateResponse objects. Update types, fixtures, tests.
1 parent d41d8d9 commit 1feb4f9

File tree

6 files changed

+35
-6
lines changed

6 files changed

+35
-6
lines changed

src/Resources/Chat.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function create(array $parameters): CreateResponse
2929

3030
$payload = Payload::create('chat/completions', $parameters);
3131

32-
/** @var Response<array{id: string, object: string, created: int, model: string, system_fingerprint?: string, choices: array<int, 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}}>}, finish_reason: string|null}>, usage: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}}> $response */
32+
/** @var Response<array{id: string, object: string, created: int, model: string, system_fingerprint?: string, choices: array<int, 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}>, usage: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}}> $response */
3333
$response = $this->transporter->requestObject($payload);
3434

3535
return CreateResponse::from($response->data(), $response->meta());

src/Responses/Chat/CreateResponse.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
use OpenAI\Testing\Responses\Concerns\Fakeable;
1313

1414
/**
15-
* @implements ResponseContract<array{id: string, object: string, created: int, model: string, system_fingerprint?: string, choices: array<int, array{index: int, message: array{role: string, content: string|null, function_call?: array{name: string, arguments: string}, tool_calls?: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}, finish_reason: string|null}>, usage: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}}>
15+
* @implements ResponseContract<array{id: string, object: string, created: int, model: string, system_fingerprint?: string, choices: array<int, array{index: int, message: array{role: string, content: string|null, 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}>, usage: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}}>
1616
*/
1717
final class CreateResponse implements ResponseContract, ResponseHasMetaInformationContract
1818
{
1919
/**
20-
* @use ArrayAccessible<array{id: string, object: string, created: int, model: string, system_fingerprint?: string, choices: array<int, array{index: int, message: array{role: string, content: string|null, function_call?: array{name: string, arguments: string}, tool_calls?: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}, finish_reason: string|null}>, usage: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}}>
20+
* @use ArrayAccessible<array{id: string, object: string, created: int, model: string, system_fingerprint?: string, choices: array<int, array{index: int, message: array{role: string, content: string|null, 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}>, usage: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int}}>
2121
*/
2222
use ArrayAccessible;
2323

@@ -41,7 +41,7 @@ private function __construct(
4141
/**
4242
* Acts as static factory, and returns a new Response instance.
4343
*
44-
* @param array{id: string, object: string, created: int, model: string, system_fingerprint?: string, choices: array<int, 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}}>}, finish_reason: string|null}>, usage: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int, prompt_tokens_details?:array{cached_tokens:int}, completion_tokens_details?:array{audio_tokens?:int, reasoning_tokens:int, accepted_prediction_tokens:int, rejected_prediction_tokens:int}}} $attributes
44+
* @param array{id: string, object: string, created: int, model: string, system_fingerprint?: string, choices: array<int, 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}>, usage: array{prompt_tokens: int, completion_tokens: int|null, total_tokens: int, prompt_tokens_details?:array{cached_tokens:int}, completion_tokens_details?:array{audio_tokens?:int, reasoning_tokens:int, accepted_prediction_tokens:int, rejected_prediction_tokens:int}}} $attributes
4545
*/
4646
public static function from(array $attributes, MetaInformation $meta): self
4747
{

src/Responses/Chat/CreateResponseChoice.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,32 @@ final class CreateResponseChoice
99
private function __construct(
1010
public readonly int $index,
1111
public readonly CreateResponseMessage $message,
12+
public readonly ?CreateResponseChoiceLogprobs $logprobs,
1213
public readonly ?string $finishReason,
1314
) {}
1415

1516
/**
16-
* @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}}>}, 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
1718
*/
1819
public static function from(array $attributes): self
1920
{
2021
return new self(
2122
$attributes['index'],
2223
CreateResponseMessage::from($attributes['message']),
24+
$attributes['logprobs'] ? CreateResponseChoiceLogprobs::from($attributes['logprobs']) : null,
2325
$attributes['finish_reason'] ?? null,
2426
);
2527
}
2628

2729
/**
28-
* @return array{index: int, message: array{role: string, content: string|null, function_call?: array{name: string, arguments: string}, tool_calls?: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>}, finish_reason: string|null}
30+
* @return array{index: int, message: array{role: string, content: string|null, 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}
2931
*/
3032
public function toArray(): array
3133
{
3234
return [
3335
'index' => $this->index,
3436
'message' => $this->message->toArray(),
37+
'logprobs' => $this->logprobs?->toArray(),
3538
'finish_reason' => $this->finishReason,
3639
];
3740
}

src/Testing/Responses/Fixtures/Chat/CreateResponseFixture.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ final class CreateResponseFixture
1919
'function_call' => null,
2020
'tool_calls' => [],
2121
],
22+
'logprobs' => null,
2223
'finish_reason' => 'stop',
2324
],
2425
],

tests/Fixtures/Chat.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function chatCompletion(): array
1717
'role' => 'assistant',
1818
'content' => "\n\nHello there, how may I assist you today?",
1919
],
20+
'logprobs' => null,
2021
'finish_reason' => 'stop',
2122
],
2223
],
@@ -96,6 +97,7 @@ function chatCompletionWithSystemFingerprint(): array
9697
'role' => 'assistant',
9798
'content' => "\n\nHello there, how may I assist you today?",
9899
],
100+
'logprobs' => null,
99101
'finish_reason' => 'stop',
100102
],
101103
],
@@ -128,6 +130,7 @@ function chatCompletionWithFunction(): array
128130
'arguments' => "{\n \"location\": \"Boston, MA\"\n}",
129131
],
130132
],
133+
'logprobs' => null,
131134
'finish_reason' => 'function_call',
132135
],
133136
],
@@ -166,6 +169,7 @@ function chatCompletionWithToolCalls(): array
166169
],
167170
],
168171
],
172+
'logprobs' => null,
169173
'finish_reason' => 'tool_calls',
170174
],
171175
],
@@ -208,6 +212,7 @@ function chatCompletionFromVision(): array
208212
'role' => 'assistant',
209213
'content' => 'The image shows a beautiful, tranquil natural landscape. A wooden boardwalk path stretches',
210214
],
215+
'logprobs' => null,
211216
],
212217
],
213218
'usage' => [

tests/Responses/Chat/CreateResponseChoice.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use OpenAI\Responses\Chat\CreateResponseChoice;
4+
use OpenAI\Responses\Chat\CreateResponseChoiceLogprobs;
45
use OpenAI\Responses\Chat\CreateResponseMessage;
56

67
test('from', function () {
@@ -9,6 +10,17 @@
910
expect($result)
1011
->index->toBe(0)
1112
->message->toBeInstanceOf(CreateResponseMessage::class)
13+
->logprobs->toBeNull()
14+
->finishReason->toBeIn(['stop', null]);
15+
});
16+
17+
test('from with logprobs', function () {
18+
$result = CreateResponseChoice::from(chatCompletionWithLogprobs()['choices'][0]);
19+
20+
expect($result)
21+
->index->toBe(0)
22+
->message->toBeInstanceOf(CreateResponseMessage::class)
23+
->logprobs->toBeInstanceOf(CreateResponseChoiceLogprobs::class)
1224
->finishReason->toBeIn(['stop', null]);
1325
});
1426

@@ -18,6 +30,7 @@
1830
expect($result)
1931
->index->toBe(0)
2032
->message->toBeInstanceOf(CreateResponseMessage::class)
33+
->logprobs->toBeNull()
2134
->finishReason->toBeNull();
2235
});
2336

@@ -27,3 +40,10 @@
2740
expect($result->toArray())
2841
->toBe(chatCompletion()['choices'][0]);
2942
});
43+
44+
test('to array with logprobs', function () {
45+
$result = CreateResponseChoice::from(chatCompletionWithLogprobs()['choices'][0]);
46+
47+
expect($result->toArray())
48+
->toBe(chatCompletionWithLogprobs()['choices'][0]);
49+
});

0 commit comments

Comments
 (0)