halilcosdu / laravel-chatbot
Laravel AI Chatbot 包
v1.2.3
2024-06-12 18:01 UTC
Requires
- php: ^8.2
- illuminate/contracts: ^10.0||^11.0
- openai-php/laravel: ^0.10.1
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- spatie/laravel-ray: ^1.35
This package is auto-updated.
Last update: 2024-09-08 23:10:51 UTC
README
Laravel Chatbot 提供了一种强大且易于使用的解决方案,可以将 AI 聊天机器人集成到您的 Laravel 应用程序中。利用 OpenAI 的力量,它允许您直接从 Laravel 应用程序中创建、管理和交互聊天线程。无论是构建客户服务聊天机器人还是交互式 AI 助手,laravel-chatbot
都提供了对 OpenAI API 的流畅、Laravel 优化的界面。
安装
您可以通过 composer 安装此包
composer require halilcosdu/laravel-chatbot
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="chatbot-config"
这是已发布配置文件的内容
您需要在 OpenAI 上创建一个助手并获取 API 密钥和助手 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 model must have threadMessages(): HasMany relation // ThreadMessage model mush have thread(): BelongsTo relation 'thread' => \HalilCosdu\ChatBot\Models\Thread::class, 'thread_messages' => \HalilCosdu\ChatBot\Models\ThreadMessage::class ], ];
您可以使用以下命令发布和运行迁移
php artisan vendor:publish --tag="chatbot-migrations"
php artisan migrate
这将迁移以下表
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(); });
使用方法
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(): LengthAwarePaginator; /* List all threads */ ChatBot::createThread('Hello, what is the capital of Turkey?'): Model; /* Create a new thread */ ChatBot::thread($id): Model; /* Get a thread with messages */ ChatBot::updateThread('Where should I visit?', $id): Model; /* Continue the conversation */ ChatBot::deleteThread($id): void; /* Delete the thread */
原始数据 - 未保存到数据库
您可以使用以下方法直接与 OpenAI API 交互。
public function createThreadAsRaw(string $subject) public function listThreadMessagesAsRaw(string $remoteThreadId) public function updateThreadAsRaw(string $remoteThreadId, array $data) /* $data = ['role' => 'user or assistant', 'content' => 'Hello'] */ public function deleteThreadAsRaw(string $remoteThreadId) public function threadAsRaw(string $threadId) public function messageAsRaw(string $threadId, string $messageId) public function modifyMessageAsRaw(string $threadId, string $messageId, array $parameters)
ChatBot::createThreadAsRaw(string $subject); ChatBot::listThreadMessagesAsRaw(string $remoteThreadId); ChatBot::updateThreadAsRaw(string $remoteThreadId, array $data); ChatBot::deleteThreadAsRaw(string $remoteThreadId); ChatBot::threadAsRaw(string $threadId); ChatBot::messageAsRaw(string $threadId, string $messageId); ChatBot::modifyMessageAsRaw(string $threadId, string $messageId, array $parameters);
测试
composer test
变更日志
请参阅 变更日志 了解最近更改的详细信息。
贡献
请参阅 贡献指南 了解详细信息。
安全漏洞
请参阅 我们的安全策略 了解如何报告安全漏洞。
致谢
许可协议
MIT 许可协议(MIT)。请参阅 许可文件 了解更多信息。