Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions src/platform/src/Bridge/Ollama/OllamaApiCatalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,25 @@

use Symfony\AI\Platform\Capability;
use Symfony\AI\Platform\Exception\InvalidArgumentException;
use Symfony\AI\Platform\ModelCatalog\FallbackModelCatalog;
use Symfony\AI\Platform\ModelCatalog\ModelCatalogInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Guillaume Loulier <personal@guillaumeloulier.fr>
*/
final class OllamaApiCatalog extends FallbackModelCatalog
final class OllamaApiCatalog implements ModelCatalogInterface
{
public function __construct(
private readonly string $host,
private readonly HttpClientInterface $httpClient,
) {
parent::__construct();
}

public function getModel(string $modelName): Ollama
{
$model = parent::getModel($modelName);

if (\array_key_exists($model->getName(), $this->models)) {
$finalModel = $this->models[$model->getName()];

return new $finalModel['class'](
$model->getName(),
$finalModel['capabilities'],
$model->getOptions(),
);
}

$response = $this->httpClient->request('POST', \sprintf('%s/api/show', $this->host), [
'json' => [
'model' => $model->getName(),
'model' => $modelName,
],
]);

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

$finalModel = new Ollama($model->getName(), $capabilities, $model->getOptions());
return new Ollama($modelName, $capabilities);
}

$this->models[$finalModel->getName()] = [
'class' => Ollama::class,
'capabilities' => $finalModel->getCapabilities(),
];
public function getModels(): array
{
$response = $this->httpClient->request('POST', \sprintf('%s/api/tags', $this->host));

$models = $response->toArray();

return $finalModel;
return array_merge(...array_map(
function (array $model): array {
$retrievedModel = $this->getModel($model['name']);

return [
$retrievedModel->getName() => [
'class' => Ollama::class,
'capabilities' => $retrievedModel->getCapabilities(),
],
];
},
$models['models'],
));
}
}
33 changes: 33 additions & 0 deletions src/platform/tests/Bridge/Ollama/OllamaApiCatalogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\AI\Platform\Tests\Bridge\Ollama;

use PHPUnit\Framework\TestCase;
use Symfony\AI\Platform\Bridge\Ollama\Ollama;
use Symfony\AI\Platform\Bridge\Ollama\OllamaApiCatalog;
use Symfony\AI\Platform\Capability;
use Symfony\Component\HttpClient\MockHttpClient;
Expand All @@ -37,4 +38,36 @@ public function testModelCatalogCanReturnModelFromApi()
], $model->getCapabilities());
$this->assertSame(1, $httpClient->getRequestsCount());
}

public function testModelCatalogCanReturnModelsFromApi()
{
$httpClient = new MockHttpClient([
new JsonMockResponse([
'models' => [
[
'name' => 'gemma3',
'details' => [],
],
],
]),
new JsonMockResponse([
'capabilities' => ['completion'],
]),
]);

$modelCatalog = new OllamaApiCatalog('http://127.0.0.1:11434', $httpClient);

$models = $modelCatalog->getModels();

$this->assertCount(1, $models);
$this->assertArrayHasKey('gemma3', $models);

$model = $models['gemma3'];
$this->assertSame(Ollama::class, $model['class']);
$this->assertCount(1, $model['capabilities']);
$this->assertSame([
Capability::INPUT_TEXT,
], $model['capabilities']);
$this->assertSame(2, $httpClient->getRequestsCount());
}
}