Skip to content

Commit

Permalink
fix src directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Baptiste Loup committed Jun 27, 2023
1 parent 943c3cf commit 1126228
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 292 deletions.
2 changes: 1 addition & 1 deletion src/ChatRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ChatRegistrar

public function __construct(){

$this->chatClass = config('chat.models.chat');
$this->chatClass = config('chatgpt.models.chat');
}

/**
Expand Down
60 changes: 42 additions & 18 deletions src/Models/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Ogrre\ChatGPT\Resources\ChatResource;

use OpenAI;

Expand All @@ -14,14 +15,16 @@ 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'));
}

/**
* @var string[]
*/
protected $fillable = [
'id',
'title',
'messages'
];

/**
Expand All @@ -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();
}
}
File renamed without changes.
39 changes: 30 additions & 9 deletions src/Traits/HasChat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
23 changes: 0 additions & 23 deletions src/src/ChatRegistrar.php

This file was deleted.

63 changes: 0 additions & 63 deletions src/src/ChatServiceProvider.php

This file was deleted.

94 changes: 0 additions & 94 deletions src/src/Models/Chat.php

This file was deleted.

32 changes: 0 additions & 32 deletions src/src/Models/Message.php

This file was deleted.

Loading

0 comments on commit 1126228

Please sign in to comment.