diff --git a/src/ChatRegistrar.php b/src/ChatRegistrar.php index 76dac18..9e38665 100644 --- a/src/ChatRegistrar.php +++ b/src/ChatRegistrar.php @@ -10,7 +10,7 @@ class ChatRegistrar public function __construct(){ - $this->chatClass = config('chat.models.chat'); + $this->chatClass = config('chatgpt.models.chat'); } /** diff --git a/src/Models/Chat.php b/src/Models/Chat.php index 4b6a46e..a8a4ab8 100644 --- a/src/Models/Chat.php +++ b/src/Models/Chat.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; +use Ogrre\ChatGPT\Resources\ChatResource; use OpenAI; @@ -14,7 +15,7 @@ class Chat extends Model public function __construct(array $attributes = []) { parent::__construct($attributes); - $this->client = OpenAI::client(config('openai.api_key')); + $this->client = OpenAI::client(config('chatgpt.secrets.api_key')); } /** @@ -22,6 +23,8 @@ public function __construct(array $attributes = []) */ protected $fillable = [ 'id', + 'title', + 'messages' ]; /** @@ -38,33 +41,54 @@ public function messages(): HasMany } /** - * @param string $prompt - * @return $this + * @return mixed */ - public function send(string $prompt): static + public function list_messages() { + return $this->messages->map(function($message) { + return ['role' => $message->role, 'content' => $message->content]; + })->all(); + } + + /** + * @param string $content + * @param string $role + * @return void + */ + private function newMessage(string $content, string $role){ $messages = $this->messages(); - $user_message = new Message(); - $user_message->fill([ - "role" => "user", - "content" => $prompt, + $message = new Message(); + $message->fill([ + "role" => $role, + "content" => $content, ]); - $messages->save($user_message); + $messages->save($message); + } + + /** + * @return array + */ + public function display() + { + return ["chat" => Chat::find($this->id), "messages" => Chat::find($this->id)->messages]; + } + + /** + * @param string $prompt + * @return array + */ + public function gpt(string $prompt) + { + $this->newMessage($prompt, "user"); $response = $this->client->chat()->create([ 'model' => 'gpt-3.5-turbo', - 'messages' => $messages, + 'messages' => $this->list_messages(), ]); - $assistant_message = new Message(); - $assistant_message->fill([ - "role" => "assistant", - "content" => $response->choices[0]->message->content, - - ]); - $messages->save($assistant_message); + $this->newMessage($response->choices[0]->message->content, "assistant"); - return $this; + return self::display(); } } diff --git a/src/src/Resources/ChatResource.php b/src/Resources/ChatResource.php similarity index 100% rename from src/src/Resources/ChatResource.php rename to src/Resources/ChatResource.php diff --git a/src/Traits/HasChat.php b/src/Traits/HasChat.php index bca863a..a576987 100644 --- a/src/Traits/HasChat.php +++ b/src/Traits/HasChat.php @@ -3,29 +3,50 @@ namespace Ogrre\ChatGPT\Traits; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\MorphToMany; +use Ogrre\ChatGPT\Resources\ChatResource; +use Ogrre\ChatGPT\ChatRegistrar; +use Ogrre\ChatGPT\Models\Message; use Ogrre\ChatGPT\Models\Chat; trait HasChat { /** - * @return HasMany + * @return MorphToMany */ - public function chats(): HasMany + public function chats(): MorphToMany { - return $this->hasMany(Chat::class); + return $this->morphToMany(Chat::class, "model", "model_has_chats"); } /** - * @param string $prompt - * @param $chat_id * @return Chat */ - public function chat(string $prompt, $chat_id = null): Chat + public function newChat(string $title = null, string $role = null): Chat { - $chat = $chat_id ? Chat::find($chat_id) : new Chat(); + $chat = Chat::create(["title" => $title ?? "New Chat"]); + $this->chats()->syncWithoutDetaching($chat); + + $first_message = new Message(); + $first_message->fill([ + "role" => "system", + "content" => $role ?? "You are a helpful assistant" + ]); + + $chat->messages()->save($first_message); + + return $chat; + } - $this->chats()->attach([$chat->id]); + /** + * @param string $prompt + * @param Chat $chat + * @return ChatResource + */ + public function chatgpt(string $prompt, Chat $chat) + { + $chat->gpt($prompt); - return $chat->send($prompt); + return $chat->display(); } } diff --git a/src/src/ChatRegistrar.php b/src/src/ChatRegistrar.php deleted file mode 100644 index 9e38665..0000000 --- a/src/src/ChatRegistrar.php +++ /dev/null @@ -1,23 +0,0 @@ -chatClass = config('chatgpt.models.chat'); - } - - /** - * @return Chat - */ - public function getChatClass(): Chat - { - return app($this->chatClass); - } -} diff --git a/src/src/ChatServiceProvider.php b/src/src/ChatServiceProvider.php deleted file mode 100644 index 7f45779..0000000 --- a/src/src/ChatServiceProvider.php +++ /dev/null @@ -1,63 +0,0 @@ -mergeConfigFrom( - __DIR__.'/../config/chatgpt.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/chatgpt.php' => config_path('chatgpt.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(); - } -} diff --git a/src/src/Models/Chat.php b/src/src/Models/Chat.php deleted file mode 100644 index a8a4ab8..0000000 --- a/src/src/Models/Chat.php +++ /dev/null @@ -1,94 +0,0 @@ -client = OpenAI::client(config('chatgpt.secrets.api_key')); - } - - /** - * @var string[] - */ - protected $fillable = [ - 'id', - 'title', - 'messages' - ]; - - /** - * @var string - */ - protected $table = 'chats'; - - /** - * @return HasMany - */ - public function messages(): HasMany - { - return $this->hasMany(Message::class); - } - - /** - * @return mixed - */ - public function list_messages() - { - return $this->messages->map(function($message) { - return ['role' => $message->role, 'content' => $message->content]; - })->all(); - } - - /** - * @param string $content - * @param string $role - * @return void - */ - private function newMessage(string $content, string $role){ - $messages = $this->messages(); - - $message = new Message(); - $message->fill([ - "role" => $role, - "content" => $content, - ]); - $messages->save($message); - } - - /** - * @return array - */ - public function display() - { - return ["chat" => Chat::find($this->id), "messages" => Chat::find($this->id)->messages]; - } - - /** - * @param string $prompt - * @return array - */ - public function gpt(string $prompt) - { - $this->newMessage($prompt, "user"); - - $response = $this->client->chat()->create([ - 'model' => 'gpt-3.5-turbo', - 'messages' => $this->list_messages(), - ]); - - $this->newMessage($response->choices[0]->message->content, "assistant"); - - return self::display(); - } -} diff --git a/src/src/Models/Message.php b/src/src/Models/Message.php deleted file mode 100644 index f495c8c..0000000 --- a/src/src/Models/Message.php +++ /dev/null @@ -1,32 +0,0 @@ -belongsTo(Chat::class); - } -} diff --git a/src/src/Traits/HasChat.php b/src/src/Traits/HasChat.php deleted file mode 100644 index a576987..0000000 --- a/src/src/Traits/HasChat.php +++ /dev/null @@ -1,52 +0,0 @@ -morphToMany(Chat::class, "model", "model_has_chats"); - } - - /** - * @return Chat - */ - public function newChat(string $title = null, string $role = null): Chat - { - $chat = Chat::create(["title" => $title ?? "New Chat"]); - $this->chats()->syncWithoutDetaching($chat); - - $first_message = new Message(); - $first_message->fill([ - "role" => "system", - "content" => $role ?? "You are a helpful assistant" - ]); - - $chat->messages()->save($first_message); - - return $chat; - } - - /** - * @param string $prompt - * @param Chat $chat - * @return ChatResource - */ - public function chatgpt(string $prompt, Chat $chat) - { - $chat->gpt($prompt); - - return $chat->display(); - } -}