Skip to content

Commit

Permalink
hmm
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Aug 27, 2023
1 parent 123e9f9 commit 804f362
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions app/Domains/Message/MessageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions app/Domains/Scheduling/TaskRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,36 @@
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([
'due' => ($task->date) ? Carbon::parse($task->date) : null,
'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);
}
}
14 changes: 8 additions & 6 deletions app/Jobs/MessageCreatedJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
1 change: 1 addition & 0 deletions app/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 3 additions & 10 deletions app/OpenAi/ChatClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(
[
Expand Down
1 change: 0 additions & 1 deletion app/OpenAi/FunctionCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions database/factories/MessageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function definition(): array
'content' => fake()->sentence(4, true),
'user_id' => User::factory(),
'parent_id' => null,
'run_functions' => true
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('messages', function (Blueprint $table) {
$table->boolean('run_functions')->default(1);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('messages', function (Blueprint $table) {
//
});
}
};
8 changes: 7 additions & 1 deletion tests/Feature/MessageRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 804f362

Please sign in to comment.