syafiq-unijaya/laravel-ai-chatbox
Installation
composer require syafiq-unijaya/laravel-ai-chatbox
php artisan vendor:publish --provider="SyafiqUnijaya\LaravelAiChatbox\LaravelAiChatboxServiceProvider" --tag="config"
php artisan migrate
Configure API
Update .env with your AI provider credentials (e.g., OpenAI, Ollama):
AI_CHATBOX_API_KEY=your_api_key_here
AI_CHATBOX_MODEL=gpt-3.5-turbo
AI_CHATBOX_PROVIDER=openai # or 'ollama', 'groq', etc.
First Use Case Add the chatbox to a Blade template:
@aiChatbox
This renders the Vue 3 frontend with default styling.
php artisan ai:test-connection to verify your AI provider is working./ai-chatbox/admin to manage conversations, models, and RAG configurations.use SyafiqUnijaya\LaravelAiChatbox\Facades\AiChatbox;
$response = AiChatbox::sendMessage("Hello, how are you?", [
'model' => 'gpt-4',
'temperature' => 0.7,
]);
Integrating with Existing Views
@aiChatbox directive in Blade templates for a quick drop-in.ai_chatbox.php):
'widget' => [
'position' => 'bottom-right', // 'top-left', 'bottom-right', etc.
'width' => '350px',
'theme' => 'light', // 'dark', 'custom'
],
Streaming Responses
streaming: true in the config.AiChatbox::streamMessage("What's the weather today?", function ($chunk) {
// Process chunks in real-time (e.g., log, display, or analyze)
});
RAG (Retrieval-Augmented Generation)
config/ai_chatbox.php:
'rag' => [
'sources' => [
'documents' => [
'type' => 'database', // or 'filesystem', 's3', etc.
'model' => SyafiqUnijaya\LaravelAiChatbox\RAG\Sources\DatabaseSource::class,
'config' => [
'table' => 'knowledge_base',
'column' => 'content',
],
],
],
],
AiChatbox::sendMessage("Explain our product features.", [
'rag' => ['sources' => ['documents']],
]);
Conversation Memory
'memory' => [
'driver' => 'database',
'table' => 'ai_chatbox_conversations',
],
Conversation model or use middleware to manage memory.SyafiqUnijaya\LaravelAiChatbox\Middleware\RateLimit).php artisan vendor:publish --tag="ai-chatbox-assets"
resources/js/ai-chatbox.js.tenant() method on the facade to scope conversations:
AiChatbox::tenant('company_123')->sendMessage("Hello...");
AiChatbox::onMessageSent(function ($message) {
// Log or process the message
});
API Key Security
if (empty(config('ai_chatbox.api_key'))) {
throw new \RuntimeException("AI_CHATBOX_API_KEY is not set.");
}
Token Limits
AiChatbox::pruneOldConversations(30); // Keep last 30 messages
AiChatbox::getTokenUsage().RAG Performance
php artisan ai:rag-test
Streaming Issues
stream: true).// In ai-chatbox.js
useEffect(() => {
const eventSource = new EventSource(`/ai-chatbox/stream/${conversationId}`);
eventSource.onerror = () => {
// Fallback to polling or notify user
};
}, [conversationId]);
Log AI Responses Enable logging in config:
'logging' => [
'enabled' => true,
'channel' => 'single',
],
View logs in storage/logs/ai_chatbox.log.
Validate API Responses
Use the debug method to inspect raw responses:
$response = AiChatbox::sendMessage("Test", ['debug' => true]);
dd($response->raw());
Frontend Debugging
Custom Providers
Extend the Provider class to support non-OpenAI APIs:
namespace App\Providers;
use SyafiqUnijaya\LaravelAiChatbox\Contracts\Provider;
class CustomProvider implements Provider {
public function send($message, array $options) {
// Implement custom logic
}
}
Register in config:
'providers' => [
'custom' => App\Providers\CustomProvider::class,
],
Middleware Add custom logic before/after API calls:
AiChatbox::before(function ($request) {
// Modify request (e.g., add headers)
});
AiChatbox::after(function ($response) {
// Process response (e.g., sanitize output)
});
Event Listeners Extend functionality via events:
// Listen for message sending
AiChatbox::onMessageSending(function ($message) {
// Modify message or cancel
return $message;
});
// Listen for responses
AiChatbox::onMessageReceived(function ($message, $response) {
// Log or process response
});
Custom RAG Sources Add new data sources (e.g., S3, Elasticsearch):
'rag' => [
'sources' => [
's3_documents' => [
'type' => 's3',
'model' => App\RAG\Sources\S3Source::class,
'config' => [
'bucket' => 'my-bucket',
'prefix' => 'docs/',
],
],
],
],
Model Fallbacks If a model fails, specify fallbacks:
'models' => [
'gpt-3.5-turbo' => [
'provider' => 'openai',
'fallback' => 'gpt-3.5-turbo-0301',
],
],
CORS for Streaming Ensure your server allows streaming endpoints:
// In routes/web.php
Route::middleware(['cors:sanctum'])->group(function () {
// AI streaming routes
});
Vue 3 Compatibility
How can I help you explore Laravel packages today?