-
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.
add some foundation to abstract out functions
- Loading branch information
Showing
6 changed files
with
179 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace App\LlmDriver\Functions; | ||
|
||
abstract class FunctionContract | ||
{ | ||
protected string $name; | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getFunction() : FunctionDto { | ||
return new FunctionDto($this->name, $this->getParameters()); | ||
|
||
Check failure on line 16 in app/LlmDriver/Functions/FunctionContract.php GitHub Actions / ci (8.2)
|
||
} | ||
|
||
|
||
abstract public function getParameters(): array; | ||
|
||
|
||
|
||
/** | ||
* @param array<string, mixed> $data | ||
* @return array<string, mixed> | ||
*/ | ||
abstract public function handle(array $data): array; | ||
} |
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,15 @@ | ||
<?php | ||
|
||
namespace App\LlmDriver\Functions; | ||
|
||
use Spatie\LaravelData\Attributes\WithCastable; | ||
|
||
class FunctionDto extends \Spatie\LaravelData\Data | ||
{ | ||
public function __construct( | ||
public string $name, | ||
public string $description, | ||
public ParametersDto $parameters, | ||
) { | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
namespace App\LlmDriver\Responses; | ||
|
||
use App\LlmDriver\Functions\ParameterDto; | ||
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 ParamaterCaster implements Castable | ||
{ | ||
|
||
public static function dataCastUsing(...$arguments): Cast | ||
{ | ||
return new class implements Cast | ||
{ | ||
public function cast(DataProperty $property, mixed $value, array $properties, CreationContext $context): mixed | ||
{ | ||
return new ParameterDto($value); | ||
} | ||
}; | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
|
||
namespace App\LlmDriver\Functions; | ||
|
||
use Spatie\LaravelData\Attributes\WithCastable; | ||
|
||
class ParameterDto extends \Spatie\LaravelData\Data | ||
{ | ||
public function __construct( | ||
public string $name, | ||
public string $description, | ||
public string $type = "string", | ||
public array $enum = [], | ||
public string $default = "", | ||
public bool $required = false, | ||
) { | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace App\LlmDriver\Functions; | ||
|
||
use Spatie\LaravelData\Attributes\WithCastable; | ||
|
||
class ParametersDto extends \Spatie\LaravelData\Data | ||
{ | ||
|
||
/** | ||
* | ||
* @param string $type | ||
* @param ParameterDto[] $parameters | ||
* @return void | ||
*/ | ||
public function __construct( | ||
public string $type = "object", | ||
public array $parameters = [], | ||
) { | ||
} | ||
} |
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,71 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\LlmDriver\Functions\FunctionDto; | ||
use App\LlmDriver\Functions\ParameterDto; | ||
use App\LlmDriver\Functions\ParametersDto; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Foundation\Testing\WithFaker; | ||
use Tests\TestCase; | ||
|
||
class FunctionDtoTest extends TestCase | ||
{ | ||
/** | ||
* A basic feature test example. | ||
*/ | ||
public function test_dto(): void | ||
{ | ||
$dto = FunctionDto::from( | ||
[ | ||
'name' => 'test', | ||
'description' => 'test', | ||
'parameters' => [ | ||
'type' => 'object', | ||
'parameters' => [ | ||
[ | ||
'name' => 'test', | ||
'description' => 'test', | ||
'type' => 'string', | ||
'enum' => [], | ||
'default' => '', | ||
'required' => false, | ||
], | ||
[ | ||
'name' => 'test2', | ||
'description' => 'test2', | ||
'type' => 'string', | ||
'enum' => ['foo', 'bar'], | ||
'default' => 'bar', | ||
'required' => true, | ||
], | ||
], | ||
], | ||
] | ||
); | ||
|
||
|
||
$this->assertNotNull($dto->name); | ||
$this->assertNotNull($dto->description); | ||
$this->assertInstanceOf(ParametersDto::class, $dto->parameters); | ||
$this->assertCount(2, $dto->parameters->parameters); | ||
$parameterOne = $dto->parameters->parameters[0]; | ||
$this->assertInstanceOf(ParameterDto::class, $parameterOne); | ||
$this->assertEquals('test', $parameterOne->name); | ||
$this->assertEquals('test', $parameterOne->description); | ||
$this->assertEquals('string', $parameterOne->type); | ||
$this->assertEquals([], $parameterOne->enum); | ||
$this->assertEquals('', $parameterOne->default); | ||
$this->assertFalse($parameterOne->required); | ||
|
||
$parameterTwo = $dto->parameters->parameters[1]; | ||
$this->assertInstanceOf(ParameterDto::class, $parameterTwo); | ||
$this->assertEquals('test2', $parameterTwo->name); | ||
$this->assertEquals('test2', $parameterTwo->description); | ||
$this->assertEquals('string', $parameterTwo->type); | ||
$this->assertEquals(['foo', 'bar'], $parameterTwo->enum); | ||
$this->assertEquals('bar', $parameterTwo->default); | ||
$this->assertTrue($parameterTwo->required); | ||
|
||
} | ||
} |