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
43 changes: 43 additions & 0 deletions examples/albert/_model-listing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\AI\Platform\Bridge\Albert\ApiClient;
use Symfony\AI\Platform\Model;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;
use Symfony\Component\Console\Style\SymfonyStyle;

require_once dirname(__DIR__).'/bootstrap.php';

$app = (new SingleCommandApplication('Albert API Model Listing'))
->setDescription('Lists all available models on Albert API')
->setCode(function (InputInterface $input, OutputInterface $output) {
$io = new SymfonyStyle($input, $output);
$io->title('Albert API Model Listing');

$apiClient = new ApiClient(env('ALBERT_API_URL'), env('ALBERT_API_KEY'), http_client());
$models = $apiClient->getModels();

if (0 === count($models)) {
$io->error('No models found for this Albert API URL.');

return Command::FAILURE;
}

$io->listing(
array_map(fn (Model $model) => $model->getName(), $models)
);

return Command::SUCCESS;
})
->run();
4 changes: 2 additions & 2 deletions examples/albert/chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
'Use the provided context to answer questions accurately.'
),
Message::ofUser($documentContext),
Message::ofUser('What are the main objectives of France\'s AI strategy?'),
Message::ofUser('Summarize the main objectives of France\'s AI strategy in one sentence.'),
);

$result = $platform->invoke('llama-3.3-70b-instruct', $messages);
$result = $platform->invoke('albert-small', $messages);

echo $result->asText().\PHP_EOL;
24 changes: 24 additions & 0 deletions examples/albert/embeddings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\AI\Platform\Bridge\Albert\PlatformFactory;

require_once dirname(__DIR__).'/bootstrap.php';

$platform = PlatformFactory::create(env('ALBERT_API_KEY'), env('ALBERT_API_URL'), http_client());

$response = $platform->invoke('embeddings-small', <<<TEXT
Once upon a time, there was a country called Japan. It was a beautiful country with a lot of mountains and rivers.
The people of Japan were very kind and hardworking. They loved their country very much and took care of it. The
country was very peaceful and prosperous. The people lived happily ever after.
TEXT);

echo 'Dimensions: '.$response->asVectors()[0]->getDimensions().\PHP_EOL;
42 changes: 42 additions & 0 deletions src/platform/src/Bridge/Albert/ApiClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Platform\Bridge\Albert;

use Symfony\AI\Platform\Model;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Christopher Hertel <mail@christopher-hertel.de>
*/
final class ApiClient
{
public function __construct(
private readonly string $apiUrl,
#[\SensitiveParameter] private readonly string $apiKey,
private ?HttpClientInterface $httpClient = null,
) {
$this->httpClient = $httpClient ?? HttpClient::create();
}

/**
* @return Model[]
*/
public function getModels(): array
{
$result = $this->httpClient->request('GET', \sprintf('%s/models', $this->apiUrl), [
'auth_bearer' => $this->apiKey,
]);

return array_map(fn (array $model) => new Model($model['id']), $result->toArray()['data']);
}
}
8 changes: 3 additions & 5 deletions src/platform/src/Bridge/Albert/ModelCatalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\AI\Platform\Bridge\Albert;

use Symfony\AI\Platform\Bridge\OpenAi\Embeddings;
use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
use Symfony\AI\Platform\Capability;
use Symfony\AI\Platform\Model;
Expand Down Expand Up @@ -44,11 +45,8 @@ public function __construct(array $additionalModels = [])
],
],
'embeddings-small' => [
'class' => Gpt::class,
'capabilities' => [
Capability::INPUT_MESSAGES,
Capability::OUTPUT_STRUCTURED,
],
'class' => Embeddings::class,
'capabilities' => [Capability::INPUT_TEXT],
],
];

Expand Down
3 changes: 2 additions & 1 deletion src/platform/tests/Bridge/Albert/ModelCatalogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\AI\Platform\Tests\Bridge\Albert;

use Symfony\AI\Platform\Bridge\Albert\ModelCatalog;
use Symfony\AI\Platform\Bridge\OpenAi\Embeddings;
use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
use Symfony\AI\Platform\Capability;
use Symfony\AI\Platform\ModelCatalog\ModelCatalogInterface;
Expand All @@ -26,7 +27,7 @@ public static function modelsProvider(): iterable
{
yield 'albert-small' => ['albert-small', Gpt::class, [Capability::INPUT_MESSAGES, Capability::OUTPUT_TEXT, Capability::OUTPUT_STREAMING]];
yield 'albert-large' => ['albert-large', Gpt::class, [Capability::INPUT_MESSAGES, Capability::OUTPUT_TEXT, Capability::OUTPUT_STREAMING]];
yield 'embeddings-small' => ['embeddings-small', Gpt::class, [Capability::INPUT_MESSAGES, Capability::OUTPUT_STRUCTURED]];
yield 'embeddings-small' => ['embeddings-small', Embeddings::class, [Capability::INPUT_TEXT]];
}

protected function createModelCatalog(): ModelCatalogInterface
Expand Down