-
-
Notifications
You must be signed in to change notification settings - Fork 681
Expand file tree
/
Copy pathCreateStreamedResponseDelta.php
More file actions
59 lines (50 loc) · 2.07 KB
/
CreateStreamedResponseDelta.php
File metadata and controls
59 lines (50 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
declare(strict_types=1);
namespace OpenAI\Responses\Chat;
final class CreateStreamedResponseDelta
{
/**
* @param array<int, CreateStreamedResponseToolCall> $toolCalls
*/
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
*/
public static function from(array $attributes): self
{
$toolCalls = array_map(fn (array $result): CreateStreamedResponseToolCall => CreateStreamedResponseToolCall::from(
$result
), $attributes['tool_calls'] ?? []);
return new self(
$attributes['role'] ?? null,
$attributes['content'] ?? null,
$attributes['reasoning_content'] ?? 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}}
*/
public function toArray(): array
{
$data = array_filter([
'role' => $this->role,
'content' => $this->content,
], fn (?string $value): bool => ! is_null($value));
if ($this->functionCall instanceof CreateStreamedResponseFunctionCall) {
$data['content'] = null;
$data['function_call'] = $this->functionCall->toArray();
}
if ($this->toolCalls !== []) {
$data['tool_calls'] = array_map(fn (CreateStreamedResponseToolCall $toolCall): array => $toolCall->toArray(), $this->toolCalls);
}
return $data;
}
}