Skip to content

Commit dbd20d4

Browse files
committed
[Platform] Use new AsModel attribute to connect Model to ModelClient
Related: symfony#299
1 parent 7736543 commit dbd20d4

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Platform\Attribute;
13+
14+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
15+
16+
final readonly class AsModel
17+
{
18+
public function __construct(
19+
public string $platform,
20+
public ?string $aspect = null,
21+
) {
22+
}
23+
}

src/platform/src/Bridge/Ollama/Ollama.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111

1212
namespace Symfony\AI\Platform\Bridge\Ollama;
1313

14+
use Symfony\AI\Platform\Attribute\AsModel;
1415
use Symfony\AI\Platform\Capability;
1516
use Symfony\AI\Platform\Model;
1617

1718
/**
1819
* @author Joshua Behrens <[email protected]>
1920
*/
21+
#[AsModel(platform: 'ollama', aspect: 'default')]
2022
class Ollama extends Model
2123
{
2224
public const DEEPSEEK_R_1 = 'deepseek-r1';

src/platform/src/Bridge/Ollama/OllamaClient.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\AI\Platform\Exception\InvalidArgumentException;
1515
use Symfony\AI\Platform\Model;
1616
use Symfony\AI\Platform\ModelClientInterface;
17+
use Symfony\AI\Platform\ModelClientTrait;
1718
use Symfony\AI\Platform\Result\RawHttpResult;
1819
use Symfony\Contracts\HttpClient\HttpClientInterface;
1920

@@ -22,6 +23,8 @@
2223
*/
2324
final readonly class OllamaClient implements ModelClientInterface
2425
{
26+
use ModelClientTrait;
27+
2528
public function __construct(
2629
private HttpClientInterface $httpClient,
2730
private string $hostUrl,
@@ -30,7 +33,7 @@ public function __construct(
3033

3134
public function supports(Model $model): bool
3235
{
33-
return $model instanceof Ollama;
36+
return $this->hasModelAttribute($model, 'ollama');
3437
}
3538

3639
public function request(Model $model, array|string $payload, array $options = []): RawHttpResult
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Platform;
13+
14+
use Symfony\AI\Platform\Attribute\AsModel;
15+
16+
trait ModelClientTrait
17+
{
18+
private function hasModelAttribute(Model $model, string $platform): bool
19+
{
20+
$reflection = new \ReflectionObject($model);
21+
$attributes = array_filter(
22+
$reflection->getAttributes(AsModel::class),
23+
static fn(\ReflectionAttribute $attribute) =>
24+
($attribute->getArguments()['platform'] ?? null) === $platform
25+
|| ($attribute->getArguments()[0] ?? null) === $platform
26+
);
27+
return $attributes !== [];
28+
}
29+
}

0 commit comments

Comments
 (0)