Skip to content

Commit c06ad35

Browse files
committed
feature #910 [Platform] OllamaApiCatalog improved (Guikingone)
This PR was squashed before being merged into the main branch. Discussion ---------- [Platform] `OllamaApiCatalog` improved | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Docs? | yes | Issues | None | License | MIT Commits ------- 572d30b [Platform] `OllamaApiCatalog` improved
2 parents 05e6981 + 572d30b commit c06ad35

File tree

2 files changed

+56
-22
lines changed

2 files changed

+56
-22
lines changed

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

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,25 @@
1313

1414
use Symfony\AI\Platform\Capability;
1515
use Symfony\AI\Platform\Exception\InvalidArgumentException;
16-
use Symfony\AI\Platform\ModelCatalog\FallbackModelCatalog;
16+
use Symfony\AI\Platform\ModelCatalog\ModelCatalogInterface;
1717
use Symfony\Contracts\HttpClient\HttpClientInterface;
1818

1919
/**
2020
* @author Guillaume Loulier <[email protected]>
2121
*/
22-
final class OllamaApiCatalog extends FallbackModelCatalog
22+
final class OllamaApiCatalog implements ModelCatalogInterface
2323
{
2424
public function __construct(
2525
private readonly string $host,
2626
private readonly HttpClientInterface $httpClient,
2727
) {
28-
parent::__construct();
2928
}
3029

3130
public function getModel(string $modelName): Ollama
3231
{
33-
$model = parent::getModel($modelName);
34-
35-
if (\array_key_exists($model->getName(), $this->models)) {
36-
$finalModel = $this->models[$model->getName()];
37-
38-
return new $finalModel['class'](
39-
$model->getName(),
40-
$finalModel['capabilities'],
41-
$model->getOptions(),
42-
);
43-
}
44-
4532
$response = $this->httpClient->request('POST', \sprintf('%s/api/show', $this->host), [
4633
'json' => [
47-
'model' => $model->getName(),
34+
'model' => $modelName,
4835
],
4936
]);
5037

@@ -66,13 +53,27 @@ public function getModel(string $modelName): Ollama
6653
$payload['capabilities'],
6754
);
6855

69-
$finalModel = new Ollama($model->getName(), $capabilities, $model->getOptions());
56+
return new Ollama($modelName, $capabilities);
57+
}
7058

71-
$this->models[$finalModel->getName()] = [
72-
'class' => Ollama::class,
73-
'capabilities' => $finalModel->getCapabilities(),
74-
];
59+
public function getModels(): array
60+
{
61+
$response = $this->httpClient->request('POST', \sprintf('%s/api/tags', $this->host));
62+
63+
$models = $response->toArray();
7564

76-
return $finalModel;
65+
return array_merge(...array_map(
66+
function (array $model): array {
67+
$retrievedModel = $this->getModel($model['name']);
68+
69+
return [
70+
$retrievedModel->getName() => [
71+
'class' => Ollama::class,
72+
'capabilities' => $retrievedModel->getCapabilities(),
73+
],
74+
];
75+
},
76+
$models['models'],
77+
));
7778
}
7879
}

src/platform/tests/Bridge/Ollama/OllamaApiCatalogTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\AI\Platform\Tests\Bridge\Ollama;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\AI\Platform\Bridge\Ollama\Ollama;
1516
use Symfony\AI\Platform\Bridge\Ollama\OllamaApiCatalog;
1617
use Symfony\AI\Platform\Capability;
1718
use Symfony\Component\HttpClient\MockHttpClient;
@@ -37,4 +38,36 @@ public function testModelCatalogCanReturnModelFromApi()
3738
], $model->getCapabilities());
3839
$this->assertSame(1, $httpClient->getRequestsCount());
3940
}
41+
42+
public function testModelCatalogCanReturnModelsFromApi()
43+
{
44+
$httpClient = new MockHttpClient([
45+
new JsonMockResponse([
46+
'models' => [
47+
[
48+
'name' => 'gemma3',
49+
'details' => [],
50+
],
51+
],
52+
]),
53+
new JsonMockResponse([
54+
'capabilities' => ['completion'],
55+
]),
56+
]);
57+
58+
$modelCatalog = new OllamaApiCatalog('http://127.0.0.1:11434', $httpClient);
59+
60+
$models = $modelCatalog->getModels();
61+
62+
$this->assertCount(1, $models);
63+
$this->assertArrayHasKey('gemma3', $models);
64+
65+
$model = $models['gemma3'];
66+
$this->assertSame(Ollama::class, $model['class']);
67+
$this->assertCount(1, $model['capabilities']);
68+
$this->assertSame([
69+
Capability::INPUT_TEXT,
70+
], $model['capabilities']);
71+
$this->assertSame(2, $httpClient->getRequestsCount());
72+
}
4073
}

0 commit comments

Comments
 (0)