-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make the claude response dto smarter and add tools to it like ollama
- Loading branch information
Showing
8 changed files
with
146 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
Modules/LlmDriver/app/Responses/ClaudeCompletionResponse.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace LlmLaraHub\LlmDriver\Responses; | ||
|
||
use Spatie\LaravelData\Attributes\MapInputName; | ||
use Spatie\LaravelData\Attributes\WithCastable; | ||
use Spatie\LaravelData\Optional; | ||
|
||
class ClaudeCompletionResponse extends CompletionResponse | ||
{ | ||
public function __construct( | ||
#[WithCastable(ClaudeContentCaster::class)] | ||
public mixed $content, | ||
public string $stop_reason = 'end_turn', | ||
public ?string $tool_used = null, | ||
/** @var array<ToolDto> */ | ||
#[WithCastable(ClaudeToolCaster::class)] | ||
#[MapInputName('content')] | ||
public array|Optional $tool_calls, | ||
#[MapInputName('usage.input_tokens')] | ||
public ?int $input_tokens = null, | ||
#[MapInputName('usage.output_tokens')] | ||
public ?int $output_tokens = null, | ||
public ?string $model = null, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace LlmLaraHub\LlmDriver\Responses; | ||
|
||
use Pgvector\Laravel\Vector; | ||
use Spatie\LaravelData\Casts\Cast; | ||
use Spatie\LaravelData\Casts\Castable; | ||
use Spatie\LaravelData\Support\Creation\CreationContext; | ||
use Spatie\LaravelData\Support\DataProperty; | ||
|
||
class ClaudeContentCaster implements Castable | ||
{ | ||
public function __construct(public array $content) | ||
{ | ||
|
||
} | ||
|
||
public static function dataCastUsing(...$arguments): Cast | ||
{ | ||
return new class implements Cast | ||
{ | ||
public function cast(DataProperty $property, mixed $value, array $properties, CreationContext $context): mixed | ||
{ | ||
$results = collect($value)->filter( | ||
function ($item) { | ||
return $item['type'] === 'text'; | ||
} | ||
)->first(); | ||
return data_get($results, 'text'); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace LlmLaraHub\LlmDriver\Responses; | ||
|
||
use Pgvector\Laravel\Vector; | ||
use Spatie\LaravelData\Casts\Cast; | ||
use Spatie\LaravelData\Casts\Castable; | ||
use Spatie\LaravelData\Support\Creation\CreationContext; | ||
use Spatie\LaravelData\Support\DataProperty; | ||
|
||
class ClaudeToolCaster implements Castable | ||
{ | ||
public function __construct(public array $tools) | ||
{ | ||
|
||
} | ||
|
||
public static function dataCastUsing(...$arguments): Cast | ||
{ | ||
return new class implements Cast | ||
{ | ||
public function cast(DataProperty $property, mixed $value, array $properties, CreationContext $context): mixed | ||
{ | ||
$results = collect($value)->filter( | ||
function ($item) { | ||
return $item['type'] === 'tool_use'; | ||
} | ||
)->toArray(); | ||
|
||
foreach($results as $index => $result) { | ||
$results[$index] = ToolDto::from( | ||
[ | ||
'name' => $result['name'], | ||
'arguments' => $result['input'], | ||
'id' => $result['id'], | ||
] | ||
); | ||
} | ||
return $results; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
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; | ||
|
||
class ClaudeCompletionResponseTest extends TestCase | ||
{ | ||
/** | ||
* A basic feature test example. | ||
*/ | ||
public function test_dto(): void | ||
{ | ||
$results = get_fixture('cloud_client_tool_use_response.json'); | ||
$dto = ClaudeCompletionResponse::from($results); | ||
$this->assertNotNull($dto->stop_reason); | ||
$this->assertNotNull($dto->model); | ||
$this->assertNotNull($dto->content); | ||
$this->assertNotNull($dto->tool_calls); | ||
$tool = Arr::first($dto->tool_calls); | ||
$this->assertInstanceOf(ToolDto::class, $tool); | ||
} | ||
} |