From 8a40ebdbc0d908adffd92076c7a06940224bf972 Mon Sep 17 00:00:00 2001 From: Alfred Nutile Date: Wed, 31 Jul 2024 08:06:21 -0400 Subject: [PATCH] working on tools to be dynamic --- Modules/LlmDriver/app/BaseClient.php | 2 +- .../app/Functions/GetWebSiteFromUrlTool.php | 15 ++------------- Modules/LlmDriver/app/OllamaClient.php | 4 +--- .../app/Responses/ClaudeCompletionResponse.php | 4 ++-- .../app/Responses/ClaudeContentCaster.php | 2 +- .../LlmDriver/app/Responses/ClaudeToolCaster.php | 4 ++-- .../app/Responses/CompletionResponse.php | 6 ++---- .../app/Responses/OllamaCompletionResponse.php | 6 +++--- Modules/LlmDriver/app/Responses/OllamaToolDto.php | 3 --- Modules/LlmDriver/app/Responses/ToolDto.php | 4 +--- .../LlmDriver/tests/Feature/OllamaClientTest.php | 3 ++- .../Orchestration/OrchestrateVersionTwo.php | 10 ++++------ app/Http/Controllers/SettingController.php | 6 +++++- app/Jobs/ToolJob.php | 8 ++------ app/Jobs/ToolsCompleteJob.php | 2 +- tests/Feature/ClaudeCompletionResponseTest.php | 5 ----- tests/Feature/OllamaCompletionResponseTest.php | 2 -- tests/Feature/OrchestrateVersionTwoTest.php | 7 ++----- 18 files changed, 31 insertions(+), 62 deletions(-) diff --git a/Modules/LlmDriver/app/BaseClient.php b/Modules/LlmDriver/app/BaseClient.php index 13d908fd..93f6a27a 100644 --- a/Modules/LlmDriver/app/BaseClient.php +++ b/Modules/LlmDriver/app/BaseClient.php @@ -4,7 +4,6 @@ use Illuminate\Support\Arr; use Illuminate\Support\Facades\Log; -use Laravel\Pennant\Feature; use LlmLaraHub\LlmDriver\Functions\FunctionDto; use LlmLaraHub\LlmDriver\Requests\MessageInDto; use LlmLaraHub\LlmDriver\Responses\CompletionResponse; @@ -39,6 +38,7 @@ public function modifyPayload(array $payload): array $payload = $this->addJsonFormat($payload); $payload['tools'] = $this->getFunctions(); + return $payload; } diff --git a/Modules/LlmDriver/app/Functions/GetWebSiteFromUrlTool.php b/Modules/LlmDriver/app/Functions/GetWebSiteFromUrlTool.php index e354249d..76c570b7 100644 --- a/Modules/LlmDriver/app/Functions/GetWebSiteFromUrlTool.php +++ b/Modules/LlmDriver/app/Functions/GetWebSiteFromUrlTool.php @@ -2,18 +2,8 @@ namespace LlmLaraHub\LlmDriver\Functions; -use App\Domains\Messages\RoleEnum; -use App\Domains\Reporting\ReportTypeEnum; -use App\Domains\Reporting\StatusEnum; -use Facades\App\Domains\Sources\WebSearch\GetPage; -use App\Jobs\GatherInfoFinalPromptJob; -use App\Jobs\GatherInfoReportSectionsJob; -use App\Models\Collection; use App\Models\Message; -use App\Models\Report; -use Facades\App\Domains\Tokenizer\Templatizer; -use Illuminate\Bus\Batch; -use Illuminate\Support\Facades\Bus; +use Facades\App\Domains\Sources\WebSearch\GetPage; use Illuminate\Support\Facades\Log; use LlmLaraHub\LlmDriver\Responses\FunctionResponse; use LlmLaraHub\LlmDriver\ToolsHelper; @@ -35,7 +25,7 @@ public function handle( $url = data_get($args, 'url', null); - if(!$url) { + if (! $url) { throw new \Exception('No url found'); } @@ -50,7 +40,6 @@ public function handle( ]); } - /** * @return PropertyDto[] */ diff --git a/Modules/LlmDriver/app/OllamaClient.php b/Modules/LlmDriver/app/OllamaClient.php index 6d9fd859..6179df22 100644 --- a/Modules/LlmDriver/app/OllamaClient.php +++ b/Modules/LlmDriver/app/OllamaClient.php @@ -103,11 +103,9 @@ public function chat(array $messages): CompletionResponse 'stream' => false, 'options' => [ 'temperature' => 0, - ] + ], ]; - - $payload = $this->modifyPayload($payload); Log::info('LlmDriver::Ollama::chat', [ 'payload' => $payload, diff --git a/Modules/LlmDriver/app/Responses/ClaudeCompletionResponse.php b/Modules/LlmDriver/app/Responses/ClaudeCompletionResponse.php index 3f504182..0697eb2b 100644 --- a/Modules/LlmDriver/app/Responses/ClaudeCompletionResponse.php +++ b/Modules/LlmDriver/app/Responses/ClaudeCompletionResponse.php @@ -11,8 +11,8 @@ class ClaudeCompletionResponse extends CompletionResponse public function __construct( #[WithCastable(ClaudeContentCaster::class)] public mixed $content, - public string $stop_reason = 'end_turn', - public ?string $tool_used = null, + public string $stop_reason, + public ?string $tool_used, /** @var array */ #[WithCastable(ClaudeToolCaster::class)] #[MapInputName('content')] diff --git a/Modules/LlmDriver/app/Responses/ClaudeContentCaster.php b/Modules/LlmDriver/app/Responses/ClaudeContentCaster.php index a6b40636..df9dab7a 100644 --- a/Modules/LlmDriver/app/Responses/ClaudeContentCaster.php +++ b/Modules/LlmDriver/app/Responses/ClaudeContentCaster.php @@ -2,7 +2,6 @@ namespace LlmLaraHub\LlmDriver\Responses; -use Pgvector\Laravel\Vector; use Spatie\LaravelData\Casts\Cast; use Spatie\LaravelData\Casts\Castable; use Spatie\LaravelData\Support\Creation\CreationContext; @@ -26,6 +25,7 @@ function ($item) { return $item['type'] === 'text'; } )->first(); + return data_get($results, 'text'); } }; diff --git a/Modules/LlmDriver/app/Responses/ClaudeToolCaster.php b/Modules/LlmDriver/app/Responses/ClaudeToolCaster.php index 74b074be..09658b26 100644 --- a/Modules/LlmDriver/app/Responses/ClaudeToolCaster.php +++ b/Modules/LlmDriver/app/Responses/ClaudeToolCaster.php @@ -2,7 +2,6 @@ namespace LlmLaraHub\LlmDriver\Responses; -use Pgvector\Laravel\Vector; use Spatie\LaravelData\Casts\Cast; use Spatie\LaravelData\Casts\Castable; use Spatie\LaravelData\Support\Creation\CreationContext; @@ -27,7 +26,7 @@ function ($item) { } )->toArray(); - foreach($results as $index => $result) { + foreach ($results as $index => $result) { $results[$index] = ToolDto::from( [ 'name' => $result['name'], @@ -36,6 +35,7 @@ function ($item) { ] ); } + return $results; } }; diff --git a/Modules/LlmDriver/app/Responses/CompletionResponse.php b/Modules/LlmDriver/app/Responses/CompletionResponse.php index 378c759f..71deba3b 100644 --- a/Modules/LlmDriver/app/Responses/CompletionResponse.php +++ b/Modules/LlmDriver/app/Responses/CompletionResponse.php @@ -2,16 +2,14 @@ namespace LlmLaraHub\LlmDriver\Responses; - -use Spatie\LaravelData\Attributes\MapInputName; use Spatie\LaravelData\Optional; class CompletionResponse extends \Spatie\LaravelData\Data { public function __construct( public mixed $content, - public string $stop_reason = 'end_turn', - public ?string $tool_used = null, + public string $stop_reason, + public ?string $tool_used, /** @var array */ public array|Optional $tool_calls, public ?int $input_tokens = null, diff --git a/Modules/LlmDriver/app/Responses/OllamaCompletionResponse.php b/Modules/LlmDriver/app/Responses/OllamaCompletionResponse.php index 0d4e0fbe..e97d2ac1 100644 --- a/Modules/LlmDriver/app/Responses/OllamaCompletionResponse.php +++ b/Modules/LlmDriver/app/Responses/OllamaCompletionResponse.php @@ -2,8 +2,8 @@ namespace LlmLaraHub\LlmDriver\Responses; -use Spatie\LaravelData\Optional; use Spatie\LaravelData\Attributes\MapInputName; +use Spatie\LaravelData\Optional; class OllamaCompletionResponse extends CompletionResponse { @@ -11,8 +11,8 @@ public function __construct( #[MapInputName('message.content')] public mixed $content, #[MapInputName('done_reason')] - public string $stop_reason = 'end_turn', - public ?string $tool_used = null, + public string $stop_reason, + public ?string $tool_used, /** @var array */ #[MapInputName('message.tool_calls')] public array|Optional $tool_calls, diff --git a/Modules/LlmDriver/app/Responses/OllamaToolDto.php b/Modules/LlmDriver/app/Responses/OllamaToolDto.php index f4f7604f..723a3c31 100644 --- a/Modules/LlmDriver/app/Responses/OllamaToolDto.php +++ b/Modules/LlmDriver/app/Responses/OllamaToolDto.php @@ -3,11 +3,9 @@ namespace LlmLaraHub\LlmDriver\Responses; use Spatie\LaravelData\Attributes\MapInputName; -use Spatie\LaravelData\Data; class OllamaToolDto extends ToolDto { - public function __construct( #[MapInputName('function.name')] public string $name, @@ -15,5 +13,4 @@ public function __construct( public array $arguments, ) { } - } diff --git a/Modules/LlmDriver/app/Responses/ToolDto.php b/Modules/LlmDriver/app/Responses/ToolDto.php index f5121211..1c55519a 100644 --- a/Modules/LlmDriver/app/Responses/ToolDto.php +++ b/Modules/LlmDriver/app/Responses/ToolDto.php @@ -6,12 +6,10 @@ class ToolDto extends Data { - public function __construct( public string $name, public array $arguments, - public string $id = "", + public string $id = '', ) { } - } diff --git a/Modules/LlmDriver/tests/Feature/OllamaClientTest.php b/Modules/LlmDriver/tests/Feature/OllamaClientTest.php index 5307885e..80ad16ed 100644 --- a/Modules/LlmDriver/tests/Feature/OllamaClientTest.php +++ b/Modules/LlmDriver/tests/Feature/OllamaClientTest.php @@ -98,7 +98,8 @@ public function test_chat(): void } - public function test_functions() { + public function test_functions() + { $client = new OllamaClient(); $functions = $client->getFunctions(); diff --git a/app/Domains/Orchestration/OrchestrateVersionTwo.php b/app/Domains/Orchestration/OrchestrateVersionTwo.php index e7b7881b..34c5a158 100644 --- a/app/Domains/Orchestration/OrchestrateVersionTwo.php +++ b/app/Domains/Orchestration/OrchestrateVersionTwo.php @@ -15,8 +15,6 @@ class OrchestrateVersionTwo { - - public function handle( Chat $chat, Message $message) @@ -98,16 +96,16 @@ public function handle( $response = LlmDriverFacade::driver($message->getDriver()) ->chat($messages); - if(!empty($response->tool_calls)) { + if (! empty($response->tool_calls)) { $jobs = []; Log::info('Orchestration V2 Tools Found', [ 'tool_calls' => collect($response->tool_calls) ->pluck('name')->toArray(), ]); - foreach($response->tool_calls as $tool_call) { + foreach ($response->tool_calls as $tool_call) { $message = $chat->addInput( - message: "Calling tools", + message: 'Calling tools', role: RoleEnum::Assistant, show_in_thread: false, meta_data: MetaDataDto::from([ @@ -123,7 +121,7 @@ public function handle( } Bus::batch([ - $jobs + $jobs, ])->name("Running tools for Chat {$message->getChat()->id} {$message->id}") ->finally(function (Batch $batch) use ($chat) { Bus::batch([ diff --git a/app/Http/Controllers/SettingController.php b/app/Http/Controllers/SettingController.php index 6fbd68af..201abe29 100644 --- a/app/Http/Controllers/SettingController.php +++ b/app/Http/Controllers/SettingController.php @@ -31,6 +31,7 @@ public function updateClaude(Request $request, Setting $setting) $setting->save(); $setting->updateStep($setting); $this->clearCache(); + return back(); } @@ -47,6 +48,7 @@ public function updateOllama(Request $request, Setting $setting) $setting->save(); $setting->updateStep($setting); $this->clearCache(); + return back(); } @@ -63,6 +65,7 @@ public function updateGroq(Request $request, Setting $setting) $setting->save(); $setting->updateStep($setting); $this->clearCache(); + return back(); } @@ -85,7 +88,8 @@ public function updateOpenAi(Request $request, Setting $setting) return back(); } - protected function clearCache() { + protected function clearCache() + { Artisan::call('optimize:clear'); } } diff --git a/app/Jobs/ToolJob.php b/app/Jobs/ToolJob.php index a0c1ee5e..e403f48e 100644 --- a/app/Jobs/ToolJob.php +++ b/app/Jobs/ToolJob.php @@ -2,7 +2,6 @@ namespace App\Jobs; -use App\Domains\Chat\MetaDataDto; use App\Domains\Chat\ToolsDto; use App\Domains\Messages\RoleEnum; use App\Models\Message; @@ -14,12 +13,11 @@ use Illuminate\Queue\SerializesModels; use LlmLaraHub\LlmDriver\Functions\FunctionContract; use LlmLaraHub\LlmDriver\Responses\FunctionResponse; -use LlmLaraHub\LlmDriver\Responses\ToolDto; class ToolJob implements ShouldQueue { - use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Batchable; + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; /** * Create a new job instance. @@ -27,8 +25,7 @@ class ToolJob implements ShouldQueue public function __construct( public FunctionContract $function, public Message $message - ) - { + ) { // } @@ -61,6 +58,5 @@ public function handle(): void * Should I do anything with the results of above FunctionResponse * Should I set the batch and if so how to best use it */ - } } diff --git a/app/Jobs/ToolsCompleteJob.php b/app/Jobs/ToolsCompleteJob.php index 0a7a80e3..70c5c9c8 100644 --- a/app/Jobs/ToolsCompleteJob.php +++ b/app/Jobs/ToolsCompleteJob.php @@ -15,8 +15,8 @@ class ToolsCompleteJob implements ShouldQueue { - use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Batchable; + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; /** * Create a new job instance. diff --git a/tests/Feature/ClaudeCompletionResponseTest.php b/tests/Feature/ClaudeCompletionResponseTest.php index e31516f5..086913c0 100644 --- a/tests/Feature/ClaudeCompletionResponseTest.php +++ b/tests/Feature/ClaudeCompletionResponseTest.php @@ -2,13 +2,8 @@ namespace Feature; -use Illuminate\Foundation\Testing\RefreshDatabase; -use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Support\Arr; use LlmLaraHub\LlmDriver\Responses\ClaudeCompletionResponse; -use LlmLaraHub\LlmDriver\Responses\ClaudeToolDto; -use LlmLaraHub\LlmDriver\Responses\OllamaCompletionResponse; -use LlmLaraHub\LlmDriver\Responses\OllamaToolDto; use LlmLaraHub\LlmDriver\Responses\ToolDto; use Tests\TestCase; diff --git a/tests/Feature/OllamaCompletionResponseTest.php b/tests/Feature/OllamaCompletionResponseTest.php index 3d3804a4..1271f972 100644 --- a/tests/Feature/OllamaCompletionResponseTest.php +++ b/tests/Feature/OllamaCompletionResponseTest.php @@ -2,8 +2,6 @@ namespace Tests\Feature; -use Illuminate\Foundation\Testing\RefreshDatabase; -use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Support\Arr; use LlmLaraHub\LlmDriver\Responses\OllamaCompletionResponse; use LlmLaraHub\LlmDriver\Responses\OllamaToolDto; diff --git a/tests/Feature/OrchestrateVersionTwoTest.php b/tests/Feature/OrchestrateVersionTwoTest.php index ddf21167..5ab730c7 100644 --- a/tests/Feature/OrchestrateVersionTwoTest.php +++ b/tests/Feature/OrchestrateVersionTwoTest.php @@ -5,8 +5,6 @@ use App\Models\Chat; use App\Models\Collection; use App\Models\Message; -use Illuminate\Foundation\Testing\RefreshDatabase; -use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Support\Facades\Bus; use LlmLaraHub\LlmDriver\LlmDriverFacade; use LlmLaraHub\LlmDriver\Responses\OllamaCompletionResponse; @@ -20,11 +18,11 @@ class OrchestrateVersionTwoTest extends TestCase public function test_example(): void { Bus::fake(); - $data = get_fixture("ollama_response_tools.json"); + $data = get_fixture('ollama_response_tools.json'); LlmDriverFacade::shouldReceive('driver->chat')->once()->andReturn( OllamaCompletionResponse::from($data) ); - $prompt = <<