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
7 changes: 5 additions & 2 deletions src/Responses/Chat/CreateStreamedResponseDelta.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ final class CreateStreamedResponseDelta
private function __construct(
public readonly ?string $role,
public readonly ?string $content,
public readonly ?string $reasoningContent,
public readonly array $toolCalls,
public readonly ?CreateStreamedResponseFunctionCall $functionCall,
) {}

/**
* @param 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}}>} $attributes
* @param array{role?: string, content?: string, reasoning?: string, function_call?: array{name?: ?string, arguments?: ?string}, tool_calls?: array<int, array{id?: string, type?: string, function: array{name?: string, arguments: string}}>} $attributes
*/
public static function from(array $attributes): self
{
Expand All @@ -28,19 +29,21 @@ public static function from(array $attributes): self
return new self(
$attributes['role'] ?? null,
$attributes['content'] ?? null,
$attributes['reasoning'] ?? null,
$toolCalls,
isset($attributes['function_call']) ? CreateStreamedResponseFunctionCall::from($attributes['function_call']) : null,
);
}

/**
* @return array{role?: string, content?: string}|array{role?: string, content: null, function_call: array{name?: string, arguments?: string}}
* @return array{role?: string, content?: string, reasoning?: string}|array{role?: string, content: null, function_call: array{name?: string, arguments?: string}}
*/
public function toArray(): array
{
$data = array_filter([
'role' => $this->role,
'content' => $this->content,
'reasoning' => $this->reasoningContent,
], fn (?string $value): bool => ! is_null($value));

if ($this->functionCall instanceof CreateStreamedResponseFunctionCall) {
Expand Down
19 changes: 19 additions & 0 deletions tests/Fixtures/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,25 @@ function chatCompletionStreamVisionContentChunk(): array
];
}

function chatCompletionStreamReasoningChunk(): array
{
return [
'id' => 'chatcmpl-123',
'object' => 'chat.completion.chunk',
'created' => 1677652288,
'model' => 'deepseek-reasoner',
'choices' => [
[
'index' => 0,
'delta' => [
'reasoning' => 'Hello world',
],
'finish_reason' => null,
],
],
];
}

/**
* @return array<string, mixed>
*/
Expand Down
16 changes: 16 additions & 0 deletions tests/Responses/Chat/CreateStreamedResponseDelta.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,19 @@
expect($result->toArray())
->toBe($data);
});

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

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

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

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