dits-sa/laravel-ai-assistant

A dynamic AI assistant package for Laravel applications that automatically discovers schema and provides real-time AI capabilities

Installs: 8

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/dits-sa/laravel-ai-assistant

dev-main 2025-10-05 22:51 UTC

This package is auto-updated.

Last update: 2025-10-05 22:51:50 UTC


README

Latest Version Total Downloads License Laravel Version

A dynamic AI assistant package for Laravel applications that automatically discovers schema and provides real-time AI capabilities. Works with any Laravel application without configuration!

๐Ÿš€ Features

  • Dynamic Schema Discovery: Automatically analyzes your Laravel models and database structure
  • AI Metadata Generation: Creates comprehensive metadata for AI consumption
  • Real-time Communication: WebSocket support for instant AI responses
  • Tool Discovery: Automatically creates CRUD tools for each model
  • Secure Authentication: Token-based authentication with IP restrictions
  • Conversation Management: Persistent chat history and context
  • Extensible: Easy to add custom tools and capabilities
  • Zero Configuration: Works out of the box with any Laravel app
  • Production Ready: Built-in security, validation, and error handling

โšก Quick Start (5 Minutes)

1. Install Package

composer require dits-sa/laravel-ai-assistant

2. Install and Configure

php artisan ai:install

3. Add Trait to Your Models

<?php
// app/Models/Project.php

use LaravelAIAssistant\Traits\AICapable;

class Project extends Model
{
    use AICapable;
}

4. Generate AI Metadata

php artisan ai:generate-metadata

5. Test API

# Get AI token
curl -X POST "http://your-app.com/api/ai/auth/token" \
  -H "Authorization: Bearer YOUR_AUTH_TOKEN"

# List projects
curl -X GET "http://your-app.com/api/ai/models/project" \
  -H "X-AI-Token: YOUR_AI_TOKEN"

That's it! Your Laravel app now has AI capabilities! ๐ŸŽ‰

๐Ÿ“ฆ Installation

Method 1: Install from Packagist (Recommended)

composer require dits-sa/laravel-ai-assistant

Method 2: Install from GitHub

composer require dits-sa/laravel-ai-assistant:dev-main

Method 3: Install from Local Path

# Add repository
composer config repositories.local path ./packages/laravel-ai-assistant

# Install package
composer require dits-sa/laravel-ai-assistant:dev-main

2. Install and Configure

php artisan ai:install

This will:

  • Publish configuration files
  • Run database migrations
  • Generate initial AI metadata

3. Add Trait to Your Models

<?php

use LaravelAIAssistant\Traits\AICapable;

class Project extends Model
{
    use AICapable;
    
    // Optional: Override AI methods for custom behavior
    public function aiSearch(string $query, array $fields = []): Collection
    {
        // Custom search logic
        return $this->where('name', 'like', "%{$query}%")
                   ->orWhere('description', 'like', "%{$query}%")
                   ->get();
    }
}

4. Configure Your Models

Edit config/ai-assistant.php:

'capabilities' => [
    'per_model' => [
        'Project' => [
            'can_create' => true,
            'can_update' => true,
            'can_delete' => false,
        ],
        'User' => [
            'can_create' => false,
            'can_delete' => false,
        ],
    ]
],

5. Generate Metadata

php artisan ai:generate-metadata

๐Ÿ”ง Configuration

Basic Configuration

// config/ai-assistant.php

return [
    'enabled' => true,
    
    'api' => [
        'prefix' => 'api/ai',
        'middleware' => ['auth:sanctum', 'ai.security'],
        'rate_limit' => '60,1',
    ],

    'security' => [
        'token_expiry' => 24, // hours
        'max_requests_per_minute' => 60,
        'allowed_ips' => '', // comma-separated
    ],

    'models' => [
        'exclude' => ['User', 'PasswordReset', 'Migration'],
        'include_only' => [], // If specified, only these models
        'custom_descriptions' => [
            'Project' => 'Investment projects and properties',
        ]
    ],

    'capabilities' => [
        'default' => [
            'can_list' => true,
            'can_search' => true,
            'can_create' => false,
            'can_update' => false,
            'can_delete' => false,
        ],
        'per_model' => [
            'Project' => ['can_create' => true, 'can_update' => true],
        ]
    ],
];

