Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e99655a

Browse files
committed
[Platform] Introduce Provider abstraction and model routing layer
1 parent ebb1a8d commit e99655a

3 files changed

Lines changed: 38 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* [BC BREAK] `PerplexityContract::create()` no longer accepts variadic `NormalizerInterface` arguments; pass an array instead
8+
* [BC BREAK] Rename `PlatformFactory` to `Factory` with explicit `createProvider()` and `createPlatform()` methods
89

910
0.7
1011
---
Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,59 @@
1414
use Symfony\AI\Platform\Bridge\Perplexity\Contract\PerplexityContract;
1515
use Symfony\AI\Platform\Contract;
1616
use Symfony\AI\Platform\ModelCatalog\ModelCatalogInterface;
17+
use Symfony\AI\Platform\ModelRouter\CatalogBasedModelRouter;
18+
use Symfony\AI\Platform\ModelRouterInterface;
1719
use Symfony\AI\Platform\Platform;
20+
use Symfony\AI\Platform\Provider;
21+
use Symfony\AI\Platform\ProviderInterface;
1822
use Symfony\Component\HttpClient\EventSourceHttpClient;
1923
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2024
use Symfony\Contracts\HttpClient\HttpClientInterface;
2125

2226
/**
2327
* @author Mathieu Santostefano <[email protected]>
2428
*/
25-
final class PlatformFactory
29+
final class Factory
2630
{
27-
public static function create(
31+
/**
32+
* @param non-empty-string $name
33+
*/
34+
public static function createProvider(
2835
#[\SensitiveParameter] string $apiKey,
2936
?HttpClientInterface $httpClient = null,
3037
ModelCatalogInterface $modelCatalog = new ModelCatalog(),
3138
?Contract $contract = null,
3239
?EventDispatcherInterface $eventDispatcher = null,
33-
): Platform {
40+
string $name = 'perplexity',
41+
): ProviderInterface {
3442
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
3543

36-
return new Platform(
44+
return new Provider(
45+
$name,
3746
[new ModelClient($httpClient, $apiKey)],
3847
[new ResultConverter()],
3948
$modelCatalog,
4049
$contract ?? PerplexityContract::create(),
4150
$eventDispatcher,
4251
);
4352
}
53+
54+
/**
55+
* @param non-empty-string $name
56+
*/
57+
public static function createPlatform(
58+
#[\SensitiveParameter] string $apiKey,
59+
?HttpClientInterface $httpClient = null,
60+
ModelCatalogInterface $modelCatalog = new ModelCatalog(),
61+
?Contract $contract = null,
62+
?EventDispatcherInterface $eventDispatcher = null,
63+
string $name = 'perplexity',
64+
?ModelRouterInterface $modelRouter = null,
65+
): Platform {
66+
return new Platform(
67+
[self::createProvider($apiKey, $httpClient, $modelCatalog, $contract, $eventDispatcher, $name)],
68+
$modelRouter ?? new CatalogBasedModelRouter(),
69+
$eventDispatcher,
70+
);
71+
}
4472
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,35 @@
1212
namespace Symfony\AI\Platform\Bridge\Perplexity\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\AI\Platform\Bridge\Perplexity\PlatformFactory;
15+
use Symfony\AI\Platform\Bridge\Perplexity\Factory;
1616
use Symfony\AI\Platform\Platform;
1717
use Symfony\Component\HttpClient\EventSourceHttpClient;
1818
use Symfony\Component\HttpClient\MockHttpClient;
1919

2020
/**
2121
* @author Mathieu Santostefano <[email protected]>
2222
*/
23-
final class PlatformFactoryTest extends TestCase
23+
final class FactoryTest extends TestCase
2424
{
2525
public function testItCreatesPlatformWithDefaultSettings()
2626
{
27-
$platform = PlatformFactory::create('pplx-test-api-key');
27+
$platform = Factory::createPlatform('pplx-test-api-key');
2828

2929
$this->assertInstanceOf(Platform::class, $platform);
3030
}
3131

3232
public function testItCreatesPlatformWithCustomHttpClient()
3333
{
3434
$httpClient = new MockHttpClient();
35-
$platform = PlatformFactory::create('pplx-test-api-key', $httpClient);
35+
$platform = Factory::createPlatform('pplx-test-api-key', $httpClient);
3636

3737
$this->assertInstanceOf(Platform::class, $platform);
3838
}
3939

4040
public function testItCreatesPlatformWithEventSourceHttpClient()
4141
{
4242
$httpClient = new EventSourceHttpClient(new MockHttpClient());
43-
$platform = PlatformFactory::create('pplx-test-api-key', $httpClient);
43+
$platform = Factory::createPlatform('pplx-test-api-key', $httpClient);
4444

4545
$this->assertInstanceOf(Platform::class, $platform);
4646
}

0 commit comments

Comments
 (0)