Skip to content

Commit 736ac6d

Browse files
committed
Fix Albert API examples
1 parent 3b12db8 commit 736ac6d

File tree

5 files changed

+114
-7
lines changed

5 files changed

+114
-7
lines changed

examples/albert/_model-listing.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
use Symfony\AI\Platform\Bridge\Albert\ApiClient;
13+
use Symfony\AI\Platform\Model;
14+
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Input\InputInterface;
16+
use Symfony\Component\Console\Output\OutputInterface;
17+
use Symfony\Component\Console\SingleCommandApplication;
18+
use Symfony\Component\Console\Style\SymfonyStyle;
19+
20+
require_once dirname(__DIR__).'/bootstrap.php';
21+
22+
$app = (new SingleCommandApplication('Albert API Model Listing'))
23+
->setDescription('Lists all available models on Albert API')
24+
->setCode(function (InputInterface $input, OutputInterface $output) {
25+
$io = new SymfonyStyle($input, $output);
26+
$io->title('Albert API Model Listing');
27+
28+
$apiClient = new ApiClient(env('ALBERT_API_URL'), env('ALBERT_API_KEY'), http_client());
29+
$models = $apiClient->getModels();
30+
31+
if (0 === count($models)) {
32+
$io->error('No models found for this Albert API URL.');
33+
34+
return Command::FAILURE;
35+
}
36+
37+
$io->listing(
38+
array_map(fn (Model $model) => $model->getName(), $models)
39+
);
40+
41+
return Command::SUCCESS;
42+
})
43+
->run();

examples/albert/chat.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
'Use the provided context to answer questions accurately.'
3737
),
3838
Message::ofUser($documentContext),
39-
Message::ofUser('What are the main objectives of France\'s AI strategy?'),
39+
Message::ofUser('Summarize the main objectives of France\'s AI strategy in one sentence.'),
4040
);
4141

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

4444
echo $result->asText().\PHP_EOL;

examples/albert/embeddings.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
use Symfony\AI\Platform\Bridge\Albert\PlatformFactory;
13+
14+
require_once dirname(__DIR__).'/bootstrap.php';
15+
16+
$platform = PlatformFactory::create(env('ALBERT_API_KEY'), env('ALBERT_API_URL'), http_client());
17+
18+
$response = $platform->invoke('embeddings-small', <<<TEXT
19+
Once upon a time, there was a country called Japan. It was a beautiful country with a lot of mountains and rivers.
20+
The people of Japan were very kind and hardworking. They loved their country very much and took care of it. The
21+
country was very peaceful and prosperous. The people lived happily ever after.
22+
TEXT);
23+
24+
echo 'Dimensions: '.$response->asVectors()[0]->getDimensions().\PHP_EOL;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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\Albert;
13+
14+
use Symfony\AI\Platform\Model;
15+
use Symfony\Component\HttpClient\HttpClient;
16+
use Symfony\Contracts\HttpClient\HttpClientInterface;
17+
18+
/**
19+
* @author Christopher Hertel <mail@christopher-hertel.de>
20+
*/
21+
final class ApiClient
22+
{
23+
public function __construct(
24+
private readonly string $apiUrl,
25+
#[\SensitiveParameter] private readonly string $apiKey,
26+
private ?HttpClientInterface $httpClient = null,
27+
) {
28+
$this->httpClient = $httpClient ?? HttpClient::create();
29+
}
30+
31+
/**
32+
* @return Model[]
33+
*/
34+
public function getModels(): array
35+
{
36+
$result = $this->httpClient->request('GET', sprintf('%s/models', $this->apiUrl), [
37+
'auth_bearer' => $this->apiKey,
38+
]);
39+
40+
return array_map(fn (array $model) => new Model($model['id']), $result->toArray()['data']);
41+
}
42+
}

src/platform/src/Bridge/Albert/ModelCatalog.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

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

14+
use Symfony\AI\Platform\Bridge\OpenAi\Embeddings;
1415
use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
1516
use Symfony\AI\Platform\Capability;
1617
use Symfony\AI\Platform\Model;
@@ -44,11 +45,8 @@ public function __construct(array $additionalModels = [])
4445
],
4546
],
4647
'embeddings-small' => [
47-
'class' => Gpt::class,
48-
'capabilities' => [
49-
Capability::INPUT_MESSAGES,
50-
Capability::OUTPUT_STRUCTURED,
51-
],
48+
'class' => Embeddings::class,
49+
'capabilities' => [Capability::INPUT_TEXT],
5250
],
5351
];
5452

0 commit comments

Comments
 (0)