๐Ÿ› ๏ธ Usage

API Endpoints

The package automatically creates dynamic API endpoints for all your models:

Method Endpoint Description
GET /api/ai/metadata Get AI metadata and available tools
GET /api/ai/models/{modelName} List model records with pagination
GET /api/ai/models/{modelName}/search Search model records
POST /api/ai/models/{modelName} Create new model record
PUT /api/ai/models/{modelName} Update existing model record
DELETE /api/ai/models/{modelName} Delete model record
GET /api/ai/models/{modelName}/{id} Get single model record
POST /api/ai/auth/token Generate AI authentication token
POST /api/ai/auth/validate Validate AI token
GET /api/ai/conversations List user conversations
POST /api/ai/conversations Create new conversation

Dynamic Model Support

The package automatically discovers and creates API endpoints for any Laravel model:

  • User โ†’ /api/ai/models/user
  • Project โ†’ /api/ai/models/project
  • Order โ†’ /api/ai/models/order
  • Product โ†’ /api/ai/models/product
  • Any Model โ†’ /api/ai/models/{ModelName}

Authentication

Generate an AI token:

curl -X POST /api/ai/auth/token \
  -H "Authorization: Bearer YOUR_AUTH_TOKEN" \
  -H "Content-Type: application/json"

Use the token for AI requests:

curl -X GET /api/ai/metadata \
  -H "X-AI-Token: YOUR_AI_TOKEN" \
  -H "Content-Type: application/json"

Example API Usage

List Records

curl -X GET "/api/ai/models/project?limit=10&offset=0" \
  -H "X-AI-Token: YOUR_AI_TOKEN"

Search Records

curl -X GET "/api/ai/models/project/search?query=investment&limit=5" \
  -H "X-AI-Token: YOUR_AI_TOKEN"

Create Record

curl -X POST "/api/ai/models/project" \
  -H "X-AI-Token: YOUR_AI_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"name": "New Project", "description": "Project description"}'

Filter Records

curl -X GET "/api/ai/models/project?filters[status]=active&filters[type]=investment" \
  -H "X-AI-Token: YOUR_AI_TOKEN"

๐Ÿค– AI Integration

Real-World Examples

Example 1: E-commerce Store

# List all products
curl -X GET "/api/ai/models/product?limit=20" \
  -H "X-AI-Token: YOUR_AI_TOKEN"

# Search for products
curl -X GET "/api/ai/models/product/search?query=laptop&limit=10" \
  -H "X-AI-Token: YOUR_AI_TOKEN"

# Create new product
curl -X POST "/api/ai/models/product" \
  -H "X-AI-Token: YOUR_AI_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"name": "MacBook Pro", "price": 1999.99, "category": "laptops"}'

# Update product status
curl -X PUT "/api/ai/models/product" \
  -H "X-AI-Token: YOUR_AI_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"id": 123, "status": "active"}'

Example 2: Project Management

# List active projects
curl -X GET "/api/ai/models/project?filters[status]=active" \
  -H "X-AI-Token: YOUR_AI_TOKEN"

# Search projects by client
curl -X GET "/api/ai/models/project/search?query=client_name&fields=name,client" \
  -H "X-AI-Token: YOUR_AI_TOKEN"

# Get project with relationships
curl -X GET "/api/ai/models/project?with=client,tasks&id=123" \
  -H "X-AI-Token: YOUR_AI_TOKEN"

Example 3: User Management

# List users with pagination
curl -X GET "/api/ai/models/user?limit=50&offset=0" \
  -H "X-AI-Token: YOUR_AI_TOKEN"

# Search users by role
curl -X GET "/api/ai/models/user?filters[role]=admin" \
  -H "X-AI-Token: YOUR_AI_TOKEN"

# Get user profile with relationships
curl -X GET "/api/ai/models/user?with=profile,orders&id=456" \
  -H "X-AI-Token: YOUR_AI_TOKEN"

Frontend Integration

React Component

import React, { useState, useEffect } from 'react';
import { DynamicAIChat } from '@/components/AI/DynamicAIChat';

