A simple and effective integration between Laravel and ChatGPT. This package provides a clean, developer-friendly interface for interacting with the OpenAI API, including support for chat, text completions, and image analysis with GPT-4 Vision.
- ✅ Chat Completions – Conversational AI with memory
- ✅ Text Completions – Continue or generate texts
- ✅ Vision Analysis – Image understanding with GPT-4 Vision
- ✅ Rate Limiting – Configurable request throttling
- ✅ Retry Logic – Automatic retries on failures
- ✅ Multiple Models – GPT-3.5, GPT-4, and GPT-4 Vision supported
- ✅ Laravel Integration – Includes Service Provider and Facade
- ✅ Auto Discovery – Automatically registered in Laravel
Install the package via Composer:
composer require sysborg/chatgptTo publish the config file:
php artisan vendor:publish --tag=chatgpt-configAdd your OpenAI API key in the .env file:
OPENAI_API_KEY=your_openai_api_key_hereCustomize config/chatgpt.php to match your needs:
return [
'api_key' => env('OPENAI_API_KEY'),
'base_url' => env('OPENAI_BASE_URL', 'https://api.openai.com/v1'),
'default_model' => env('OPENAI_DEFAULT_MODEL', 'gpt-3.5-turbo'),
'timeout' => env('OPENAI_TIMEOUT', 60),
'max_tokens' => env('OPENAI_MAX_TOKENS', 1000),
'temperature' => env('OPENAI_TEMPERATURE', 0.7),
'retry_attempts' => env('OPENAI_RETRY_ATTEMPTS', 2),
'retry_delay' => env('OPENAI_RETRY_DELAY', 1),
'rate_limit' => env('OPENAI_RATE_LIMIT', 60), // 0 = unlimited
];use Sysborg\ChatGPT\Facades\ChatGPT;
$response = ChatGPT::chat('Hello, how are you?');
echo $response->getContent();$messages = [
['role' => 'user', 'content' => 'What is the capital of Brazil?'],
['role' => 'assistant', 'content' => 'The capital of Brazil is Brasília.'],
['role' => 'user', 'content' => 'What is the population?']
];
$response = ChatGPT::chatWithHistory($messages);
echo $response->getContent();$response = ChatGPT::completion('Once upon a time in a distant kingdom');
echo $response->getContent();$response = ChatGPT::vision(
'https://example.com/image.jpg',
'Describe this image in detail'
);
// With base64 image
$base64 = base64_encode(file_get_contents('/path/to/image.jpg'));
$response = ChatGPT::visionFromBase64(
$base64,
'What do you see in this image?'
);
echo $response->getAnalysis();$response = ChatGPT::setModel('gpt-4')
->setTemperature(0.9)
->setMaxTokens(2000)
->chat('Tell a creative story');$response = ChatGPT::chat('Hi!');
echo $response->getContent();
echo $response->getModel();
echo $response->getTotalTokens();
echo $response->getFinishReason();
if ($response->isComplete()) {
echo "Completed successfully";
}
if ($response->isTruncated()) {
echo "Response was truncated";
}$response = ChatGPT::vision($imageUrl, 'Analyze this image');
echo $response->getAnalysis();
print_r($response->getDetectedEntities());
echo $response->getSummary();
if ($response->hasSafetyConcerns()) {
echo "This image may contain sensitive content";
}use Sysborg\ChatGPT\Exceptions\ChatGPTException;
use Sysborg\ChatGPT\Exceptions\RateLimitException;
try {
$response = ChatGPT::chat('Hi!');
} catch (RateLimitException $e) {
echo "Rate limit exceeded. Retry in: " . $e->getRetryAfter() . " seconds";
} catch (ChatGPTException $e) {
echo "API error: " . $e->getMessage();
print_r($e->getContext());
}Built-in rate limit handling:
$status = ChatGPT::getRateLimitStatus();
echo "Requests remaining: " . $status['remaining'];
ChatGPT::resetRateLimit(); // Useful for testing$models = ChatGPT::getAvailableModels();
print_r($models);Example Output:
[
'gpt-3.5-turbo',
'gpt-3.5-turbo-16k',
'gpt-4',
'gpt-4-turbo',
'gpt-4-vision-preview',
'gpt-4-32k'
]$response = ChatGPT::chatWithHistory([
['role' => 'system', 'content' => 'You are a programming assistant.'],
['role' => 'user', 'content' => 'How do I create a REST API in Laravel?']
], [
'model' => 'gpt-4',
'temperature' => 0.3,
'max_tokens' => 1500,
'top_p' => 0.9
]);$response = ChatGPT::vision($imageUrl, 'Analyze this image', [
'model' => 'gpt-4-vision-preview',
'detail' => 'high', // Options: low, high, auto
'max_tokens' => 1000
]);Contributions are welcome! Please open an issue or submit a pull request.
This package is licensed under the MIT License.
Anderson Arruda 📧 andmarruda@gmail.com 🐙 @andmarruda on GitHub
If you encounter any issues or have questions, please open a GitHub Issue.