PHPackages                             laravelai/smartagent - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. laravelai/smartagent

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

laravelai/smartagent
====================

A Laravel package for managing AI agents with support for OpenAI and Gemini

011PHP

Since Oct 11Pushed 7mo agoCompare

[ Source](https://github.com/jbhai8058/laravel-ai-agent-package)[ Packagist](https://packagist.org/packages/laravelai/smartagent)[ RSS](/packages/laravelai-smartagent/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

Laravel AI Agent Package
========================

[](#laravel-ai-agent-package)

[![Latest Version on Packagist](https://camo.githubusercontent.com/6fd0d90b8b964d824bf7876090d77e76aae96eabfb69629c256342cb72ff12c9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c61726176656c61692f736d6172746167656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/laravelai/smartagent)[![Total Downloads](https://camo.githubusercontent.com/41f8ec788b8793d0287c12849fbfa071de85aaf7dcde785d93264a397d31c8d4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c61726176656c61692f736d6172746167656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/laravelai/smartagent)[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://opensource.org/licenses/MIT)[![PHP Version](https://camo.githubusercontent.com/99bdf9b8d305337de3ba3c2340d79e7f5af4ef8f2399a8a2cf41e43a8902598a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6c61726176656c61692f736d6172746167656e74)](https://php.net/)

> **Copyright © 2025 Muhammad Junaid Rehman Siddiqui**
>
> All rights reserved. This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
>
> **Note:** Unauthorized use, modification, or distribution of this software without proper attribution is strictly prohibited and may result in legal action.

A powerful Laravel package for integrating AI agents (OpenAI, Gemini) with advanced database awareness, automatic query generation, and safe execution.

[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://opensource.org/licenses/MIT)

> **Copyright © 2025 Muhammad Junaid Rehman Siddiqui**
>
> All rights reserved. This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
>
> **Note:** Unauthorized use, modification, or distribution of this software without proper attribution is strictly prohibited and may result in legal action.

A powerful Laravel package for integrating AI agents (OpenAI, Gemini) with database awareness and memory management.

Features
--------

[](#features)

- 🤖 **Multiple AI Providers** - Supports OpenAI and Gemini out of the box
- 🧠 **Conversation Memory** - Maintains context across multiple interactions
- 🗃️ **Schema-Aware** - Understands your database structure
- 🔄 **Smart Query Generation** - Converts natural language to SQL
- 🔗 **JOIN Support** - Automatically generates complex JOIN queries
- 🔒 **Safe Execution** - Validates and sanitizes all queries
- ⚡ **Performance Optimized** - Efficient schema analysis and caching
- 📊 **Query Analysis** - Explains and validates generated queries

Requirements
------------

[](#requirements)

- PHP 7.4+
- Laravel 8.0+
- Composer
- OpenAI API key or Google Cloud credentials (for Gemini)

Installation
------------

[](#installation)

1. Install via Composer:

    ```
    composer require laravelai/smartagent:dev-main
    ```
2. Run the install command to set up the package:

    ```
    php artisan ai-agents:install
    ```

    This will:

    - Publish the config file to `config/ai-agents.php`
    - Add required environment variables to your `.env` file
3. Add your API keys to `.env`:

    ```
    # For OpenAI
    OPENAI_API_KEY=your_openai_api_key

    # For Gemini
    GOOGLE_CLOUD_PROJECT_ID=your_project_id
    GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json

    # General Settings
    AI_AGENT_DRIVER=openai  # or 'gemini'
    AI_AGENT_MODEL=gpt-4    # or 'gemini-pro'
    ```

    Database Integration
    --------------------

    [](#database-integration)

### Schema-Aware Query Generation

[](#schema-aware-query-generation)

The package automatically analyzes your database schema to generate accurate SQL queries. It understands:

- Table relationships (foreign keys)
- Column data types
- Primary keys
- Indexes
- Table aliases

### Working with JOINs

[](#working-with-joins)

```
// Natural language to complex JOIN query
$result = AiAgent::queryWithDatabaseContext(
    "Show me all posts with their author names and comments count"
);

// Generated SQL will automatically include proper JOINs:
/*
SELECT p.*, u.name as author, COUNT(c.id) as comments_count
FROM posts p
JOIN users u ON p.user_id = u.id
LEFT JOIN comments c ON p.id = c.post_id
GROUP BY p.id, u.name
*/
```

### Schema Inspection

[](#schema-inspection)

Inspect your database schema programmatically:

```
use LaravelAI\SmartAgent\Services\DatabaseSchemaService;

$schema = new DatabaseSchemaService();

// Get complete schema
$fullSchema = $schema->getSchema();

// Get specific table details
$usersTable = $schema->getTableSchema('users');

// Check table relationships
$relationships = $schema->getTableRelationships('posts');
```

Basic Usage
-----------

[](#basic-usage)

```
use LaravelAI\SmartAgent\Facades\AiAgent;

// Simple chat with a single message
$response = AiAgent::chat([
    [
        'role' => 'user',
        'content' => 'Hello, how are you?'
    ]
]);

// With memory
// First message
AiAgent::chat([
    [
        'role' => 'user',
        'content' => 'My name is John'
    ]
]);

// Second message that will have context from the first
$response = AiAgent::chat([
    [
        'role' => 'user',
        'content' => 'What is my name?'
    ]
]); // Will remember the name is John
```

### Advanced Query Generation

[](#advanced-query-generation)

```
// Natural language to SQL
$result = AiAgent::queryWithDatabaseContext(
    "Show me all active users who made a purchase in the last 30 days"
);

// Complex queries with JOINs
$result = AiAgent::queryWithDatabaseContext(
    "Find all customers who purchased more than 5 items in the last month,
     along with their total spending, ordered by most recent purchase"
);

// Safe query execution with parameters
$users = AiAgent::executeSafeQuery(
    "SELECT u.*, COUNT(o.id) as order_count
     FROM users u
     LEFT JOIN orders o ON u.id = o.user_id
     WHERE u.active = ? AND o.created_at > ?
     GROUP BY u.id",
    [1, now()->subDays(30)]
);
```

#### Query Validation

[](#query-validation)

All generated queries are validated against:

- SQL syntax
- Table and column existence
- JOIN conditions
- Potentially dangerous operations
- Foreign key relationships

Configuration
-------------

[](#configuration)

Edit `config/ai-agents.php` for advanced configuration:

```
return [
    /*
    |--------------------------------------------------------------------------
    | Default AI Provider
    |--------------------------------------------------------------------------
    |
    | This option controls the default AI provider that will be used to
    | generate responses. Supported: "openai", "gemini"
    */
    'default' => env('AI_AGENT_DRIVER', 'openai'),

    /*
    |--------------------------------------------------------------------------
    | Database Settings
    |--------------------------------------------------------------------------
    */
    'database' => [
        'auto_load_schema' => true,
        'exclude_tables' => [
            'migrations',
            'password_resets',
            'telescope_*',
            'failed_jobs',
            'jobs',
            'sessions',
        ],
        'cache_ttl' => 3600, // Cache schema for 1 hour
    ],
];
```

Security
--------

[](#security)

- Only SELECT queries are allowed by default
- All queries are validated against the database schema
- Sensitive information is never logged

License
-------

[](#license)

This package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

Contributing
------------

[](#contributing)

Contributions are welcome! Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details.

Support
-------

[](#support)

For issues and feature requests, please use the [GitHub Issues](https://github.com/yourusername/laravel-ai-agents/issues) page.

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance45

Moderate activity, may be stable

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity15

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/bd8f9a63586fb1207a3639c57ebbb3a228e590cb11d18c185fd2a34708ab291c?d=identicon)[junaidrehman0406](/maintainers/junaidrehman0406)

---

Top Contributors

[![jbhai8058](https://avatars.githubusercontent.com/u/118351615?v=4)](https://github.com/jbhai8058 "jbhai8058 (50 commits)")

### Embed Badge

![Health badge](/badges/laravelai-smartagent/health.svg)

```
[![Health](https://phpackages.com/badges/laravelai-smartagent/health.svg)](https://phpackages.com/packages/laravelai-smartagent)
```

###  Alternatives

[passchn/cakephp-vite

ViteJS plugin for CakePHP

2220.8k1](/packages/passchn-cakephp-vite)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
