-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jean-Baptiste Loup
committed
Jun 26, 2023
0 parents
commit 9bad46a
Showing
10 changed files
with
356 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Laravel ChatGPT | ||
|
||
### Documentation | ||
|
||
1. Installation: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "ogrre/laravel-chatgpt", | ||
"description": "Package to add chat management for openai client", | ||
"type": "library", | ||
"require": { | ||
"php" : "^7.3|^8.0|^8.1", | ||
"illuminate/database": "^7.0|^8.0|^9.0", | ||
"illuminate/support": "10.x-dev" | ||
}, | ||
"homepage": "https://github.com/ogrre/laravel-chatgpt", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Ogrre", | ||
"homepage": "https://jbloup.dev", | ||
"role": "Developer" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Ogrre\\ChatGPT\\": "src/" | ||
} | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
}, | ||
"minimum-stability": "dev", | ||
"prefer-stable": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
return [ | ||
|
||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('chats', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->timestamps(); | ||
|
||
$table->string('model_type'); | ||
$table->unsignedBigInteger('model_id'); | ||
|
||
$table->primary(['id', 'model_type', 'model_id']); | ||
}); | ||
|
||
Schema::create('messages', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->string('role'); | ||
$table->text('content'); | ||
$table->unsignedBigInteger('chat_id'); | ||
$table->foreign('chat_id') | ||
->references('id') | ||
->on('chats') | ||
->onDelete('cascade'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('chats'); | ||
Schema::dropIfExists('messages'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('chats', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->timestamps(); | ||
|
||
$table->string('model_type'); | ||
$table->unsignedBigInteger('model_id'); | ||
|
||
$table->primary(['id', 'model_type', 'model_id']); | ||
}); | ||
|
||
Schema::create('messages', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->string('role'); | ||
$table->text('content'); | ||
$table->unsignedBigInteger('chat_id'); | ||
$table->foreign('chat_id') | ||
->references('id') | ||
->on('chats') | ||
->onDelete('cascade'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('chats'); | ||
Schema::dropIfExists('messages'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Ogrre\ChatGPT; | ||
|
||
use Ogrre\ChatGPT\Models\Chat; | ||
|
||
class ChatRegistrar | ||
{ | ||
protected string $chatClass; | ||
|
||
public function __construct(){ | ||
|
||
$this->chatClass = config('chat.models.chat'); | ||
} | ||
|
||
/** | ||
* @return Chat | ||
*/ | ||
public function getChatClass(): Chat | ||
{ | ||
return app($this->chatClass); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Ogrre\ChatGPT; | ||
|
||
use Illuminate\Contracts\Container\BindingResolutionException; | ||
use Illuminate\Filesystem\Filesystem; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class ChatServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Register services. | ||
* | ||
* @return void | ||
*/ | ||
public function register(): void | ||
{ | ||
$this->mergeConfigFrom( | ||
__DIR__.'/../config/chat.php', 'chat' | ||
); | ||
} | ||
|
||
/** | ||
* Bootstrap services. | ||
* | ||
* @return void | ||
* @throws BindingResolutionException | ||
*/ | ||
public function boot(): void | ||
{ | ||
$this->app->singleton(ChatRegistrar::class, function (){ | ||
return new ChatRegistrar(); | ||
}); | ||
|
||
$this->publishes([ | ||
__DIR__.'/../config/chat.php' => config_path('chat.php'), | ||
], 'config'); | ||
|
||
$this->publishes([ | ||
__DIR__.'/../database/migrations/create_chat_gpt_tables.php.stub' => $this->getMigrationFileName('create_chat_gpt_tables.php'), | ||
], 'migrations'); | ||
} | ||
|
||
/** | ||
* @param $migrationFileName | ||
* @return string | ||
* @throws BindingResolutionException | ||
*/ | ||
protected function getMigrationFileName($migrationFileName): string | ||
{ | ||
$timestamp = date('Y_m_d_His'); | ||
|
||
$filesystem = $this->app->make(Filesystem::class); | ||
|
||
return Collection::make($this->app->databasePath() . DIRECTORY_SEPARATOR .'migrations'.DIRECTORY_SEPARATOR) | ||
->flatMap(function ($path) use ($filesystem, $migrationFileName) { | ||
return $filesystem->glob($path.'*_'.$migrationFileName); | ||
}) | ||
->push($this->app->databasePath()."/migrations/{$timestamp}_{$migrationFileName}") | ||
->first(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Ogrre\ChatGPT\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\HasMany; | ||
|
||
use OpenAI; | ||
|
||
class Chat extends Model | ||
{ | ||
public mixed $client; | ||
|
||
public function __construct(array $attributes = []) | ||
{ | ||
parent::__construct($attributes); | ||
$this->client = OpenAI::client(config('openai.api_key')); | ||
} | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
protected $fillable = [ | ||
'id', | ||
]; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $table = 'chats'; | ||
|
||
/** | ||
* @return HasMany | ||
*/ | ||
public function messages(): HasMany | ||
{ | ||
return $this->hasMany(Message::class); | ||
} | ||
|
||
/** | ||
* @param string $prompt | ||
* @return $this | ||
*/ | ||
public function send(string $prompt): static | ||
{ | ||
$messages = $this->messages(); | ||
|
||
$user_message = new Message(); | ||
$user_message->fill([ | ||
"role" => "user", | ||
"content" => $prompt, | ||
]); | ||
$messages->save($user_message); | ||
|
||
$response = $this->client->chat()->create([ | ||
'model' => 'gpt-3.5-turbo', | ||
'messages' => $messages, | ||
]); | ||
|
||
$assistant_message = new Message(); | ||
$assistant_message->fill([ | ||
"role" => "assistant", | ||
"content" => $response->choices[0]->message->content, | ||
|
||
]); | ||
$messages->save($assistant_message); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Ogrre\ChatGPT\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
|
||
class Message extends Model | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
protected $fillable = [ | ||
'id', | ||
'role', | ||
'content', | ||
'chat_id', | ||
]; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $table = 'messages'; | ||
|
||
/** | ||
* @return BelongsTo | ||
*/ | ||
public function chat(): BelongsTo | ||
{ | ||
return $this->belongsTo(Chat::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Ogrre\ChatGPT\Traits; | ||
|
||
use Illuminate\Database\Eloquent\Relations\HasMany; | ||
use Ogrre\ChatGPT\Models\Chat; | ||
|
||
trait HasChat | ||
{ | ||
/** | ||
* @return HasMany | ||
*/ | ||
public function chats(): HasMany | ||
{ | ||
return $this->hasMany(Chat::class); | ||
} | ||
|
||
/** | ||
* @param string $prompt | ||
* @param $chat_id | ||
* @return Chat | ||
*/ | ||
public function chat(string $prompt, $chat_id = null): Chat | ||
{ | ||
$chat = $chat_id ? Chat::find($chat_id) : new Chat(); | ||
|
||
$this->chats()->attach([$chat->id]); | ||
|
||
return $chat->send($prompt); | ||
} | ||
} |