config/ai.php and environment-based API key management adhere to Laravel’s best practices for configuration and security.bind()), making it trivial to inject the AI service into controllers, commands, or jobs. Example:
use KwakuOfosuAgyeman\AiAssistant\Facades\AiAssistant;
AiAssistant::generateText("Prompt", "openai");
AiRequestSent, AiResponseReceived) for logging, analytics, or async processing.logging or a custom middleware).throttle) that can integrate with this package?.env sufficient, or will a secrets manager (e.g., AWS Secrets Manager) be required for production?bind() or facades.AiResponseReceived).Mockery to stub AI responses in tests.DependencyInjection).config/ai.php tweaks.// Before
$response = Http::withHeaders(['Authorization' => 'Bearer ' . config('services.openai.key')])
->post('https://api.openai.com/v1/completions', [...]);
// After
$response = AiAssistant::generateText("Prompt", "openai");
config/ai.php (e.g., retry with Gemini if OpenAI fails).'providers' => [
'openai' => [...],
'gemini' => [...],
],
'fallback' => ['openai', 'gemini'],
cURL or file_get_contents for HTTP requests (standard in Laravel).php artisan vendor:publish --tag=ai-config)..env with API keys.AppServiceProvider (if not using facades):
$this->app->bind(AiAssistant::class, function ($app) {
return new AiAssistant(config('ai'));
});
AiAssistant::generateText().AiAssistant::createEmbedding().AiAssistant::logRequest()).config/ai.php reduces maintenance overhead but requires discipline to update across environments (dev/staging/prod).config/ai.log_level = 'debug').error.code).dd() or dump() for response inspection.throttle middleware.openai.com/v1 vs. openai.eu/v1).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Provider API outage | No AI responses | Fallback to secondary provider or graceful degradation (e.g., cached responses). |
| Rate limit exceeded | 429 errors | Implement retries with backoff; use queue delays. |
| API key revoked | Authentication failures | Rotate keys via .env; monitor key usage. |
| High latency | Slow user experience | Use queues for non-critical paths; warn users of delays. |
| Cost overrun | Unexpected bills | Set budget alerts (e.g., via OpenAI’s usage API); implement approval flows. |
| Malicious prompts | Toxic/too-long responses | Sanitize inputs; use provider-specific safety filters (e.g., OpenAI’s temperature). |
How can I help you explore Laravel packages today?