diff --git a/Modules/LlmDriver/app/BaseClient.php b/Modules/LlmDriver/app/BaseClient.php index 0b4156cf..604b4ee8 100644 --- a/Modules/LlmDriver/app/BaseClient.php +++ b/Modules/LlmDriver/app/BaseClient.php @@ -121,12 +121,10 @@ public function modifyPayload(array $payload, bool $noTools = false): array $payload['tools'] = $this->getFunctions(); } - if ($this->system) { $payload['system'] = $this->system; } - $payload = $this->addJsonFormat($payload); return $payload; diff --git a/Modules/LlmDriver/app/ClaudeClient.php b/Modules/LlmDriver/app/ClaudeClient.php index 8b05cd4f..3e234b4c 100644 --- a/Modules/LlmDriver/app/ClaudeClient.php +++ b/Modules/LlmDriver/app/ClaudeClient.php @@ -357,20 +357,20 @@ public function remapMessages(array $messages, bool $userLast = false): array return true; }) ->transform(function (MessageInDto $item) { - /** - * @NOTE - * Claude does not like to end a certain way - */ - $item->content = str( - cleanString($item->content) - )->replaceEnd("\n", '')->trim()->toString(); - - if (empty($item->content)) { - $item->content = 'See content in thread.'; - } + /** + * @NOTE + * Claude does not like to end a certain way + */ + $item->content = str( + cleanString($item->content) + )->replaceEnd("\n", '')->trim()->toString(); + + if (empty($item->content)) { + $item->content = 'See content in thread.'; + } - return $item->toArray(); - }); + return $item->toArray(); + }); /** * Claude needs me to not use the role tool diff --git a/Modules/LlmDriver/app/Functions/CreateTasksTool.php b/Modules/LlmDriver/app/Functions/CreateTasksTool.php index 0137aea8..127b7fda 100644 --- a/Modules/LlmDriver/app/Functions/CreateTasksTool.php +++ b/Modules/LlmDriver/app/Functions/CreateTasksTool.php @@ -3,13 +3,10 @@ namespace LlmLaraHub\LlmDriver\Functions; use App\Models\Message; -use App\Models\Project; use App\Models\Task; use App\Models\User; -use Facades\App\Domains\Sources\WebSearch\GetPage; use Illuminate\Support\Facades\Log; use LlmLaraHub\LlmDriver\Responses\FunctionResponse; -use LlmLaraHub\LlmDriver\ToolsHelper; class CreateTasksTool extends FunctionContract { @@ -26,7 +23,6 @@ class CreateTasksTool extends FunctionContract ToolTypes::ChatCompletion, ]; - public function handle( Message $message): FunctionResponse { @@ -50,7 +46,7 @@ public function handle( 'details' => $details, 'due_date' => $due_date, 'assistant' => $assistant, - 'user_id' => ($user_id !== "" && User::whereId($user_id)->exists()) ? $user_id : null, + 'user_id' => ($user_id !== '' && User::whereId($user_id)->exists()) ? $user_id : null, ]); } diff --git a/Modules/LlmDriver/tests/Feature/CreateTasksToolTest.php b/Modules/LlmDriver/tests/Feature/CreateTasksToolTest.php index 23707cb8..6fbb31be 100644 --- a/Modules/LlmDriver/tests/Feature/CreateTasksToolTest.php +++ b/Modules/LlmDriver/tests/Feature/CreateTasksToolTest.php @@ -2,19 +2,10 @@ namespace LlmLaraHub\LlmDriver\Tests\Feature; -use App\Domains\Chat\DateRangesEnum; -use App\Domains\Chat\MetaDataDto; use App\Models\Chat; -use App\Models\Document; -use App\Models\DocumentChunk; -use App\Models\Filter; use App\Models\Message; use App\Models\Project; -use Illuminate\Support\Facades\File; -use LlmLaraHub\LlmDriver\DistanceQuery\DistanceQueryFacade; -use LlmLaraHub\LlmDriver\DistanceQuery\Drivers\PostGres; use LlmLaraHub\LlmDriver\Functions\CreateTasksTool; -use Pgvector\Laravel\Vector; use Tests\TestCase; class CreateTasksToolTest extends TestCase @@ -38,7 +29,7 @@ public function test_generates_tasks(): void 'chat_id' => $chat->id, 'args' => [ 'tasks' => $data, - ] + ], ]); $this->assertDatabaseCount('tasks', 0); diff --git a/app/Domains/Projects/KickOffProject.php b/app/Domains/Projects/KickOffProject.php index 633c9589..7d060ca3 100644 --- a/app/Domains/Projects/KickOffProject.php +++ b/app/Domains/Projects/KickOffProject.php @@ -10,7 +10,7 @@ class KickOffProject { public function handle(Project $project) { - $chat = $project->chats?->first(); + $chat = $project->chats()->first(); $chat->updateQuietly([ 'chat_status' => UiStatusEnum::InProgress, diff --git a/app/Domains/Projects/Orchestrate.php b/app/Domains/Projects/Orchestrate.php index f85d7604..940d5c80 100644 --- a/app/Domains/Projects/Orchestrate.php +++ b/app/Domains/Projects/Orchestrate.php @@ -4,7 +4,6 @@ use App\Domains\Messages\RoleEnum; use App\Models\Chat; -use App\Models\Project; use Illuminate\Support\Facades\Log; use LlmLaraHub\LlmDriver\Functions\ToolTypes; use LlmLaraHub\LlmDriver\LlmDriverFacade; diff --git a/app/Domains/Projects/Prompts/CampaignPromptTemplate.php b/app/Domains/Projects/Prompts/CampaignPromptTemplate.php index 12124dfd..7edf2a35 100644 --- a/app/Domains/Projects/Prompts/CampaignPromptTemplate.php +++ b/app/Domains/Projects/Prompts/CampaignPromptTemplate.php @@ -4,8 +4,8 @@ class CampaignPromptTemplate { - - public static function systemPrompt(): string { + public static function systemPrompt(): string + { $now = now()->toISOString(); return << [ [ - 'key' => "Campaign Template", + 'key' => 'Campaign Template', 'content' => CampaignPromptTemplate::prompt(), 'system_prompt' => CampaignPromptTemplate::systemPrompt(), - ] + ], ], - 'statuses' => StatusEnum::selectOptions() + 'statuses' => StatusEnum::selectOptions(), ]); } @@ -87,7 +86,8 @@ public function show(Project $project) ]); } - public function showWithChat(Project $project, Chat$chat) { + public function showWithChat(Project $project, Chat $chat) + { return inertia('Projects/Show', [ 'project' => new ProjectResourceShow($project), diff --git a/app/Http/Resources/MessageResource.php b/app/Http/Resources/MessageResource.php index 078b18a1..3b7f998c 100644 --- a/app/Http/Resources/MessageResource.php +++ b/app/Http/Resources/MessageResource.php @@ -15,9 +15,10 @@ class MessageResource extends JsonResource public function toArray(Request $request): array { $user = []; - if($this->user_id) { + if ($this->user_id) { $user = new UserResource($this->user); } + return [ 'id' => $this->id, 'role' => $this->role->value, diff --git a/app/Models/Chat.php b/app/Models/Chat.php index 4a34e7e4..14260dc1 100644 --- a/app/Models/Chat.php +++ b/app/Models/Chat.php @@ -37,7 +37,7 @@ class Chat extends Model implements HasDrivers public function getDriver(): string { - if($this->chat_driver) { + if ($this->chat_driver) { return $this->chat_driver->value; } @@ -71,9 +71,10 @@ public function getChatable(): HasDrivers public function getEmbeddingDriver(): string { - if($this->embedding_driver) { + if ($this->embedding_driver) { return $this->embedding_driver->value; } + return $this->chatable->getEmbeddingDriver(); } @@ -144,7 +145,7 @@ public function addInputWithTools( string $message, mixed $tool_id, mixed $tool_name, - mixed $tool_args) : Message + mixed $tool_args): Message { return DB::transaction(function () use ($message, $tool_id, $tool_name, $tool_args) { @@ -192,7 +193,8 @@ public function getAiResponse($input) return $this->getChatResponse(); } - public function getMessageThread(int $limit = 5): array { + public function getMessageThread(int $limit = 5): array + { return $this->getChatResponse($limit); } diff --git a/app/Models/Message.php b/app/Models/Message.php index a7647e0d..46172082 100644 --- a/app/Models/Message.php +++ b/app/Models/Message.php @@ -59,18 +59,16 @@ public function getFromAiAttribute(): bool return $this->role !== RoleEnum::User; } - public function user() : BelongsTo + public function user(): BelongsTo { return $this->belongsTo(User::class); } - public function scopeNotSystem(Builder $query) { return $query->where('role', '!=', RoleEnum::System->value); } - public function scopeNotTool(Builder $query) { return $query->where('role', '!=', RoleEnum::Tool->value); diff --git a/app/Models/Project.php b/app/Models/Project.php index 9fc7ce9c..4d8c3c67 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -10,7 +10,6 @@ use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\SoftDeletes; use LlmLaraHub\LlmDriver\HasDrivers; -use LlmLaraHub\LlmDriver\HasDriversTrait; class Project extends Model implements HasDrivers { @@ -34,7 +33,6 @@ public function tasks(): HasMany return $this->hasMany(Task::class); } - public function chats(): MorphMany { return $this->morphMany(Chat::class, 'chatable'); diff --git a/database/migrations/2024_09_08_233618_add_status_to_chat.php b/database/migrations/2024_09_08_233618_add_status_to_chat.php index c6b80341..f630ce5c 100644 --- a/database/migrations/2024_09_08_233618_add_status_to_chat.php +++ b/database/migrations/2024_09_08_233618_add_status_to_chat.php @@ -12,7 +12,7 @@ public function up(): void { Schema::table('chats', function (Blueprint $table) { - $table->string("chat_status") + $table->string('chat_status') ->nullable() ->default(\App\Domains\Chat\UiStatusEnum::NotStarted->value); }); diff --git a/tests/Feature/Http/Controllers/ProjectControllerTest.php b/tests/Feature/Http/Controllers/ProjectControllerTest.php index 11be76b6..e5878e43 100644 --- a/tests/Feature/Http/Controllers/ProjectControllerTest.php +++ b/tests/Feature/Http/Controllers/ProjectControllerTest.php @@ -130,7 +130,7 @@ public function test_update(): void $this->actingAs($user)->put( route('projects.update', $project), [ - "name" => "Test Campaign 2", + 'name' => 'Test Campaign 2', 'start_date' => '2023-01-01', 'system_prompt' => 'Test Description', 'end_date' => '2023-01-01', @@ -154,5 +154,4 @@ public function test_destroy(): void )->assertSessionHasNoErrors() ->assertStatus(302); } - } diff --git a/tests/Feature/Models/ChatTest.php b/tests/Feature/Models/ChatTest.php index 3f314d67..fd8602f1 100644 --- a/tests/Feature/Models/ChatTest.php +++ b/tests/Feature/Models/ChatTest.php @@ -36,14 +36,15 @@ public function test_factory(): void $this->assertNotNull($collection->chats()->first()->id); } - public function test_default_driver() { + public function test_default_driver() + { $project = Project::factory()->create(); $chat = Chat::factory()->create([ 'chatable_id' => $project->id, - "chatable_type" => Project::class, - "chat_driver" => DriversEnum::Claude, - "embedding_driver" => DriversEnum::Ollama, + 'chatable_type' => Project::class, + 'chat_driver' => DriversEnum::Claude, + 'embedding_driver' => DriversEnum::Ollama, ]); $this->assertEquals(DriversEnum::Claude->value, $chat->getDriver()); diff --git a/tests/Feature/ProjectOrchestrateTest.php b/tests/Feature/ProjectOrchestrateTest.php index 40042129..dfb6c6ab 100644 --- a/tests/Feature/ProjectOrchestrateTest.php +++ b/tests/Feature/ProjectOrchestrateTest.php @@ -37,14 +37,13 @@ public function test_tools(): void CompletionResponse::from($response) ); - LlmDriverFacade::shouldReceive('driver->chat') ->once() ->andReturn( CompletionResponse::from($response) ); - (new Orchestrate())->handle($chat, 'Test Prompt', "System Prompt"); + (new Orchestrate())->handle($chat, 'Test Prompt', 'System Prompt'); $this->assertDatabaseCount('messages', 4); $this->assertDatabaseCount('tasks', 5);