-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
186 additions
and
29 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
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
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|Optional $stop_reason, | ||
public ?string $tool_used = '', | ||
/** @var array<ToolDto> */ | ||
#[WithCastable(ClaudeToolCaster::class)] | ||
#[MapInputName('content')] | ||
public array $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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace LlmLaraHub\LlmDriver\Responses; | ||
|
||
use Spatie\LaravelData\Data; | ||
use Spatie\LaravelData\Optional; | ||
|
||
class ToolDto extends Data | ||
{ | ||
public function __construct( | ||
public string $name, | ||
public array $arguments, | ||
public string|Optional $id = '', | ||
) { | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
|
||
namespace Tests\Api; | ||
|
||
use App\Domains\Messages\RoleEnum; | ||
use App\Models\Setting; | ||
use App\Models\Source; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Facades\Http; | ||
use LlmLaraHub\LlmDriver\LlmDriverFacade; | ||
use App\Domains\EmailParser\MailDto; | ||
use LlmLaraHub\LlmDriver\Requests\MessageInDto; | ||
use Tests\TestCase; | ||
|
||
class ClientsTest extends TestCase | ||
{ | ||
|
||
|
||
public function test_clients() { | ||
$this->markTestSkipped('@TODO will setup the tokens shortly'); | ||
$prompt = <<<PROMPT | ||
What do you know about Laravel | ||
PROMPT; | ||
|
||
$messages = []; | ||
$messages[] = MessageInDto::from([ | ||
"role" => RoleEnum::User->value, | ||
"content" => $prompt | ||
]); | ||
$results = LlmDriverFacade::driver("groq")->chat($messages); | ||
$this->assertNotNull($results->content); | ||
$results = LlmDriverFacade::driver("openai")->chat($messages); | ||
$this->assertNotNull($results->content); | ||
$results = LlmDriverFacade::driver("claude")->chat($messages); | ||
$this->assertNotNull($results->content); | ||
|
||
} | ||
} |