Skip to content

Commit

Permalink
working on tools to be dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Jul 31, 2024
1 parent bdb7c52 commit 8a40ebd
Show file tree
Hide file tree
Showing 18 changed files with 31 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Modules/LlmDriver/app/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,6 +38,7 @@ public function modifyPayload(array $payload): array
$payload = $this->addJsonFormat($payload);

$payload['tools'] = $this->getFunctions();

return $payload;
}

Expand Down
15 changes: 2 additions & 13 deletions Modules/LlmDriver/app/Functions/GetWebSiteFromUrlTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,7 +25,7 @@ public function handle(

$url = data_get($args, 'url', null);

if(!$url) {
if (! $url) {
throw new \Exception('No url found');
}

Expand All @@ -50,7 +40,6 @@ public function handle(
]);
}


/**
* @return PropertyDto[]
*/
Expand Down
4 changes: 1 addition & 3 deletions Modules/LlmDriver/app/OllamaClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions Modules/LlmDriver/app/Responses/ClaudeCompletionResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<ToolDto> */
#[WithCastable(ClaudeToolCaster::class)]
#[MapInputName('content')]
Expand Down
2 changes: 1 addition & 1 deletion Modules/LlmDriver/app/Responses/ClaudeContentCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +25,7 @@ function ($item) {
return $item['type'] === 'text';
}
)->first();

return data_get($results, 'text');
}
};
Expand Down
4 changes: 2 additions & 2 deletions Modules/LlmDriver/app/Responses/ClaudeToolCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +26,7 @@ function ($item) {
}
)->toArray();

foreach($results as $index => $result) {
foreach ($results as $index => $result) {
$results[$index] = ToolDto::from(
[
'name' => $result['name'],
Expand All @@ -36,6 +35,7 @@ function ($item) {
]
);
}

return $results;
}
};
Expand Down
6 changes: 2 additions & 4 deletions Modules/LlmDriver/app/Responses/CompletionResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<ToolDto> */
public array|Optional $tool_calls,
public ?int $input_tokens = null,
Expand Down
6 changes: 3 additions & 3 deletions Modules/LlmDriver/app/Responses/OllamaCompletionResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace LlmLaraHub\LlmDriver\Responses;

use Spatie\LaravelData\Optional;
use Spatie\LaravelData\Attributes\MapInputName;
use Spatie\LaravelData\Optional;

class OllamaCompletionResponse extends CompletionResponse
{
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<OllamaToolDto> */
#[MapInputName('message.tool_calls')]
public array|Optional $tool_calls,
Expand Down
3 changes: 0 additions & 3 deletions Modules/LlmDriver/app/Responses/OllamaToolDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
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,
#[MapInputName('function.arguments')]
public array $arguments,
) {
}

}
4 changes: 1 addition & 3 deletions Modules/LlmDriver/app/Responses/ToolDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

class ToolDto extends Data
{

public function __construct(
public string $name,
public array $arguments,
public string $id = "",
public string $id = '',
) {
}

}
3 changes: 2 additions & 1 deletion Modules/LlmDriver/tests/Feature/OllamaClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public function test_chat(): void

}

public function test_functions() {
public function test_functions()
{
$client = new OllamaClient();
$functions = $client->getFunctions();

Expand Down
10 changes: 4 additions & 6 deletions app/Domains/Orchestration/OrchestrateVersionTwo.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

class OrchestrateVersionTwo
{


public function handle(
Chat $chat,
Message $message)
Expand Down Expand Up @@ -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([
Expand All @@ -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([
Expand Down
6 changes: 5 additions & 1 deletion app/Http/Controllers/SettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function updateClaude(Request $request, Setting $setting)
$setting->save();
$setting->updateStep($setting);
$this->clearCache();

return back();
}

Expand All @@ -47,6 +48,7 @@ public function updateOllama(Request $request, Setting $setting)
$setting->save();
$setting->updateStep($setting);
$this->clearCache();

return back();
}

Expand All @@ -63,6 +65,7 @@ public function updateGroq(Request $request, Setting $setting)
$setting->save();
$setting->updateStep($setting);
$this->clearCache();

return back();
}

Expand All @@ -85,7 +88,8 @@ public function updateOpenAi(Request $request, Setting $setting)
return back();
}

protected function clearCache() {
protected function clearCache()
{
Artisan::call('optimize:clear');
}
}
8 changes: 2 additions & 6 deletions app/Jobs/ToolJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,21 +13,19 @@
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.
*/
public function __construct(
public FunctionContract $function,
public Message $message
)
{
) {
//
}

Expand Down Expand Up @@ -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
*/

}
}
2 changes: 1 addition & 1 deletion app/Jobs/ToolsCompleteJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 0 additions & 5 deletions tests/Feature/ClaudeCompletionResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 0 additions & 2 deletions tests/Feature/OllamaCompletionResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 2 additions & 5 deletions tests/Feature/OrchestrateVersionTwoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = <<<PROMPT
$prompt = <<<'PROMPT'
Get the content from the url https://dailyai.studio
Then do a summary of the content breaking it down into
Expand Down Expand Up @@ -52,6 +50,5 @@ public function test_example(): void

Bus::assertBatchCount(1);


}
}

0 comments on commit 8a40ebd

Please sign in to comment.