-
-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathResponse.php
More file actions
77 lines (70 loc) · 2.53 KB
/
Copy pathResponse.php
File metadata and controls
77 lines (70 loc) · 2.53 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
<?php
declare(strict_types=1);
namespace Prism\Prism\Text;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;
use Prism\Prism\Contracts\Message;
use Prism\Prism\Enums\FinishReason;
use Prism\Prism\ValueObjects\Messages\AssistantMessage;
use Prism\Prism\ValueObjects\Messages\SystemMessage;
use Prism\Prism\ValueObjects\Messages\ToolResultMessage;
use Prism\Prism\ValueObjects\Messages\UserMessage;
use Prism\Prism\ValueObjects\Meta;
use Prism\Prism\ValueObjects\ToolCall;
use Prism\Prism\ValueObjects\ToolResult;
use Prism\Prism\ValueObjects\Usage;
/**
* @implements Arrayable<string, mixed>
*/
readonly class Response implements Arrayable
{
/**
* @param Collection<int, Step> $steps
* @param ToolCall[] $toolCalls
* @param ToolResult[] $toolResults
* @param Collection<int, Message> $messages
* @param array<string,mixed> $additionalContent
* @param array<string,mixed>|null $raw
*/
public function __construct(
public Collection $steps,
public string $text,
public FinishReason $finishReason,
public array $toolCalls,
public array $toolResults,
public Usage $usage,
public Meta $meta,
public Collection $messages,
public array $additionalContent = [],
public ?array $raw = null
) {}
/**
* @return array<string, mixed>
*/
#[\Override]
public function toArray(): array
{
return [
'steps' => $this->steps->map(fn (Step $step): array => $step->toArray())->toArray(),
'text' => $this->text,
'finish_reason' => $this->finishReason->value,
'tool_calls' => array_map(fn (ToolCall $toolCall): array => $toolCall->toArray(), $this->toolCalls),
'tool_results' => array_map(fn (ToolResult $toolResult): array => $toolResult->toArray(), $this->toolResults),
'usage' => $this->usage->toArray(),
'meta' => $this->meta->toArray(),
'messages' => $this->messages->map($this->messageToArray(...))->toArray(),
'additional_content' => $this->additionalContent,
'raw' => $this->raw,
];
}
/**
* @return array<string, mixed>
*/
protected function messageToArray(Message $message): array
{
if ($message instanceof UserMessage || $message instanceof AssistantMessage || $message instanceof ToolResultMessage || $message instanceof SystemMessage) {
return $message->toArray();
}
return ['type' => 'unknown'];
}
}