Skip to content

Commit 05c406e

Browse files
authored
feat(DeepSeek): add reasoning_content field to CreateResponseMessage  (#737)
* fix: add reasoning_content to response message * fix: add reasoning_content to response message * fix: add reasoning_content to response message * add 2 test * add 2 test
1 parent 1ca579d commit 05c406e

File tree

6 files changed

+66
-5
lines changed

6 files changed

+66
-5
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}}>}, 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 */
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, reasoning_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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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, annotations?: array<int, array{type: string, url_citation: array{start_index: int, end_index: int, title: string, url: 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
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, reasoning_content?: ?string, annotations?: array<int, array{type: string, url_citation: array{start_index: int, end_index: int, title: string, url: 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ private function __construct(
1414
) {}
1515

1616
/**
17-
* @param array{index: int, message: array{role: string, content: ?string, annotations?: array<int, array{type: string, url_citation: array{start_index: int, end_index: int, title: string, url: 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, reasoning_content?: ?string, annotations?: array<int, array{type: string, url_citation: array{start_index: int, end_index: int, title: string, url: 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
{

src/Responses/Chat/CreateResponseMessage.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ final class CreateResponseMessage
1818
private function __construct(
1919
public readonly string $role,
2020
public readonly ?string $content,
21+
public readonly ?string $reasoningContent,
2122
public readonly array $annotations,
2223
public readonly array $toolCalls,
2324
public readonly ?CreateResponseFunctionCall $functionCall,
@@ -26,7 +27,7 @@ private function __construct(
2627
) {}
2728

2829
/**
29-
* @param array{role: string, content: ?string, annotations?: array<int, array{type: string, url_citation: array{start_index: int, end_index: int, title: string, url: string}}>, function_call?: array{name: string, arguments: string}, tool_calls?: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>, audio?: CreateResponseChoiceAudioType, images?: array<int, CreateResponseChoiceImageType>} $attributes
30+
* @param array{role: string, content: ?string, reasoning_content?: ?string, annotations?: array<int, array{type: string, url_citation: array{start_index: int, end_index: int, title: string, url: string}}>, function_call?: array{name: string, arguments: string}, tool_calls?: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>, audio?: CreateResponseChoiceAudioType, images?: array<int, CreateResponseChoiceImageType>} $attributes
3031
*/
3132
public static function from(array $attributes): self
3233
{
@@ -45,6 +46,7 @@ public static function from(array $attributes): self
4546
return new self(
4647
role: $attributes['role'],
4748
content: $attributes['content'] ?? null,
49+
reasoningContent: $attributes['reasoning_content'] ?? null,
4850
annotations: $annotations,
4951
toolCalls: $toolCalls,
5052
functionCall: isset($attributes['function_call']) ? CreateResponseFunctionCall::from($attributes['function_call']) : null,
@@ -54,7 +56,7 @@ functionCall: isset($attributes['function_call']) ? CreateResponseFunctionCall::
5456
}
5557

5658
/**
57-
* @return array{role: string, content: string|null, annotations?: array<int, array{type: string, url_citation: array{start_index: int, end_index: int, title: string, url: string}}>, function_call?: array{name: string, arguments: string}, tool_calls?: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>, audio?: CreateResponseChoiceAudioType, images?: array<int, CreateResponseChoiceImageType>}
59+
* @return array{role: string, content: string|null, reasoning_content?: string, annotations?: array<int, array{type: string, url_citation: array{start_index: int, end_index: int, title: string, url: string}}>, function_call?: array{name: string, arguments: string}, tool_calls?: array<int, array{id: string, type: string, function: array{name: string, arguments: string}}>, audio?: CreateResponseChoiceAudioType, images?: array<int, CreateResponseChoiceImageType>}
5860
*/
5961
public function toArray(): array
6062
{
@@ -63,6 +65,10 @@ public function toArray(): array
6365
'content' => $this->content,
6466
];
6567

68+
if ($this->reasoningContent !== null) {
69+
$data['reasoning_content'] = $this->reasoningContent;
70+
}
71+
6672
if ($this->annotations !== []) {
6773
$data['annotations'] = array_map(fn (CreateResponseChoiceAnnotations $annotations): array => $annotations->toArray(), $this->annotations);
6874
}

tests/Fixtures/Chat.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,44 @@ function chatCompletionStreamVisionContentChunk(): array
722722
];
723723
}
724724

725+
/**
726+
* @return array<string, mixed>
727+
*/
728+
function chatCompletionReasoningContent(): array
729+
{
730+
return [
731+
'id' => 'chatcmpl-123',
732+
'object' => 'chat.completion',
733+
'created' => 1677652288,
734+
'model' => 'deepseek-reasoner',
735+
'choices' => [
736+
[
737+
'index' => 0,
738+
'message' => [
739+
'role' => 'assistant',
740+
'content' => null,
741+
'reasoning_content' => 'Hello world',
742+
],
743+
'logprobs' => null,
744+
'finish_reason' => 'stop',
745+
],
746+
],
747+
'usage' => [
748+
'prompt_tokens' => 9,
749+
'completion_tokens' => 12,
750+
'total_tokens' => 21,
751+
'prompt_tokens_details' => [
752+
'cached_tokens' => 5,
753+
],
754+
'completion_tokens_details' => [
755+
'reasoning_tokens' => 12,
756+
'accepted_prediction_tokens' => 0,
757+
'rejected_prediction_tokens' => 0,
758+
],
759+
],
760+
];
761+
}
762+
725763
/**
726764
* @return resource
727765
*/

tests/Responses/Chat/CreateResponseMessage.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,24 @@
5656
->functionCall->toBeInstanceOf(CreateResponseFunctionCall::class);
5757
});
5858

59+
test('from reasoning', function () {
60+
$result = CreateResponseMessage::from(chatCompletionReasoningContent()['choices'][0]['message']);
61+
62+
expect($result)
63+
->role->toBe('assistant')
64+
->reasoningContent->toBe('Hello world')
65+
->annotations->toBeArray()
66+
->functionCall->toBeNull();
67+
});
68+
5969
test('to array', function () {
70+
$result = CreateResponseMessage::from(chatCompletionReasoningContent()['choices'][0]['message']);
71+
72+
expect($result->toArray())
73+
->toBe(chatCompletionReasoningContent()['choices'][0]['message']);
74+
});
75+
76+
test('to array from reasoning', function () {
6077
$result = CreateResponseMessage::from(chatCompletion()['choices'][0]['message']);
6178

6279
expect($result->toArray())

0 commit comments

Comments
 (0)