-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCachePlatform.php
More file actions
149 lines (126 loc) · 5.76 KB
/
Copy pathCachePlatform.php
File metadata and controls
149 lines (126 loc) · 5.76 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
142
143
144
145
146
147
148
149
<?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\Model;
use Symfony\AI\Platform\ModelCatalog\ModelCatalogInterface;
use Symfony\AI\Platform\PlainConverter;
use Symfony\AI\Platform\PlatformInterface;
use Symfony\AI\Platform\Result\DeferredResult;
use Symfony\AI\Platform\Result\InMemoryRawResult;
use Symfony\AI\Platform\Result\ResultInterface;
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Clock\MonotonicClock;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\String\UnicodeString;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
/**
* @author Guillaume Loulier <personal@guillaumeloulier.fr>
*/
final class CachePlatform implements PlatformInterface
{
/**
* @param iterable<CacheKeyGenerator> $cacheKeyGenerators Tried in order to key non-scalar inputs (objects)
*/
public function __construct(
private readonly PlatformInterface $platform,
private readonly ClockInterface $clock = new MonotonicClock(),
private readonly (CacheInterface&TagAwareAdapterInterface)|null $cache = null,
private readonly SerializerInterface&NormalizerInterface&DenormalizerInterface $serializer = new Serializer([
new ResultNormalizer(new ObjectNormalizer(
propertyTypeExtractor: new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]),
classDiscriminatorResolver: new ClassDiscriminatorFromClassMetadata(new ClassMetadataFactory(new AttributeLoader())),
)),
], [new JsonEncoder()]),
private readonly ?string $cacheKey = null,
private readonly ?int $cacheTtl = null,
private iterable $cacheKeyGenerators = [
new MessageBagCacheKeyGenerator(),
new DocumentUrlCacheKeyGenerator(),
new ImageUrlCacheKeyGenerator(),
new FileCacheKeyGenerator(),
],
) {
}
public function invoke(string|Model $model, array|string|object $input, array $options = []): DeferredResult
{
if (null === $this->cache || !\array_key_exists('prompt_cache_key', $options) || '' === $options['prompt_cache_key']) {
return $this->platform->invoke($model, $input, $options);
}
$modelName = $model instanceof Model ? $model->getName() : $model;
$normalizedInput = match (true) {
\is_string($input) => md5($input),
\is_array($input) => json_encode($input),
default => $this->generateInputCacheKey($input),
};
$cacheKey = (new UnicodeString())->join([
$options['prompt_cache_key'] ?? $this->cacheKey,
(new UnicodeString($modelName))->camel(),
$normalizedInput,
]);
$ttl = $options['prompt_cache_ttl'] ?? $this->cacheTtl;
unset($options['prompt_cache_key'], $options['prompt_cache_ttl']);
$cached = $this->cache->get($cacheKey, function (ItemInterface $item) use ($model, $modelName, $input, $options, $cacheKey, $ttl): array {
$item->tag((new UnicodeString($modelName))->camel());
if (null !== $ttl) {
$item->expiresAfter($ttl);
}
$deferredResult = $this->platform->invoke($model, $input, $options);
$result = $deferredResult->getResult();
return [
'result' => $this->serializer->normalize($result),
'raw_data' => $deferredResult->getRawResult()->getData(),
'metadata' => $result->getMetadata()->all(),
'cached_at' => $this->clock->now()->getTimestamp(),
'cache_key' => $cacheKey,
];
});
$restoredResult = $this->serializer->denormalize($cached['result'], ResultInterface::class);
$restoredResult->getMetadata()->set([
...$cached['metadata'],
'cached' => true,
'cache_key' => $cached['cache_key'],
'cached_at' => $cached['cached_at'],
]);
$result = new DeferredResult(
new PlainConverter($restoredResult),
new InMemoryRawResult($cached['raw_data']),
$options,
);
$result->getMetadata()->merge($restoredResult->getMetadata());
return $result;
}
public function getModelCatalog(): ModelCatalogInterface
{
return $this->platform->getModelCatalog();
}
private function generateInputCacheKey(object $input): string
{
foreach ($this->cacheKeyGenerators as $generator) {
if ($generator->supports($input)) {
return $generator->generate($input);
}
}
throw new InvalidArgumentException(\sprintf('Unsupported input type: "%s".', get_debug_type($input)));
}
}