-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFailoverPlatform.php
More file actions
89 lines (74 loc) · 3.01 KB
/
FailoverPlatform.php
File metadata and controls
89 lines (74 loc) · 3.01 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
<?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\Failover;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\AI\Platform\Exception\InvalidArgumentException;
use Symfony\AI\Platform\Exception\RuntimeException;
use Symfony\AI\Platform\ModelCatalog\ModelCatalogInterface;
use Symfony\AI\Platform\PlatformInterface;
use Symfony\AI\Platform\Result\DeferredResult;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Clock\MonotonicClock;
use Symfony\Component\RateLimiter\RateLimiterFactoryInterface;
/**
* @author Guillaume Loulier <personal@guillaumeloulier.fr>
*/
final class FailoverPlatform implements PlatformInterface
{
/**
* @var \WeakMap<PlatformInterface, int>
*/
private readonly \WeakMap $failedPlatforms;
/**
* @param PlatformInterface[] $platforms
*/
public function __construct(
private readonly iterable $platforms,
private readonly RateLimiterFactoryInterface $rateLimiterFactory,
private readonly ClockInterface $clock = new MonotonicClock(),
private readonly LoggerInterface $logger = new NullLogger(),
) {
if ([] === $platforms) {
throw new InvalidArgumentException(\sprintf('"%s" must have at least one platform configured.', self::class));
}
$this->failedPlatforms = new \WeakMap();
}
public function invoke(string $model, object|array|string $input, array $options = []): DeferredResult
{
return $this->do(static fn (PlatformInterface $platform): DeferredResult => $platform->invoke($model, $input, $options));
}
public function getModelCatalog(): ModelCatalogInterface
{
return $this->do(static fn (PlatformInterface $platform): ModelCatalogInterface => $platform->getModelCatalog());
}
private function do(\Closure $func): DeferredResult|ModelCatalogInterface
{
foreach ($this->platforms as $platform) {
$limiter = $this->rateLimiterFactory->create($platform::class);
try {
if ($limiter->consume()->isAccepted() && $this->failedPlatforms->offsetExists($platform)) {
$this->failedPlatforms->offsetUnset($platform);
}
return $func($platform);
} catch (\Throwable $throwable) {
$limiter->consume();
$this->failedPlatforms->offsetSet($platform, $this->clock->now()->getTimestamp());
$this->logger->error('The {platform} platform failed due to an error/exception: {message}', [
'platform' => $platform::class,
'message' => $throwable->getMessage(),
'exception' => $throwable,
]);
continue;
}
}
throw new RuntimeException('All platforms failed.');
}
}