function App() {
    const [showAI, setShowAI] = useState(false);
    const [aiToken, setAiToken] = useState(null);
    
    useEffect(() => {
        // Get AI token when component mounts
        fetchAIToken();
    }, []);
    
    const fetchAIToken = async () => {
        try {
            const response = await fetch('/api/ai/auth/token', {
                method: 'POST',
                headers: {
                    'Authorization': `Bearer ${authToken}`,
                    'Content-Type': 'application/json'
                }
            });
            
            if (response.ok) {
                const data = await response.json();
                setAiToken(data.data.token);
            }
        } catch (error) {
            console.error('Failed to get AI token:', error);
        }
    };
    
    return (
        <div>
            <button 
                onClick={() => setShowAI(true)}
                className="fixed bottom-4 right-4 bg-blue-500 text-white p-3 rounded-full shadow-lg hover:bg-blue-600"
            >
                ๐Ÿค– AI Assistant
            </button>
            
            <DynamicAIChat 
                isOpen={showAI} 
                onClose={() => setShowAI(false)}
                aiToken={aiToken}
            />
        </div>
    );
}

Vue.js Component

<template>
    <div>
        <button @click="showAI = true" class="ai-button">
            ๐Ÿค– AI Assistant
        </button>
        
        <DynamicAIChat 
            v-if="showAI"
            :is-open="showAI"
            @close="showAI = false"
            :ai-token="aiToken"
        />
    </div>
</template>

<script>
import DynamicAIChat from '@/components/AI/DynamicAIChat.vue';

export default {
    components: {
        DynamicAIChat
    },
    data() {
        return {
            showAI: false,
            aiToken: null
        }
    },
    async mounted() {
        await this.fetchAIToken();
    },
    methods: {
        async fetchAIToken() {
            try {
                const response = await fetch('/api/ai/auth/token', {
                    method: 'POST',
                    headers: {
                        'Authorization': `Bearer ${this.authToken}`,
                        'Content-Type': 'application/json'
                    }
                });
                
                if (response.ok) {
                    const data = await response.json();
                    this.aiToken = data.data.token;
                }
            } catch (error) {
                console.error('Failed to get AI token:', error);
            }
        }
    }
}
</script>

VPS Setup for Real-time AI

  1. Deploy AI Service to your VPS
  2. Configure WebSocket for real-time communication
  3. Set up OpenAI integration
  4. Connect to Laravel API

See the VPS Setup Guide for detailed instructions.

๐Ÿ“Š Commands

Generate Metadata

php artisan ai:generate-metadata

Options:

  • --output=path - Custom output file path
  • --format=json|yaml - Output format
  • --force - Force regeneration

Clear Cache

php artisan ai:clear-cache

Options:

  • --all - Clear all AI cache
  • --metadata - Clear only metadata cache
  • --tokens - Clear only token cache

Install Package

php artisan ai:install

Options:

  • --force - Force installation
  • --publish-config - Publish config only
  • --publish-migrations - Publish migrations only

๐Ÿ”’ Security

Token Security

  • Tokens are stored in cache with expiration
  • IP restrictions can be configured
  • Rate limiting prevents abuse
  • Tokens are validated on each request

Input Validation

  • All inputs are validated and sanitized
  • SQL injection protection
  • XSS protection
  • CSRF protection

Rate Limiting

  • Configurable rate limits per IP
  • Separate limits for different operations
  • Automatic blocking of abusive requests

๐Ÿงช Testing

# Run package tests
composer test

# Run specific test
php artisan test --filter=AIAssistantTest

๐Ÿ“ˆ Performance

Caching

  • Metadata is cached for performance
  • Configurable cache TTL
  • Automatic cache invalidation

Database Optimization

  • Efficient queries with proper indexing
  • Eager loading for relationships
  • Pagination for large datasets

Memory Management

  • Optimized for memory usage
  • Garbage collection for large operations
  • Connection pooling

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Submit a pull request

๐Ÿ“„ License

This package is open-sourced software licensed under the MIT license.

๐Ÿ†˜ Support & Troubleshooting

Common Issues

Package Not Found

# Clear composer cache
composer clear-cache

# Update composer
composer update

# Check if package is installed
composer show dits-sa/laravel-ai-assistant

