From e7937be4876bb07800b7cf9261c20c4b129fe440 Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Fri, 24 Jan 2025 11:34:57 +0100 Subject: [PATCH 1/2] Support descriptions from @param --- .../ResponseFormatFactoryTest.php | 89 ++++++++++++++----- .../UserWithAtParamAnnotation.php | 20 +++++ 2 files changed, 87 insertions(+), 22 deletions(-) create mode 100644 tests/Fixture/StructuredOutput/UserWithAtParamAnnotation.php diff --git a/tests/Chain/StructuredOutput/ResponseFormatFactoryTest.php b/tests/Chain/StructuredOutput/ResponseFormatFactoryTest.php index 51825b52..a0a8f3c7 100644 --- a/tests/Chain/StructuredOutput/ResponseFormatFactoryTest.php +++ b/tests/Chain/StructuredOutput/ResponseFormatFactoryTest.php @@ -4,10 +4,12 @@ namespace PhpLlm\LlmChain\Tests\Chain\StructuredOutput; +use Fixture\StructuredOutput\UserWithAtParamAnnotation; use PhpLlm\LlmChain\Chain\StructuredOutput\ResponseFormatFactory; use PhpLlm\LlmChain\Chain\StructuredOutput\SchemaFactory; use PhpLlm\LlmChain\Tests\Fixture\StructuredOutput\User; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\Attributes\UsesClass; use PHPUnit\Framework\TestCase; @@ -17,33 +19,76 @@ final class ResponseFormatFactoryTest extends TestCase { #[Test] - public function create(): void + #[DataProvider('createProvider')] + /** + * @param array $expected + * @param class-string $class + */ + public function create(array $expected, string $class): void { - self::assertSame([ - 'type' => 'json_schema', - 'json_schema' => [ - 'name' => 'User', - 'schema' => [ - 'title' => 'User', - 'type' => 'object', - 'properties' => [ - 'id' => ['type' => 'integer'], - 'name' => [ - 'type' => 'string', - 'description' => 'The name of the user in lowercase', + self::assertSame($expected, (new ResponseFormatFactory())->create($class)); + } + + public static function createProvider(): iterable + { + yield [ + [ + 'type' => 'json_schema', + 'json_schema' => [ + 'name' => 'User', + 'schema' => [ + 'title' => 'User', + 'type' => 'object', + 'properties' => [ + 'id' => ['type' => 'integer'], + 'name' => [ + 'type' => 'string', + 'description' => 'The name of the user in lowercase', + ], + 'createdAt' => [ + 'type' => 'string', + 'format' => 'date-time', + ], + 'isActive' => ['type' => 'boolean'], + 'age' => ['type' => ['integer', 'null']], ], - 'createdAt' => [ - 'type' => 'string', - 'format' => 'date-time', + 'required' => ['id', 'name', 'createdAt', 'isActive'], + 'additionalProperties' => false, + ], + 'strict' => true, + ], + ], + User::class, + ]; + + yield [ + [ + 'type' => 'json_schema', + 'json_schema' => [ + 'name' => 'User', + 'schema' => [ + 'title' => 'User', + 'type' => 'object', + 'properties' => [ + 'id' => ['type' => 'integer'], + 'name' => [ + 'type' => 'string', + 'description' => 'The name of the user in lowercase', + ], + 'createdAt' => [ + 'type' => 'string', + 'format' => 'date-time', + ], + 'isActive' => ['type' => 'boolean'], + 'age' => ['type' => ['integer', 'null']], ], - 'isActive' => ['type' => 'boolean'], - 'age' => ['type' => ['integer', 'null']], + 'required' => ['id', 'name', 'createdAt', 'isActive'], + 'additionalProperties' => false, ], - 'required' => ['id', 'name', 'createdAt', 'isActive'], - 'additionalProperties' => false, + 'strict' => true, ], - 'strict' => true, ], - ], (new ResponseFormatFactory())->create(User::class)); + UserWithAtParamAnnotation::class, + ]; } } diff --git a/tests/Fixture/StructuredOutput/UserWithAtParamAnnotation.php b/tests/Fixture/StructuredOutput/UserWithAtParamAnnotation.php new file mode 100644 index 00000000..193eae59 --- /dev/null +++ b/tests/Fixture/StructuredOutput/UserWithAtParamAnnotation.php @@ -0,0 +1,20 @@ + Date: Fri, 24 Jan 2025 11:36:28 +0100 Subject: [PATCH 2/2] - --- tests/Chain/StructuredOutput/ResponseFormatFactoryTest.php | 2 +- tests/Fixture/StructuredOutput/UserWithAtParamAnnotation.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Chain/StructuredOutput/ResponseFormatFactoryTest.php b/tests/Chain/StructuredOutput/ResponseFormatFactoryTest.php index a0a8f3c7..4941d349 100644 --- a/tests/Chain/StructuredOutput/ResponseFormatFactoryTest.php +++ b/tests/Chain/StructuredOutput/ResponseFormatFactoryTest.php @@ -4,10 +4,10 @@ namespace PhpLlm\LlmChain\Tests\Chain\StructuredOutput; -use Fixture\StructuredOutput\UserWithAtParamAnnotation; use PhpLlm\LlmChain\Chain\StructuredOutput\ResponseFormatFactory; use PhpLlm\LlmChain\Chain\StructuredOutput\SchemaFactory; use PhpLlm\LlmChain\Tests\Fixture\StructuredOutput\User; +use PhpLlm\LlmChain\Tests\Fixture\StructuredOutput\UserWithAtParamAnnotation; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; diff --git a/tests/Fixture/StructuredOutput/UserWithAtParamAnnotation.php b/tests/Fixture/StructuredOutput/UserWithAtParamAnnotation.php index 193eae59..7937a54e 100644 --- a/tests/Fixture/StructuredOutput/UserWithAtParamAnnotation.php +++ b/tests/Fixture/StructuredOutput/UserWithAtParamAnnotation.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Fixture\StructuredOutput; +namespace PhpLlm\LlmChain\Tests\Fixture\StructuredOutput; final class UserWithAtParamAnnotation {