Skip to content

Commit

Permalink
add some foundation to abstract out functions
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Mar 30, 2024
1 parent 74b336f commit 0ccbe50
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 0 deletions.
29 changes: 29 additions & 0 deletions app/LlmDriver/Functions/FunctionContract.php
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

View workflow job for this annotation

GitHub Actions / ci (8.2)

Class App\LlmDriver\Functions\FunctionDto constructor invoked with 2 parameters, 3 required.

Check failure on line 16 in app/LlmDriver/Functions/FunctionContract.php

View workflow job for this annotation

GitHub Actions / ci (8.2)

Parameter #2 $description of class App\LlmDriver\Functions\FunctionDto constructor expects string, array given.
}


abstract public function getParameters(): array;



/**
* @param array<string, mixed> $data
* @return array<string, mixed>
*/
abstract public function handle(array $data): array;
}
15 changes: 15 additions & 0 deletions app/LlmDriver/Functions/FunctionDto.php
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,
) {
}
}
25 changes: 25 additions & 0 deletions app/LlmDriver/Functions/ParameterCaster.php
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

Check failure on line 19 in app/LlmDriver/Functions/ParameterCaster.php

View workflow job for this annotation

GitHub Actions / ci (8.2)

Class App\LlmDriver\Functions\ParameterDto constructor invoked with 1 parameter, 2-6 required.
{
return new ParameterDto($value);
}
};
}
}
18 changes: 18 additions & 0 deletions app/LlmDriver/Functions/ParameterDto.php
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,
) {
}
}
21 changes: 21 additions & 0 deletions app/LlmDriver/Functions/ParametersDto.php
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 = [],
) {
}
}
71 changes: 71 additions & 0 deletions tests/Feature/FunctionDtoTest.php
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);

}
}

0 comments on commit 0ccbe50

Please sign in to comment.