Introduction
Integrating OpenAI's ChatGPT API into Laravel applications opens up powerful possibilities for AI-driven features. In this guide, I'll walk through a production-ready implementation.
Setup
First, install the OpenAI PHP client:
composer require openai-php/laravelAdd your API key to .env:
OPENAI_API_KEY=your-api-key-hereBasic Implementation
Create a service class to handle ChatGPT interactions:
namespace App\Services;
use OpenAI\Laravel\Facades\OpenAI;
class ChatService
{
public function generateResponse(string $prompt): string
{
$response = OpenAI::chat()->create([
'model' => 'gpt-4',
'messages' => [
['role' => 'user', 'content' => $prompt]
],
'max_tokens' => 150,
]);
return $response->choices[0]->message->content;
}
}Error Handling
Always implement proper error handling:
try {
$response = OpenAI::chat()->create([...]);
} catch (\Exception $e) {
Log::error('OpenAI API Error: ' . $e->getMessage());
// Fallback response
return 'I apologize, but I\'m having trouble processing your request.';
}Rate Limiting
Implement rate limiting to manage API costs:
use Illuminate\Support\Facades\RateLimiter;
if (RateLimiter::tooManyAttempts('openai-request', 10)) {
throw new \Exception('Too many requests');
}
RateLimiter::hit('openai-request');Caching Responses
Cache common queries to reduce API calls:
$cacheKey = 'chatgpt-' . md5($prompt);
return Cache::remember($cacheKey, 3600, function() use ($prompt) {
return $this->generateResponse($prompt);
});Best Practices
- Set appropriate max_tokens - Don't request more than you need
- Use system messages - Guide the AI's behavior with system prompts
- Implement retry logic - Handle transient failures gracefully
- Monitor usage - Track API calls and costs
- Sanitize inputs - Clean user inputs before sending to API
Production Considerations
- Use queued jobs for async processing
- Implement proper logging
- Set up monitoring and alerts
- Consider using streaming responses for better UX
- Implement fallback mechanisms
This integration has enabled powerful AI features in our applications while maintaining reliability and cost control.