Skip to content
Halil Coşdu edited this page Apr 23, 2024 · 2 revisions

Laravel AI Chatbot Package

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package, laravel-chatbot, provides a robust and easy-to-use solution for integrating AI chatbots into your Laravel applications. Leveraging the power of OpenAI, it allows you to create, manage, and interact with chat threads directly from your Laravel application. Whether you're building a customer service chatbot or an interactive AI assistant, laravel-chatbot offers a streamlined, Laravel-friendly interface to the OpenAI API.

Installation

You can install the package via composer:

composer require halilcosdu/laravel-chatbot

You can publish the config file with:

php artisan vendor:publish --tag="chatbot-config"

This is the contents of the published config file:

You have to create an assistant on OpenAI and get the API key and assistant ID.

https://platform.openai.com/assistants

return [
    'assistant_id' => env('OPENAI_API_ASSISTANT_ID'),
    'api_key' => env('OPENAI_API_KEY'),
    'organization' => env('OPENAI_ORGANIZATION'),
    'request_timeout' => env('OPENAI_TIMEOUT'),
    'sleep_seconds' => env('OPENAI_SLEEP_SECONDS'), // Sleep seconds between requests default .1
    'models' => [
        'thread' => \HalilCosdu\ChatBot\Models\Thread::class,
    ],
];

You can publish and run the migrations with:

php artisan vendor:publish --tag="chatbot-migrations"
php artisan migrate

This will migrate the following tables:

Schema::create('threads', function (Blueprint $table) {
    $table->id();
    $table->string('owner_id')->nullable()->index();
    $table->string('subject');
    $table->string('remote_thread_id')->index();

    $table->timestamps();
});
Schema::create('thread_messages', function (Blueprint $table) {
    $table->id();
    $table->foreignIdFor(config('chatbot.models.thread'))->constrained()->cascadeOnDelete();
    $table->string('role')->index();
    $table->longText('content');

    $table->timestamps();
});

Usage

public function listThreads(mixed $ownerId = null, mixed $search = null, mixed $appends = null): \Illuminate\Contracts\Pagination\LengthAwarePaginator
public function createThread(string $subject, mixed $ownerId = null): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder
public function thread(int $id, mixed $ownerId = null): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder
public function updateThread(string $message, int $id, mixed $ownerId = null): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder
public function deleteThread(int $id, mixed $ownerId = null): void
ChatBot::listThreads(); /* List all threads */
ChatBot::createThread('Hello'); /* Create a new thread */
ChatBot::thread($id); /* Get a thread with messages */
ChatBot::updateThread('Hi', $id); /* Continue the conversation */
ChatBot::deleteThread($id); /* Delete the thread */

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.