Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit 257541e

Browse files
committed
feat: add Albert API support
- Albert API is an OpenAI-compatible sovereign AI gateway by French government - Added examples for chat, RAG, streaming, and tool calling - Updated README to include Albert in supported platforms - Added Albert configuration to .env file Closes #346
1 parent 929a7c1 commit 257541e

File tree

7 files changed

+320
-0
lines changed

7 files changed

+320
-0
lines changed

.env

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,10 @@ RUN_EXPENSIVE_EXAMPLES=false
6565
# For using Gemini
6666
GOOGLE_API_KEY=
6767

68+
# For using Albert API (French Sovereign AI)
69+
ALBERT_API_KEY=
70+
ALBERT_API_URL=
71+
ALBERT_MODEL=
72+
6873
# For MariaDB store. Server defined in compose.yaml
6974
MARIADB_URI=pdo-mysql://[email protected]:3309/my_database

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ $embeddings = new Embeddings();
6969
* [DeepSeek's R1](https://www.deepseek.com/) with [OpenRouter](https://www.openrouter.com/) as Platform
7070
* [Amazon's Nova](https://nova.amazon.com) with [AWS](https://aws.amazon.com/bedrock/) as Platform
7171
* [Mistral's Mistral](https://www.mistral.ai/) with [Mistral](https://www.mistral.ai/) as Platform
72+
* OpenAI-compatible models via [Albert API](https://github.com/etalab-ia/albert-api) (French government's sovereign AI gateway)
7273
* Embeddings Models
7374
* [OpenAI's Text Embeddings](https://platform.openai.com/docs/guides/embeddings/embedding-models) with [OpenAI](https://platform.openai.com/docs/overview) and [Azure](https://learn.microsoft.com/azure/ai-services/openai/concepts/models) as Platform
7475
* [Voyage's Embeddings](https://docs.voyageai.com/docs/embeddings) with [Voyage](https://www.voyageai.com/) as Platform
@@ -130,6 +131,7 @@ $response = $chain->call($messages, [
130131

131132
#### Code Examples
132133

134+
1. [Albert API (French Sovereign AI)](examples/albert/chat.php)
133135
1. [Anthropic's Claude](examples/anthropic/chat.php)
134136
1. [OpenAI's GPT with Azure](examples/azure/chat-gpt.php)
135137
1. [OpenAI's GPT](examples/openai/chat.php)

examples/albert/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Albert API Examples
2+
3+
[Albert API](https://github.com/etalab-ia/albert-api) is an open-source generative AI API gateway developed by the French government. It provides a sovereign AI solution with OpenAI-compatible APIs, making it easy to integrate with LLM Chain.
4+
5+
## Prerequisites
6+
7+
1. Deploy Albert API following the [official deployment guide](https://github.com/etalab-ia/albert-api)
8+
2. Obtain an API key from your Albert instance
9+
3. Set the required environment variables:
10+
11+
```bash
12+
export ALBERT_API_KEY="your-api-key"
13+
export ALBERT_API_URL="https://your-albert-instance.com"
14+
export ALBERT_MODEL="albert-7b-v2" # or your configured model
15+
```
16+
17+
## Examples
18+
19+
### Basic Chat
20+
```bash
21+
php examples/albert/chat.php
22+
```
23+
Demonstrates basic conversation with Albert API.
24+
25+
### RAG (Retrieval-Augmented Generation)
26+
```bash
27+
php examples/albert/rag.php
28+
```
29+
Shows how to use Albert's built-in RAG capabilities with document context.
30+
31+
### Streaming
32+
```bash
33+
php examples/albert/stream.php
34+
```
35+
Demonstrates real-time streaming responses from Albert API.
36+
37+
### Tool Calling
38+
```bash
39+
php examples/albert/toolcall.php
40+
```
41+
Shows how to use function/tool calling with Albert API.
42+
43+
## Configuration
44+
45+
Albert API is OpenAI-compatible, so it works seamlessly with LLM Chain's OpenAI bridge:
46+
47+
```php
48+
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\PlatformFactory;
49+
50+
$platform = PlatformFactory::create(
51+
apiKey: $albertApiKey,
52+
baseUrl: rtrim($albertApiUrl, '/').'/v1/',
53+
);
54+
```
55+
56+
## Features
57+
58+
- **Sovereign AI**: Host your models on your own infrastructure
59+
- **OpenAI Compatible**: Works with existing OpenAI integrations
60+
- **Built-in RAG**: Native support for retrieval-augmented generation
61+
- **Multiple Backends**: Supports OpenAI, vLLM, and HuggingFace models
62+
- **Enterprise Ready**: Authentication, load balancing, and monitoring
63+
64+
## Notes
65+
66+
- The model name depends on your Albert deployment configuration
67+
- Albert supports various model backends (OpenAI, vLLM, HuggingFace TEI)
68+
- Check your Albert instance documentation for available models and features

examples/albert/chat.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpLlm\LlmChain\Chain\Chain;
6+
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\GPT;
7+
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\PlatformFactory;
8+
use PhpLlm\LlmChain\Platform\Message\Message;
9+
use PhpLlm\LlmChain\Platform\Message\MessageBag;
10+
11+
require_once dirname(__DIR__).'/../vendor/autoload.php';
12+
13+
// Albert API configuration
14+
$albertApiKey = $_ENV['ALBERT_API_KEY'] ?? null;
15+
$albertApiUrl = $_ENV['ALBERT_API_URL'] ?? null;
16+
17+
if (empty($albertApiKey)) {
18+
echo 'Please set the ALBERT_API_KEY environment variable.'.PHP_EOL;
19+
exit(1);
20+
}
21+
22+
if (empty($albertApiUrl)) {
23+
echo 'Please set the ALBERT_API_URL environment variable (e.g., https://your-albert-instance.com).'.PHP_EOL;
24+
exit(1);
25+
}
26+
27+
// Albert API is OpenAI-compatible, so we use the OpenAI platform factory
28+
// with a custom base URL pointing to your Albert instance
29+
$platform = PlatformFactory::create(
30+
apiKey: $albertApiKey,
31+
baseUrl: rtrim($albertApiUrl, '/').'/v1/', // Ensure proper URL format
32+
);
33+
34+
// Use the model name provided by your Albert instance
35+
// This could be a custom model or one of the supported backends
36+
$model = new GPT($_ENV['ALBERT_MODEL'] ?? 'albert-7b-v2');
37+
38+
$chain = new Chain($platform, $model);
39+
40+
$messages = new MessageBag(
41+
Message::forSystem('You are a helpful AI assistant powered by Albert API.'),
42+
Message::ofUser('Hello! Can you tell me about the French government\'s AI initiatives?'),
43+
);
44+
45+
$response = $chain->call($messages);
46+
47+
echo 'Albert API Response:'.PHP_EOL;
48+
echo '==================='.PHP_EOL;
49+
echo $response->getContent().PHP_EOL;

examples/albert/rag.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpLlm\LlmChain\Chain\Chain;
6+
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\GPT;
7+
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\PlatformFactory;
8+
use PhpLlm\LlmChain\Platform\Message\Message;
9+
use PhpLlm\LlmChain\Platform\Message\MessageBag;
10+
11+
require_once dirname(__DIR__).'/../vendor/autoload.php';
12+
13+
// Albert API configuration
14+
$albertApiKey = $_ENV['ALBERT_API_KEY'] ?? null;
15+
$albertApiUrl = $_ENV['ALBERT_API_URL'] ?? null;
16+
17+
if (empty($albertApiKey)) {
18+
echo 'Please set the ALBERT_API_KEY environment variable.'.PHP_EOL;
19+
exit(1);
20+
}
21+
22+
if (empty($albertApiUrl)) {
23+
echo 'Please set the ALBERT_API_URL environment variable (e.g., https://your-albert-instance.com).'.PHP_EOL;
24+
exit(1);
25+
}
26+
27+
$platform = PlatformFactory::create(
28+
apiKey: $albertApiKey,
29+
baseUrl: rtrim($albertApiUrl, '/').'/v1/',
30+
);
31+
32+
$model = new GPT($_ENV['ALBERT_MODEL'] ?? 'albert-7b-v2');
33+
$chain = new Chain($platform, $model);
34+
35+
// Albert API supports RAG out of the box
36+
// You can pass document context as part of your messages
37+
$documentContext = <<<'CONTEXT'
38+
Document: AI Strategy of France
39+
40+
France has launched a comprehensive national AI strategy with the following key objectives:
41+
1. Strengthening the AI ecosystem and attracting talent
42+
2. Developing sovereign AI capabilities
43+
3. Ensuring ethical and responsible AI development
44+
4. Supporting AI adoption in public services
45+
5. Investing €1.5 billion in AI research and development
46+
47+
The Albert project is part of this strategy, providing a sovereign AI solution for French public administration.
48+
CONTEXT;
49+
50+
$messages = new MessageBag(
51+
Message::forSystem(
52+
'You are an AI assistant with access to documents about French AI initiatives. '.
53+
'Use the provided context to answer questions accurately.'
54+
),
55+
Message::ofUser($documentContext),
56+
Message::ofUser('What are the main objectives of France\'s AI strategy?'),
57+
);
58+
59+
$response = $chain->call($messages);
60+
61+
echo 'Albert API RAG Response:'.PHP_EOL;
62+
echo '========================'.PHP_EOL;
63+
echo $response->getContent().PHP_EOL;

examples/albert/stream.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpLlm\LlmChain\Chain\Chain;
6+
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\GPT;
7+
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\PlatformFactory;
8+
use PhpLlm\LlmChain\Platform\Message\Message;
9+
use PhpLlm\LlmChain\Platform\Message\MessageBag;
10+
11+
require_once dirname(__DIR__).'/../vendor/autoload.php';
12+
13+
// Albert API configuration
14+
$albertApiKey = $_ENV['ALBERT_API_KEY'] ?? null;
15+
$albertApiUrl = $_ENV['ALBERT_API_URL'] ?? null;
16+
17+
if (empty($albertApiKey)) {
18+
echo 'Please set the ALBERT_API_KEY environment variable.'.PHP_EOL;
19+
exit(1);
20+
}
21+
22+
if (empty($albertApiUrl)) {
23+
echo 'Please set the ALBERT_API_URL environment variable (e.g., https://your-albert-instance.com).'.PHP_EOL;
24+
exit(1);
25+
}
26+
27+
$platform = PlatformFactory::create(
28+
apiKey: $albertApiKey,
29+
baseUrl: rtrim($albertApiUrl, '/').'/v1/',
30+
);
31+
32+
$model = new GPT($_ENV['ALBERT_MODEL'] ?? 'albert-7b-v2');
33+
$chain = new Chain($platform, $model);
34+
35+
$messages = new MessageBag(
36+
Message::forSystem('You are a helpful AI assistant powered by Albert API.'),
37+
Message::ofUser('Write a short story about a robot discovering emotions.'),
38+
);
39+
40+
// Enable streaming for real-time response
41+
$response = $chain->call($messages, ['stream' => true]);
42+
43+
echo 'Albert API Streaming Response:'.PHP_EOL;
44+
echo '=============================='.PHP_EOL;
45+
46+
// Stream the response token by token
47+
foreach ($response->getContent() as $token) {
48+
echo $token;
49+
flush(); // Ensure immediate output
50+
}
51+
52+
echo PHP_EOL;

examples/albert/toolcall.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PhpLlm\LlmChain\Chain\Chain;
6+
use PhpLlm\LlmChain\Chain\Toolbox\Attribute\AsTool;
7+
use PhpLlm\LlmChain\Chain\Toolbox\ChainProcessor;
8+
use PhpLlm\LlmChain\Chain\Toolbox\Toolbox;
9+
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\GPT;
10+
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\PlatformFactory;
11+
use PhpLlm\LlmChain\Platform\Message\Message;
12+
use PhpLlm\LlmChain\Platform\Message\MessageBag;
13+
14+
require_once dirname(__DIR__).'/../vendor/autoload.php';
15+
16+
// Albert API configuration
17+
$albertApiKey = $_ENV['ALBERT_API_KEY'] ?? null;
18+
$albertApiUrl = $_ENV['ALBERT_API_URL'] ?? null;
19+
20+
if (empty($albertApiKey)) {
21+
echo 'Please set the ALBERT_API_KEY environment variable.'.PHP_EOL;
22+
exit(1);
23+
}
24+
25+
if (empty($albertApiUrl)) {
26+
echo 'Please set the ALBERT_API_URL environment variable (e.g., https://your-albert-instance.com).'.PHP_EOL;
27+
exit(1);
28+
}
29+
30+
// Custom tool for French administrative information
31+
#[AsTool('french_departments', 'Get information about French departments')]
32+
final class FrenchDepartments
33+
{
34+
private array $departments = [
35+
'75' => ['name' => 'Paris', 'region' => 'Île-de-France', 'prefecture' => 'Paris'],
36+
'13' => ['name' => 'Bouches-du-Rhône', 'region' => 'Provence-Alpes-Côte d\'Azur', 'prefecture' => 'Marseille'],
37+
'69' => ['name' => 'Rhône', 'region' => 'Auvergne-Rhône-Alpes', 'prefecture' => 'Lyon'],
38+
'31' => ['name' => 'Haute-Garonne', 'region' => 'Occitanie', 'prefecture' => 'Toulouse'],
39+
'44' => ['name' => 'Loire-Atlantique', 'region' => 'Pays de la Loire', 'prefecture' => 'Nantes'],
40+
];
41+
42+
/**
43+
* Get information about a French department by its number.
44+
*
45+
* @param string $departmentNumber The department number (e.g., "75" for Paris)
46+
*/
47+
public function __invoke(string $departmentNumber): array
48+
{
49+
if (!isset($this->departments[$departmentNumber])) {
50+
return ['error' => "Department $departmentNumber not found in the database"];
51+
}
52+
53+
return $this->departments[$departmentNumber];
54+
}
55+
}
56+
57+
// Initialize Albert API
58+
$platform = PlatformFactory::create(
59+
apiKey: $albertApiKey,
60+
baseUrl: rtrim($albertApiUrl, '/').'/v1/',
61+
);
62+
63+
$model = new GPT($_ENV['ALBERT_MODEL'] ?? 'albert-7b-v2');
64+
65+
// Set up toolbox with our custom tool
66+
$tool = new FrenchDepartments();
67+
$toolbox = Toolbox::create($tool);
68+
$processor = new ChainProcessor($toolbox);
69+
70+
$chain = new Chain($platform, $model, [$processor], [$processor]);
71+
72+
$messages = new MessageBag(
73+
Message::forSystem('You are a helpful assistant for French administrative information.'),
74+
Message::ofUser('What is the prefecture of department 69?'),
75+
);
76+
77+
$response = $chain->call($messages);
78+
79+
echo 'Albert API Tool Calling Response:'.PHP_EOL;
80+
echo '================================='.PHP_EOL;
81+
echo $response->getContent().PHP_EOL;

0 commit comments

Comments
 (0)