Trait Not Found

// Make sure you have the correct import
use LaravelAIAssistant\Traits\AICapable;

class YourModel extends Model
{
    use AICapable;
}

API Endpoints Not Working

# Check if routes are registered
php artisan route:list | grep ai

# Clear route cache
php artisan route:clear

# Check middleware configuration
php artisan config:show ai-assistant

Metadata Generation Fails

# Clear all caches
php artisan cache:clear
php artisan config:clear
php artisan route:clear

# Regenerate metadata
php artisan ai:clear-cache --all
php artisan ai:generate-metadata --force

Debug Commands

# Check package installation
composer show dits-sa/laravel-ai-assistant

# Test schema analysis
php artisan tinker
>>> app(\LaravelAIAssistant\Services\SchemaAnalyzer::class)->analyzeApplication()

# Test metadata generation
php artisan tinker
>>> app(\LaravelAIAssistant\Services\AIMetadataGenerator::class)->generateMetadata()

# Check configuration
php artisan config:show ai-assistant

Getting Help

Performance Tips

  1. Enable Caching: Configure Redis or Memcached for better performance
  2. Optimize Queries: Use eager loading for relationships
  3. Rate Limiting: Configure appropriate rate limits for your use case
  4. Database Indexing: Add indexes for frequently searched fields

โ“ Frequently Asked Questions

Q: Does this work with any Laravel application?

A: Yes! The package automatically discovers your models and database structure, so it works with any Laravel application without configuration.

Q: Do I need to modify my existing models?

A: No! You only need to add the AICapable trait to models you want to make available to the AI. Your existing models remain unchanged.

Q: Is this secure?

A: Yes! The package includes token-based authentication, rate limiting, input validation, and IP restrictions. All data is validated and sanitized.

Q: Can I customize the AI capabilities?

A: Yes! You can override any AI method in your models and configure capabilities in the config file.

Q: Does this work with Laravel 9, 10, and 11?

A: Yes! The package supports Laravel 9, 10, and 11 with PHP 8.1+.

Q: Can I use this in production?

A: Absolutely! The package is production-ready with comprehensive testing, security features, and error handling.

Q: How does the dynamic discovery work?

A: The package scans your app/Models directory, analyzes your database schema, and automatically creates API endpoints and AI tools for each model.

Q: Can I add custom AI tools?

A: Yes! You can define custom tools in the configuration file or override methods in your models.

๐Ÿ”„ Changelog

v1.0.0 (2024-01-15)

  • โœจ Initial Release
  • ๐Ÿš€ Dynamic schema discovery for any Laravel application
  • ๐Ÿค– AI metadata generation with comprehensive descriptions
  • ๐Ÿ”Œ Dynamic API endpoints for all model operations
  • ๐Ÿ›ก๏ธ Security middleware with rate limiting and token validation
  • ๐Ÿ’ฌ Conversation management with persistent chat history
  • ๐ŸŽฏ AICapable trait for easy model integration
  • ๐Ÿ“Š Artisan commands for package management
  • โš›๏ธ React frontend components for AI chat interface
  • ๐Ÿ“š Complete documentation and installation guides
  • ๐Ÿ”ง Zero configuration - works out of the box
  • ๐Ÿญ Production ready with comprehensive testing
  • ๐ŸŒ Support for Laravel 9, 10, and 11
  • ๐Ÿ˜ PHP 8.1+ support
  • ๐Ÿ“„ MIT License

๐Ÿ† Why Choose Laravel AI Assistant?

  • ๐Ÿš€ Zero Configuration: Works with any Laravel app instantly
  • ๐Ÿ” Dynamic Discovery: Automatically finds and analyzes your data
  • ๐Ÿ›ก๏ธ Production Ready: Built-in security and error handling
  • โšก High Performance: Optimized for speed and memory usage
  • ๐Ÿ”ง Extensible: Easy to customize and extend
  • ๐Ÿ“š Well Documented: Comprehensive guides and examples
  • ๐ŸŒ Universal: Works with any Laravel application
  • ๐Ÿ’ก Smart: AI-powered data understanding and manipulation

Made with โค๏ธ by DITS SA for the Laravel community

GitHub Packagist License