From 804f362bef4f2dca15be8dced4b84c63baabbde8 Mon Sep 17 00:00:00 2001 From: Alfred Nutile <365385+alnutile@users.noreply.github.com> Date: Sun, 27 Aug 2023 13:54:31 -0400 Subject: [PATCH] hmm --- .../GetContentFromUrl/GetContentFromUrl.php | 1 + app/Domains/Message/MessageRepository.php | 6 ++-- app/Domains/Scheduling/TaskRepository.php | 13 +++++++++ app/Jobs/MessageCreatedJob.php | 14 ++++++---- app/Models/Message.php | 1 + app/OpenAi/ChatClient.php | 13 ++------- app/OpenAi/FunctionCall.php | 1 - database/factories/MessageFactory.php | 1 + ...34_add_run_ai_option_to_messages_table.php | 28 +++++++++++++++++++ tests/Feature/MessageRepositoryTest.php | 8 +++++- 10 files changed, 65 insertions(+), 21 deletions(-) create mode 100644 database/migrations/2023_08_27_173334_add_run_ai_option_to_messages_table.php diff --git a/app/Domains/LlmFunctions/GetContentFromUrl/GetContentFromUrl.php b/app/Domains/LlmFunctions/GetContentFromUrl/GetContentFromUrl.php index ac69352..56897e0 100644 --- a/app/Domains/LlmFunctions/GetContentFromUrl/GetContentFromUrl.php +++ b/app/Domains/LlmFunctions/GetContentFromUrl/GetContentFromUrl.php @@ -33,6 +33,7 @@ public function handle(FunctionCallDto $functionCallDto): Message ); $functionCallDto->message->content = $content; + $functionCallDto->message->run_functions = false; //just to prevent loops etc $functionCallDto->message->updateQuietly(); return $functionCallDto->message->refresh(); diff --git a/app/Domains/Message/MessageRepository.php b/app/Domains/Message/MessageRepository.php index ca50488..cfb4bbe 100644 --- a/app/Domains/Message/MessageRepository.php +++ b/app/Domains/Message/MessageRepository.php @@ -24,16 +24,16 @@ public function __construct(MessageBuilder $messageBuilder = null) $this->messageBuilder = $messageBuilder; } - public function handle(Message $message): Response + public function handle(Message $message): ?Response { $this->parent_message = $message; $this->messageBuilder->setMessages($this->createPrompt()); return ChatClient::setMessage($message) - ->chat($this->messageBuilder->getMessagesLimitTokenCount( + ->chat(messages: $this->messageBuilder->getMessagesLimitTokenCount( remove_token_count: true - )); + ), run_functions: $message->run_functions); } protected function createPrompt(): MessagesDto diff --git a/app/Domains/Scheduling/TaskRepository.php b/app/Domains/Scheduling/TaskRepository.php index 2a5a0df..b50615a 100644 --- a/app/Domains/Scheduling/TaskRepository.php +++ b/app/Domains/Scheduling/TaskRepository.php @@ -3,14 +3,17 @@ namespace App\Domains\Scheduling; use App\Domains\Scheduling\Dtos\TasksDto; +use App\Jobs\MessageCreatedJob; use App\Models\Message; use App\Models\Task; +use App\OpenAi\ChatClient; use Carbon\Carbon; class TaskRepository { public function handle(TasksDto $tasksDto, Message $message): void { + $summary = []; foreach ($tasksDto->tasks as $task) { if (! Task::where('description', $task->description)->where('message_id', $message->id)->exists()) { Task::create([ @@ -18,8 +21,18 @@ public function handle(TasksDto $tasksDto, Message $message): void 'description' => $task->description, 'message_id' => $message->id, ]); + + $summary[] = $task->description . ' on ' . $task->date; } } + $summary = implode("\n", $summary); + + $summary = sprintf("The following tasks have been created %s\n\n", $summary); + + $message->content = str($message->content)->prepend($summary); +// $message->run_functions = false;//prevent a loop + $message->updateQuietly(); +// MessageCreatedJob::dispatchSync($message); } } diff --git a/app/Jobs/MessageCreatedJob.php b/app/Jobs/MessageCreatedJob.php index 4e4353b..5ef1aac 100644 --- a/app/Jobs/MessageCreatedJob.php +++ b/app/Jobs/MessageCreatedJob.php @@ -34,12 +34,14 @@ public function handle(): void MessageStatusEvent::dispatch($this->message); /** @var Response $results */ $results = MessageRepository::handle($this->message); - Message::create([ - 'user_id' => $this->message->user_id, - 'content' => $results->content, - 'role' => 'assistant', - 'parent_id' => $this->message->id, - ]); + if($results != null) { + Message::create([ + 'user_id' => $this->message->user_id, + 'content' => $results->content, + 'role' => 'assistant', + 'parent_id' => $this->message->id, + ]); + } MessageStatusEvent::dispatch($this->message); } catch (\Exception $e) { logger('Error getting results'); diff --git a/app/Models/Message.php b/app/Models/Message.php index 19ddbbf..1cb08a0 100644 --- a/app/Models/Message.php +++ b/app/Models/Message.php @@ -13,6 +13,7 @@ * @property int $id * @property string $role * @property int $parent_id + * @property boolean $run_functions * @property int $user_id * @property string $content * @property array $embedding diff --git a/app/OpenAi/ChatClient.php b/app/OpenAi/ChatClient.php index d975aa8..0117ce9 100644 --- a/app/OpenAi/ChatClient.php +++ b/app/OpenAi/ChatClient.php @@ -21,8 +21,9 @@ public function setMessage(Message $message): self return $this; } - public function chat(array $messages, bool $run_functions = true): Response + public function chat(array $messages, bool $run_functions = true): ?Response { + if (config('openai.mock') && ! app()->environment('testing')) { logger('Mocking'); sleep(2); @@ -68,15 +69,7 @@ public function chat(array $messages, bool $run_functions = true): Response FunctionCall::handle($name, $dto); - $messages[] = [ - 'role' => 'assistant', - 'content' => sprintf('As an assistant I ran the function %s for you with these parameters %s', - $name, - json_encode($dto->arguments) - ), - ]; - - return ChatClientFacade::chat($messages, false); + return null; } else { return Response::from( [ diff --git a/app/OpenAi/FunctionCall.php b/app/OpenAi/FunctionCall.php index 9f583ca..cde93eb 100644 --- a/app/OpenAi/FunctionCall.php +++ b/app/OpenAi/FunctionCall.php @@ -13,7 +13,6 @@ public function handle(string $function_name, FunctionCallDto $callDto): void try { $function = LlmFunction::where('label', $function_name)->firstOrfail(); $results = $function_name($callDto); - } catch (ModelNotFoundException $exception) { logger('The function does not exist '.$function_name); } diff --git a/database/factories/MessageFactory.php b/database/factories/MessageFactory.php index 0177d90..2a905be 100644 --- a/database/factories/MessageFactory.php +++ b/database/factories/MessageFactory.php @@ -25,6 +25,7 @@ public function definition(): array 'content' => fake()->sentence(4, true), 'user_id' => User::factory(), 'parent_id' => null, + 'run_functions' => true ]; } diff --git a/database/migrations/2023_08_27_173334_add_run_ai_option_to_messages_table.php b/database/migrations/2023_08_27_173334_add_run_ai_option_to_messages_table.php new file mode 100644 index 0000000..c75b73f --- /dev/null +++ b/database/migrations/2023_08_27_173334_add_run_ai_option_to_messages_table.php @@ -0,0 +1,28 @@ +boolean('run_functions')->default(1); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('messages', function (Blueprint $table) { + // + }); + } +}; diff --git a/tests/Feature/MessageRepositoryTest.php b/tests/Feature/MessageRepositoryTest.php index f28194e..3b58ec5 100644 --- a/tests/Feature/MessageRepositoryTest.php +++ b/tests/Feature/MessageRepositoryTest.php @@ -14,7 +14,13 @@ class MessageRepositoryTest extends TestCase public function test_makes_request() { - ChatClient::shouldReceive('setMessage->chat')->once(); + $response = Response::from([ + 'content' => "Foo" + ]); + ChatClient::shouldReceive('setMessage->chat')->once()->andReturn( + $response + ); + $message = Message::factory()->create([ 'role' => 'user']); $meta_data1 = MetaData::factory()->create();