PHP code example of sentix / ai-chatbot

1. Go to this page and download the library: Download sentix/ai-chatbot library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sentix / ai-chatbot example snippets


namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Sentix\AiChatbot\Traits\HasAiContext;

class Product extends Model
{
    use HasAiContext;

    // Define fields exposed to the AI chatbot (defaults to $fillable if not set)
    protected $aiFields = ['id', 'name', 'price', 'description', 'in_stock'];

    // Relationships are automatically detected if the related model ALSO uses HasAiContext!
    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public function reviews()
    {
        return $this->hasMany(Review::class);
    }
}

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Sentix\AiChatbot\Traits\HasAiContext;

class Category extends Model
{
    use HasAiContext;

    protected $aiFields = ['id', 'name', 'slug'];
}

'context_models' => [
    \App\Models\Product::class,
    \App\Models\Category::class,
    \App\Models\Order::class,
],

public function scopeUserScoped($query, $userId)
{
    return $query->where('customer_id', $userId);
}

'custom_context' => [
    'How to add products' => 'Go to Admin > Products > Click Add Product. Fill in Name, Price, SKU, and Category.',
    'Menu Navigation' => 'Header menu 

use Sentix\AiChatbot\Contracts\AiToolInterface;
use Sentix\AiChatbot\Tools\AiToolRegistry;

class GetOrderStatusTool implements AiToolInterface
{
    public function name(): string
    {
        return 'get_order_status';
    }

    public function description(): string
    {
        return 'Get live delivery status of an order using order ID.';
    }

    public function parameters(): array
    {
        return [
            'order_id' => 'string (

use Sentix\AiChatbot\Contracts\AiDriverInterface;
use Sentix\AiChatbot\Drivers\AiDriverFactory;

class MyCustomAiDriver implements AiDriverInterface
{
    public function ask(string $prompt, array $chatHistory, string $systemPrompt, array $config): string
    {
        return "Response from MyCustomAiDriver";
    }

    public function askStream(string $prompt, array $chatHistory, string $systemPrompt, array $config, \Closure $onChunk): void
    {
        $onChunk("Streaming chunk response...");
    }

    public function testConnection(string $apiKey, string $model): bool
    {
        return true;
    }
}

// Register in your AppServiceProvider:
AiDriverFactory::extend('my_custom_ai', MyCustomAiDriver::class);
bash
# Publish configuration file
php artisan vendor:publish --tag=ai-chatbot-config

# Publish frontend assets (CSS/JS)
php artisan vendor:publish --tag=ai-chatbot-assets
html
<!-- Blade Component Syntax (Recommended) -->
<x-ai-chatbot />

<!-- Or with Custom Props -->
<x-ai-chatbot name="Support Assistant" primary-color="#6366f1" secondary-color="#9333ea" />

<!-- Or Blade Directive Syntax -->
@aiChatbotWidget