symfony/ai-cerebras-platform
Symfony AI bridge for the Cerebras inference platform. Adds a Cerebras connector to run chat completions and other inference requests through Symfony AI, with links to Cerebras API docs and contribution/issue tracking in the main Symfony AI repository.
Provider abstraction requires a custom Laravel Service Provider to bind the CerebrasClient and expose it via a facade or container alias. This ensures compatibility with Laravel’s dependency injection while abstracting Symfony’s DI container.
// app/Providers/CerebrasServiceProvider.php
public function register()
{
$this->app->singleton(CerebrasClient::class, function ($app) {
return new CerebrasClient(
$app['config']['services.cerebras.api_key'],
new HttpClient(),
new ModelRouter() // Custom routing logic
);
});
}
public function chatCompletions(array $payload): array|StreamedResponse;
public function inference(array $payload): array|StreamedResponse;
/ai/cerebras/chat) with middleware for:
throttle:60,1).Route::post('/ai/cerebras/chat', [CerebrasController::class, 'chat'])
->middleware(['auth:api', 'throttle:ai']);
DeltaInterface streams to Laravel’s SymfonyStreamedResponse for real-time endpoints (e.g., chat UIs).use Symfony\Component\HttpFoundation\StreamedResponse;
public function streamChat(ChatRequest $request): StreamedResponse
{
$client = app(CerebrasClient::class);
$stream = $client->chatCompletions($request->validated());
return new StreamedResponse(
fn() => $this->processStream($stream),
200,
['Content-Type' => 'text/event-stream']
);
}
class CerebrasInferenceJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue;
public function handle()
{
$client = app(CerebrasClient::class);
$result = $client->inference($this->payload);
// Store/process result
}
}
Cache::tags(['ai:cerebras', 'model:gpt-4'])->remember(
'inference:user_123',
now()->addHours(1),
fn() => $client->inference($payload)
);
// CerebrasController.php
public function streamForLivewire(ChatRequest $request)
{
$stream = app(CerebrasClient::class)->chatCompletions($request->validated());
return response()->stream(fn() => $this->emitStream($stream));
}
// Livewire component
window.Echo.channel('ai.cerebras.stream')
.listen('CerebrasStreamEvent', (data) => {
this.appendMessage(data.delta);
});
Handler to convert Cerebras’ ApiError into ProblemDetails or custom exceptions.public function register()
{
$this->app->bind(ApiError::class, function () {
return new LaravelApiError(); // Custom wrapper
});
}
Phase 1: Proof of Concept (2–4 weeks)
Phase 2: Core Integration (4–6 weeks)
laravel-cerebras) with CI/CD pipeline.Phase 3: Production Rollout (3–5 weeks)
| Laravel Feature | Compatibility | Workarounds |
|---|---|---|
| Service Container | Medium | Custom Service Provider + facade. |
| Blade Templates | Low | Use API responses as JSON data sources; avoid direct streaming. |
| Eloquent Models | Medium | Map structured outputs to Eloquent attributes via accessors/mutators. |
| Livewire | High | Streamed responses via SymfonyStreamedResponse or Echo. |
| API Resources | High | Format Cerebras responses to match Laravel API Resource contracts. |
| Queues | High | Offload inference tasks to CerebrasInferenceJob. |
| Caching | High | Use Laravel Cache with tags for invalidation. |
| Middleware | High | Add auth/rate-limiting middleware to routes. |
| Testing | Medium | Mock CerebrasClient in PHPUnit; use Pest for streaming tests. |
Prerequisites:
symfony/ai) and its dependencies.Core Integration:
Advanced Features:
Production Readiness:
Optimization:
Provider interface).laravel-envoy or GitHub Actions for dependency updates.class CerebrasChatRequest extends FormRequest
{
public function rules(): array
{
return [
'model' => 'required|string|in:'.implode(',', CerebrasClient::SUPPORTED_MODELS),
'messages' => 'required|array',
];
}
}
Log::channel('cerebras')->info
How can I help you explore Laravel packages today?