symfony/ai-albert-platform
Symfony AI bridge for the French government’s Albert Platform (OpenGateLLM). Connect Symfony apps to Albert’s OpenAI-compatible chat and embeddings endpoints, with links to the API reference, supported models, and upstream sources.
| | 'providers' => [
| | \Symfony\Component\Ai\Provider\AlbertProvider::class,
| | \Symfony\Component\Ai\Provider\OpenAiProvider::class, // Fallback
| | ],
| |
| | // app/Providers/AppServiceProvider.php
| | public function register()
| | {
| | $this->app->bind(\Symfony\Component\Ai\AiClientInterface::class, function ($app) {
| | return new \Symfony\Component\Ai\AiClient(
| | $app->make(\Symfony\Component\Ai\Provider\AlbertProvider::class)
| | );
| | });
| | }
| | ``` |
| **HTTP Client** | Use Laravel’s **Guzzle HTTP client** (default) or **Symfony HTTP Client** (if explicitly required by `symfony/ai`). Configure retries and timeouts in `config/ai.php`. |
| **Queues** | Offload AI calls to **Laravel queues** (e.g., `bus:dispatch`) for async processing. Useful for:
| | - Generating embeddings in bulk.
| | - Handling long-running completions (e.g., chatbot responses).
| | Example:
| | ```php
| | AiClient::create()
| | ->queue(new GenerateEmbeddingsJob($documents))
| | ->onQueue('ai');
| | ``` |
| **Caching** | Cache **embeddings** or **frequent API responses** using Laravel’s cache drivers (e.g., Redis, database). Example:
| | ```php
| | $embeddings = Cache::remember("embeddings_{$documentId}", now()->addHours(1), function () {
| | return AiClient::create()->embeddings()->create([$document]);
| | });
| | ``` |
| **Events** | Dispatch **Laravel events** (e.g., `AiResponseGenerated`) to trigger post-processing (e.g., storing responses in a database, sending notifications). |
| **Validation** | Use Laravel’s **Form Request validation** or **API resource validation** to sanitize inputs before sending to Albert. Example:
| | ```php
| | public function rules()
| | {
| | return [
| | 'prompt' => 'required|string|max:4096',
| | 'model' => 'sometimes|in:mistral-7b,llama-2',
| | ];
| | }
| | ``` |
| **Blade/Templating** | Render AI responses in **Blade templates** or **Inertia.js** for frontend integration. Example:
| | ```blade
| | @foreach($chatbotResponses as $response)
| | <div class="ai-response">{{ $response->content }}</div>
| | @endforeach
| | ``` |
| **Database** | Store AI responses, embeddings, or metadata in **Eloquent models**. Example:
| | ```php
| | class AiResponse extends Model
| | {
| | protected $casts = [
| | 'content' => 'array',
| | 'embedding' => 'array',
| | ];
| | }
| | ``` |
| **Testing** | Mock Albert’s API using **Laravel’s HTTP testing** or **Symfony’s `HttpClientMock`**. Example:
| | ```php
| | public function test_ai_response()
| | {
| | Http::fake([
| | 'albert.api.etalab.gouv.fr/*' => Http::response(['choices' => [['text' => 'Mock response']]]),
| | ]);
| |
| | $response = AiClient::create()->completions()->create('Test prompt');
| |
| | $response->assertSuccess();
| | $this->assertEquals('Mock response', $response->choices[0]->text);
| | }
| | ``` |
---
### **Migration Path**
1. **Assessment Phase (1–2 weeks)**
- Audit existing AI integrations (e.g., OpenAI SDK calls).
- Identify **use cases** (e.g., chat, embeddings, moderation) and map them to Albert’s [supported models](https://docs.opengatellm.org/features/supported_models/).
- Benchmark **latency**, **cost**, and **quality** against OpenAI for critical workflows.
2. **Proof of Concept (PoC) (1 week)**
- Implement a **minimal viable integration** for one use case (e.g., chat completions).
- Test the **Provider abstraction** with a fallback to OpenAI.
- Validate **embeddings** with a vector database (e.g., Meilisearch).
3. **Core Integration (2–3 weeks)**
- Replace OpenAI calls with `symfony/ai-albert-platform`.
- Configure **service container**, **queues**, and **caching**.
- Implement **error handling** (e.g., retries, fallbacks).
- Add **monitoring** (e.g., log AI API calls, track costs).
4. **Advanced Features (1–2 weeks, optional)**
- Dynamic **model routing** (e.g., route by cost/quality).
- **Async processing** for embeddings or long-running tasks.
- **Custom providers** for non-Albert/OpenAI sources (e.g., Hugging Face).
5. **Rollout and Optimization**
- Gradually replace OpenAI calls in **feature flags**.
- Optimize **caching** and **queue batching** for cost/performance.
- Monitor **SLA compliance** (e.g., response time, error rates).
---
### **Compatibility**
| **Compatibility Factor** | **Assessment** |
|-----------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| **PHP Version** | Requires **PHP 8.1+** (per Symfony AI). Laravel 9+ apps are compatible; older versions may need upgrades. |
| **Symfony AI Version** | Must align with `symfony/ai` version (e.g., `v0.8.0` of this package may require `symfony/ai:^0.8`). Check [Symfony AI’s changelog](https://github.com/symfony/ai/releases). |
| **Laravel Version** | No hard dependency, but **Laravel 8.83+** (Symfony 5.4+) is recommended for best compatibility with Symfony components. |
| **Database/Storage** | Embeddings can be stored in **PostgreSQL (vector extension)**, **Meilisearch**, or **Redis**. No vendor lock-in; Laravel’s database agnosticism applies. |
| **Frontend Frameworks** | Works with **Blade**, **Inertia.js**, or **API-based frontends** (e.g., React, Vue). AI responses can be serialized as JSON or rendered in templates. |
| **Third-Party Tools** | Compatible with:
| | - **Vector databases**: Meilisearch, Weaviate, PostgreSQL vectors.
| | - **Moderation tools**: Integrate with external services if Albert lacks moderation.
| | - **Monitoring**: Laravel Scout, Sentry, or custom metrics. |
| **Internationalization** | Albert supports **French/English** models (e.g., `mistral-7b`). For other languages, evaluate [OpenGateLLM’s multilingual models](https://docs.opengatellm.org/features/supported_models/). |
| **Security** | - **API Keys**: Store Albert’s API key in Laravel’s `.env` (e.g., `ALBERT_API_KEY`).
| | - **Rate Limiting**: Implement Laravel middleware to enforce request limits.
| | - **Input Sanitization**: Use Laravel validation to prevent prompt injection. |
---
### **Sequencing**
1. **Phase 1: Chat Completions**
- Replace OpenAI chat calls with Albert’s `completions` endpoint.
- Implement **fallback to OpenAI** if Albert fails.
- Test with **Laravel’s HTTP client mocking**.
2. **Phase 2: Embeddings**
- Integrate embeddings for **search or recommendations**.
- Store vectors in **Meilisearch/PostgreSQL** and query via Laravel Eloquent.
- Cache embeddings to reduce API calls.
3. **Phase 3: Advanced Routing**
- Configure **dynamic model selection** (e.g., `mistral-7b` for cost, `llama-2` for quality).
- Add **usage-based routing** (e.g., route high-priority requests to OpenAI).
4. **Phase 4: Observability**
- Log AI API calls with **Laravel’s logging**.
- Track **costs**, **latency**, and **error rates** via **Sentry** or **Prometheus**.
- Set up **alerts**
How can I help you explore Laravel packages today?