forked from openai-php/client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentPart.php
More file actions
79 lines (70 loc) · 2.56 KB
/
ContentPart.php
File metadata and controls
79 lines (70 loc) · 2.56 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
namespace OpenAI\Responses\Responses\Streaming;
use OpenAI\Contracts\ResponseContract;
use OpenAI\Contracts\ResponseHasMetaInformationContract;
use OpenAI\Responses\Concerns\ArrayAccessible;
use OpenAI\Responses\Concerns\HasMetaInformation;
use OpenAI\Responses\Meta\MetaInformation;
use OpenAI\Responses\Responses\Output\OutputMessageContentOutputText;
use OpenAI\Responses\Responses\Output\OutputMessageContentRefusal;
use OpenAI\Testing\Responses\Concerns\Fakeable;
/**
* @phpstan-import-type OutputTextType from OutputMessageContentOutputText
* @phpstan-import-type ContentRefusalType from OutputMessageContentRefusal
*
* @phpstan-type ContentPartType array{type: string, content_index: int, item_id: string, output_index: int, sequence_number: int, part: OutputTextType|ContentRefusalType}
*
* @implements ResponseContract<ContentPartType>
*/
final class ContentPart implements ResponseContract, ResponseHasMetaInformationContract
{
/**
* @use ArrayAccessible<ContentPartType>
*/
use ArrayAccessible;
use Fakeable;
use HasMetaInformation;
private function __construct(
public readonly string $type,
public readonly int $contentIndex,
public readonly string $itemId,
public readonly int $outputIndex,
public readonly int $sequenceNumber,
public readonly OutputMessageContentOutputText|OutputMessageContentRefusal $part,
private readonly MetaInformation $meta,
) {}
/**
* @param ContentPartType $attributes
*/
public static function from(array $attributes, MetaInformation $meta): self
{
$part = match ($attributes['part']['type']) {
'output_text' => OutputMessageContentOutputText::from($attributes['part']),
'refusal' => OutputMessageContentRefusal::from($attributes['part']),
};
return new self(
type: $attributes['type'],
contentIndex: $attributes['content_index'],
itemId: $attributes['item_id'],
outputIndex: $attributes['output_index'],
sequenceNumber: $attributes['sequence_number'],
part: $part,
meta: $meta,
);
}
/**
* {@inheritDoc}
*/
public function toArray(): array
{
return [
'type' => $this->type,
'content_index' => $this->contentIndex,
'item_id' => $this->itemId,
'output_index' => $this->outputIndex,
'sequence_number' => $this->sequenceNumber,
'part' => $this->part->toArray(),
];
}
}