-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResultNormalizer.php
More file actions
141 lines (131 loc) · 6.05 KB
/
Copy pathResultNormalizer.php
File metadata and controls
141 lines (131 loc) · 6.05 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\AI\Platform\Bridge\Cache;
use Symfony\AI\Platform\Exception\InvalidArgumentException;
use Symfony\AI\Platform\Result\BinaryResult;
use Symfony\AI\Platform\Result\ChoiceResult;
use Symfony\AI\Platform\Result\MultiPartResult;
use Symfony\AI\Platform\Result\ObjectResult;
use Symfony\AI\Platform\Result\ResultInterface;
use Symfony\AI\Platform\Result\StreamResult;
use Symfony\AI\Platform\Result\TextResult;
use Symfony\AI\Platform\Result\ThinkingResult;
use Symfony\AI\Platform\Result\ToolCall;
use Symfony\AI\Platform\Result\ToolCallResult;
use Symfony\AI\Platform\Result\VectorResult;
use Symfony\AI\Platform\Vector\Vector;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
/**
* @author Guillaume Loulier <personal@guillaumeloulier.fr>
*/
final class ResultNormalizer implements NormalizerInterface, DenormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;
public function __construct(
private readonly ObjectNormalizer $objectNormalizer,
) {
}
/**
* @return array{
* class: string,
* payload: array<string, mixed>,
* }
*/
public function normalize(mixed $data, ?string $format = null, array $context = []): array
{
return [
'class' => $data::class,
'payload' => match ($data::class) {
BinaryResult::class => [
'asBase64' => $data->toBase64(),
'mimeType' => $data->getMimeType(),
],
ChoiceResult::class => array_map(
fn (ResultInterface $result): array => $this->normalize($result, $format, $context),
$data->getContent(),
),
MultiPartResult::class => array_map(
fn (ResultInterface $result): array => $this->normalize($result, $format, $context),
$data->getContent(),
),
ThinkingResult::class => [
'content' => $data->getContent(),
'signature' => $data->getSignature(),
],
ObjectResult::class => [
'type' => get_debug_type($data->getContent()),
'content' => \is_array($data->getContent()) ? $data->getContent() : $this->objectNormalizer->normalize($data->getContent(), $format, $context),
],
StreamResult::class => throw new InvalidArgumentException(\sprintf('"%s" cannot be normalized.', StreamResult::class)),
TextResult::class => $data->getContent(),
ToolCallResult::class => array_map(
fn (ToolCall $toolCall): array => $this->normalizer->normalize($toolCall, $format, $context),
$data->getContent(),
),
VectorResult::class => array_map(
static fn (Vector $vector): array => [
'data' => $vector->getData(),
'dimensions' => $vector->getDimensions(),
],
$data->getContent(),
),
default => throw new InvalidArgumentException(\sprintf('Unsupported result type: "%s".', $data::class)),
},
];
}
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return $data instanceof ResultInterface;
}
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): ResultInterface
{
return match ($data['class']) {
BinaryResult::class => BinaryResult::fromBase64($data['payload']['asBase64'], $data['payload']['mimeType']),
ChoiceResult::class => new ChoiceResult(array_map(
fn (array $choice): ResultInterface => $this->denormalize($choice, $type, $format, $context),
$data['payload']
)),
MultiPartResult::class => new MultiPartResult(array_map(
fn (array $part): ResultInterface => $this->denormalize($part, $type, $format, $context),
$data['payload']
)),
ThinkingResult::class => new ThinkingResult($data['payload']['content'], $data['payload']['signature']),
ObjectResult::class => new ObjectResult('array' === $data['payload']['type'] ? $data['payload']['content'] : $this->objectNormalizer->denormalize($data['payload']['content'], $data['payload']['type'], $format, $context)),
TextResult::class => new TextResult($data['payload']),
ToolCallResult::class => new ToolCallResult(array_map(
static fn (array $toolCall): ToolCall => new ToolCall(
$toolCall['id'],
$toolCall['function']['name'],
json_decode($toolCall['function']['arguments'], true),
),
$data['payload'],
)),
VectorResult::class => new VectorResult(array_map(
static fn (array $vector): Vector => new Vector($vector['data'], $vector['dimensions']),
$data['payload'],
)),
default => throw new InvalidArgumentException(\sprintf('Unsupported result type: %s.', get_debug_type($data['class']))),
};
}
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
{
return ResultInterface::class === $type;
}
public function getSupportedTypes(?string $format): array
{
return [
ResultInterface::class => true,
];
}
}