Skip to content

Commit a00950d

Browse files
committed
ref(platform): OllamaCatalog
1 parent 33811e8 commit a00950d

File tree

4 files changed

+47
-18
lines changed

4 files changed

+47
-18
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Bridge\Ollama;
13+
14+
use Symfony\AI\Platform\Model;
15+
use Symfony\AI\Platform\ModelCatalog\DynamicModelCatalog;
16+
use Symfony\Contracts\HttpClient\HttpClientInterface;
17+
18+
final class OllamaCatalog extends DynamicModelCatalog
19+
{
20+
public function __construct(
21+
private readonly string $host,
22+
private readonly HttpClientInterface $httpClient,
23+
) {
24+
parent::__construct();
25+
}
26+
27+
public function getModel(string $modelName): Model
28+
{
29+
$response = $this->httpClient->request('POST', \sprintf('%s/api/show', $this->host), [
30+
'json' => [
31+
'model' => $modelName,
32+
],
33+
]);
34+
35+
$payload = $response->toArray();
36+
37+
return new Ollama($modelName, $payload['capabilities']);
38+
}
39+
}

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,13 @@ public function supports(Model $model): bool
3535

3636
public function request(Model $model, array|string $payload, array $options = []): RawHttpResult
3737
{
38-
$response = $this->httpClient->request('POST', \sprintf('%s/api/show', $this->hostUrl), [
39-
'json' => [
40-
'model' => $model->getName(),
41-
],
42-
]);
43-
44-
$capabilities = $response->toArray()['capabilities'] ?? null;
45-
46-
if (null === $capabilities) {
38+
if ([] === $model->getCapabilities()) {
4739
throw new InvalidArgumentException('The model information could not be retrieved from the Ollama API. Your Ollama server might be too old. Try upgrade it.');
4840
}
4941

5042
return match (true) {
51-
\in_array('completion', $capabilities, true) => $this->doCompletionRequest($payload, $options),
52-
\in_array('embedding', $capabilities, true) => $this->doEmbeddingsRequest($model, $payload, $options),
43+
\in_array('completion', $model->getCapabilities(), true) => $this->doCompletionRequest($payload, $options),
44+
\in_array('embedding', $model->getCapabilities(), true) => $this->doEmbeddingsRequest($model, $payload, $options),
5345
default => throw new InvalidArgumentException(\sprintf('Unsupported model "%s": "%s".', $model::class, $model->getName())),
5446
};
5547
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\AI\Platform\Bridge\Ollama\Contract\OllamaContract;
1515
use Symfony\AI\Platform\Contract;
16-
use Symfony\AI\Platform\ModelCatalog\DynamicModelCatalog;
1716
use Symfony\AI\Platform\ModelCatalog\ModelCatalogInterface;
1817
use Symfony\AI\Platform\Platform;
1918
use Symfony\Component\HttpClient\EventSourceHttpClient;
@@ -27,15 +26,15 @@ final class PlatformFactory
2726
public static function create(
2827
string $hostUrl = 'http://localhost:11434',
2928
?HttpClientInterface $httpClient = null,
30-
ModelCatalogInterface $modelCatalog = new DynamicModelCatalog(Ollama::class),
29+
?ModelCatalogInterface $modelCatalog = null,
3130
?Contract $contract = null,
3231
): Platform {
3332
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
3433

3534
return new Platform(
3635
[new OllamaClient($httpClient, $hostUrl)],
3736
[new OllamaResultConverter()],
38-
$modelCatalog,
37+
$modelCatalog ?? new OllamaCatalog($hostUrl, $httpClient),
3938
$contract ?? OllamaContract::create(),
4039
);
4140
}

src/platform/src/ModelCatalog/DynamicModelCatalog.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,15 @@
2525

2626
class DynamicModelCatalog extends AbstractModelCatalog
2727
{
28-
public function __construct(
29-
private readonly ?string $expectedModel = Model::class,
30-
) {
28+
public function __construct()
29+
{
3130
$this->models = [];
3231
}
3332

3433
public function getModel(string $modelName): Model
3534
{
3635
$parsed = self::parseModelName($modelName);
3736

38-
return new $this->expectedModel($parsed['name'], Capability::cases(), $parsed['options']);
37+
return new Model($parsed['name'], Capability::cases(), $parsed['options']);
3938
}
4039
}

0 commit comments

Comments
 (0)