Skip to content

Fix: the toArray() on streamed chat response deltas should only filter null #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 20, 2023
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
8 changes: 4 additions & 4 deletions src/Responses/Chat/CreateStreamedResponseDelta.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public static function from(array $attributes): self
*/
public function toArray(): array
{
return array_filter([
'role' => $this->role,
'content' => $this->content,
]);
return array_filter([
'role' => $this->role,
'content' => $this->content,
], fn ($value): bool => ! is_null($value));
}
}
38 changes: 38 additions & 0 deletions tests/Fixtures/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,44 @@ function chatCompletion(): array
];
}

function chatCompletionStreamFirstChunk(): array
{
return [
'id' => 'chatcmpl-6wdIE4DsUtqf1srdMTsfkJp0VWZgz',
'object' => 'chat.completion.chunk',
'created' => 1679432086,
'model' => 'gpt-4-0314',
'choices' => [
[
'index' => 0,
'delta' => [
'role' => 'assistant',
],
'finish_reason' => null,
],
],
];
}

function chatCompletionStreamContentChunk(): array
{
return [
'id' => 'chatcmpl-6wdIE4DsUtqf1srdMTsfkJp0VWZgz',
'object' => 'chat.completion.chunk',
'created' => 1679432086,
'model' => 'gpt-4-0314',
'choices' => [
[
'index' => 0,
'delta' => [
'content' => 'Hello',
],
'finish_reason' => null,
],
],
];
}

/**
* @return resource
*/
Expand Down
20 changes: 20 additions & 0 deletions tests/Responses/Chat/CreateStreamdResponseChoice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

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

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

expect($result)
->index->toBe(0)
->delta->toBeInstanceOf(CreateStreamedResponseDelta::class)
->finishReason->toBeIn(['stop', null]);
});

test('to array', function () {
$result = CreateStreamedResponseChoice::from(chatCompletionStreamFirstChunk()['choices'][0]);

expect($result->toArray())
->toBe(chatCompletionStreamFirstChunk()['choices'][0]);
});
28 changes: 28 additions & 0 deletions tests/Responses/Chat/CreateStreamedResponse.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
<?php

use OpenAI\Responses\Chat\CreateStreamedResponse;
use OpenAI\Responses\Chat\CreateStreamedResponseChoice;

test('from', function () {
$completion = CreateStreamedResponse::from(chatCompletionStreamFirstChunk());

expect($completion)
->toBeInstanceOf(CreateStreamedResponse::class)
->id->toBe('chatcmpl-6wdIE4DsUtqf1srdMTsfkJp0VWZgz')
->object->toBe('chat.completion.chunk')
->created->toBe(1679432086)
->model->toBe('gpt-4-0314')
->choices->toBeArray()->toHaveCount(1)
->choices->each->toBeInstanceOf(CreateStreamedResponseChoice::class);
});

test('as array accessible', function () {
$completion = CreateStreamedResponse::from(chatCompletionStreamFirstChunk());

expect($completion['id'])->toBe('chatcmpl-6wdIE4DsUtqf1srdMTsfkJp0VWZgz');
});

test('to array', function () {
$completion = CreateStreamedResponse::from(chatCompletionStreamFirstChunk());

expect($completion->toArray())
->toBeArray()
->toBe(chatCompletionStreamFirstChunk());
});

test('fake', function () {
$response = CreateStreamedResponse::fake();
Expand Down
33 changes: 33 additions & 0 deletions tests/Responses/Chat/CreateStreamedResponseDelta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use OpenAI\Responses\Chat\CreateStreamedResponseDelta;

test('from first chunk', function () {
$result = CreateStreamedResponseDelta::from(chatCompletionStreamFirstChunk()['choices'][0]['delta']);

expect($result)
->role->toBe('assistant')
->content->toBeNull();
});

test('from content chunk', function () {
$result = CreateStreamedResponseDelta::from(chatCompletionStreamContentChunk()['choices'][0]['delta']);

expect($result)
->role->toBeNull()
->content->toBe('Hello');
});

test('to array from first chunk', function () {
$result = CreateStreamedResponseDelta::from(chatCompletionStreamFirstChunk()['choices'][0]['delta']);

expect($result->toArray())
->toBe(chatCompletionStreamFirstChunk()['choices'][0]['delta']);
});

test('to array for a content chunk', function () {
$result = CreateStreamedResponseDelta::from(chatCompletionStreamContentChunk()['choices'][0]['delta']);

expect($result->toArray())
->toBe(chatCompletionStreamContentChunk()['choices'][0]['delta']);
});
2 changes: 1 addition & 1 deletion tests/Responses/FineTunes/ListEventsResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
expect($response)
->object->toBe('list')
->and($response['data'][0])
->level->toBe('info');
->level->toBe('info');
});

test('fake with override', function () {
Expand Down
2 changes: 1 addition & 1 deletion tests/Responses/Models/ListResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
$response = ListResponse::fake();

expect($response)
->object->toBe('list')
->object->toBe('list')
->and($response->data[0])
->id->toBe('text-babbage:001');
});
Expand Down
2 changes: 1 addition & 1 deletion tests/Responses/Models/RetrieveResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

expect($response)
->id->toBe('text-babbage:001')
->and($response->permission[0])
->and($response->permission[0])
->allowCreateEngine->toBeFalse();
});

Expand Down