-
-
Couldn't load subscription status.
- Fork 108
[Platform] Fix structured output capability check for unsupported models #794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Griffon-Weglot
wants to merge
2
commits into
symfony:main
Choose a base branch
from
Griffon-Weglot:fix/structured-outputs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -13,9 +13,11 @@ | |
|
|
||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\AI\Agent\Exception\MissingModelSupportException; | ||
| use Symfony\AI\Agent\Input; | ||
| use Symfony\AI\Agent\Output; | ||
| use Symfony\AI\Agent\StructuredOutput\AgentProcessor; | ||
| use Symfony\AI\Agent\StructuredOutput\ResponseFormatFactoryInterface; | ||
| use Symfony\AI\Fixtures\SomeStructure; | ||
| use Symfony\AI\Fixtures\StructuredOutput\MathReasoning; | ||
| use Symfony\AI\Fixtures\StructuredOutput\PolymorphicType\ListItemAge; | ||
|
|
@@ -25,18 +27,31 @@ | |
| use Symfony\AI\Fixtures\StructuredOutput\UnionType\HumanReadableTimeUnion; | ||
| use Symfony\AI\Fixtures\StructuredOutput\UnionType\UnionTypeDto; | ||
| use Symfony\AI\Fixtures\StructuredOutput\UnionType\UnixTimestampUnion; | ||
| use Symfony\AI\Platform\Capability; | ||
| use Symfony\AI\Platform\Message\MessageBag; | ||
| use Symfony\AI\Platform\Message\UserMessage; | ||
| use Symfony\AI\Platform\Metadata\Metadata; | ||
| use Symfony\AI\Platform\Model; | ||
| use Symfony\AI\Platform\ModelCatalog\ModelCatalogInterface; | ||
| use Symfony\AI\Platform\PlatformInterface; | ||
| use Symfony\AI\Platform\Result\ObjectResult; | ||
| use Symfony\AI\Platform\Result\TextResult; | ||
| use Symfony\Component\Serializer\SerializerInterface; | ||
|
|
||
| if (!class_exists(__NAMESPACE__.'\ConfigurableResponseFormatFactory')) { | ||
| class ConfigurableResponseFormatFactory implements ResponseFormatFactoryInterface { | ||
| public function __construct(private array $format = []) {} | ||
| public function create(string $structure): array { return $this->format; } | ||
| } | ||
| } | ||
|
Comment on lines
+41
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need this? there is already |
||
|
|
||
| final class AgentProcessorTest extends TestCase | ||
| { | ||
| public function testProcessInputWithOutputStructure() | ||
| { | ||
| $processor = new AgentProcessor(new ConfigurableResponseFormatFactory(['some' => 'format'])); | ||
| $input = new Input('gpt-4', new MessageBag(), ['output_structure' => 'SomeStructure']); | ||
| $platformMock = $this->createPlatformMock('gpt-4'); | ||
| $processor = new AgentProcessor($platformMock, new ConfigurableResponseFormatFactory(['some' => 'format'])); | ||
| $input = new Input('gpt-4', new MessageBag(), ['output_structure' => SomeStructure::class]); | ||
|
|
||
| $processor->processInput($input); | ||
|
|
||
|
|
@@ -45,38 +60,75 @@ public function testProcessInputWithOutputStructure() | |
|
|
||
| public function testProcessInputWithoutOutputStructure() | ||
| { | ||
| $processor = new AgentProcessor(new ConfigurableResponseFormatFactory()); | ||
| $platformMock = $this->createMock(PlatformInterface::class); | ||
| $processor = new AgentProcessor($platformMock, new ConfigurableResponseFormatFactory()); | ||
| $input = new Input('gpt-4', new MessageBag()); | ||
|
|
||
| $processor->processInput($input); | ||
|
|
||
| $this->assertSame([], $input->getOptions()); | ||
| } | ||
|
|
||
| public function testProcessInputThrowsExceptionForMissingSupport() | ||
| { | ||
| $modelName = 'model-without-structured-output'; | ||
|
|
||
| $modelMock = $this->createMock(Model::class); | ||
| $modelMock->method('getCapabilities')->willReturn([ | ||
| Capability::INPUT_MESSAGES, | ||
| Capability::OUTPUT_TEXT, | ||
| ]); | ||
| $modelMock->method('getName')->willReturn($modelName); | ||
|
|
||
| $modelCatalogMock = $this->createMock(ModelCatalogInterface::class); | ||
| $modelCatalogMock | ||
| ->expects($this->once()) | ||
| ->method('getModel') | ||
| ->with($modelName) | ||
| ->willReturn($modelMock); | ||
|
|
||
| $platformMock = $this->createMock(PlatformInterface::class); | ||
| $platformMock | ||
| ->expects($this->once()) | ||
| ->method('getModelCatalog') | ||
| ->willReturn($modelCatalogMock); | ||
|
|
||
| $processor = new AgentProcessor($platformMock, new ConfigurableResponseFormatFactory()); | ||
| $messages = new MessageBag(new UserMessage(new \Symfony\AI\Platform\Message\Content\Text('Hello'))); | ||
| $options = ['output_structure' => 'App\Dto\MyStructure']; | ||
| $input = new Input($modelName, $messages, $options); | ||
|
|
||
| $this->expectException(MissingModelSupportException::class); | ||
| $this->expectExceptionMessage(\sprintf('Model "%s" does not support "structured output".', $modelName)); | ||
|
|
||
| $processor->processInput($input); | ||
| } | ||
|
|
||
| public function testProcessOutputWithResponseFormat() | ||
| { | ||
| $processor = new AgentProcessor(new ConfigurableResponseFormatFactory(['some' => 'format'])); | ||
| $platformMock = $this->createPlatformMock('gpt-4'); | ||
| $processor = new AgentProcessor($platformMock, new ConfigurableResponseFormatFactory(['some' => 'format'])); | ||
|
|
||
| $options = ['output_structure' => SomeStructure::class]; | ||
| $input = new Input('gpt-4', new MessageBag(), $options); | ||
| $processor->processInput($input); | ||
|
|
||
| $result = new TextResult('{"some": "data"}'); | ||
|
|
||
| $output = new Output('gpt-4', $result, new MessageBag(), $input->getOptions()); | ||
|
|
||
| $processor->processOutput($output); | ||
|
|
||
| $this->assertInstanceOf(ObjectResult::class, $output->getResult()); | ||
| $this->assertInstanceOf(SomeStructure::class, $output->getResult()->getContent()); | ||
| $resultContent = $output->getResult()->getContent(); | ||
| $this->assertInstanceOf(SomeStructure::class, $resultContent); | ||
| $this->assertInstanceOf(Metadata::class, $output->getResult()->getMetadata()); | ||
| $this->assertNull($output->getResult()->getRawResult()); | ||
| $this->assertSame('data', $output->getResult()->getContent()->some); | ||
| $this->assertSame('data', $resultContent->some); | ||
| } | ||
|
|
||
| public function testProcessOutputWithComplexResponseFormat() | ||
| { | ||
| $processor = new AgentProcessor(new ConfigurableResponseFormatFactory(['some' => 'format'])); | ||
| $platformMock = $this->createPlatformMock('gpt-4'); | ||
| $processor = new AgentProcessor($platformMock, new ConfigurableResponseFormatFactory(['some' => 'format'])); | ||
|
|
||
| $options = ['output_structure' => MathReasoning::class]; | ||
| $input = new Input('gpt-4', new MessageBag(), $options); | ||
|
|
@@ -112,15 +164,17 @@ public function testProcessOutputWithComplexResponseFormat() | |
| JSON); | ||
|
|
||
| $output = new Output('gpt-4', $result, new MessageBag(), $input->getOptions()); | ||
|
|
||
| $processor->processOutput($output); | ||
|
|
||
| $this->assertInstanceOf(ObjectResult::class, $output->getResult()); | ||
| $this->assertInstanceOf(MathReasoning::class, $structure = $output->getResult()->getContent()); | ||
| $structure = $output->getResult()->getContent(); | ||
| $this->assertInstanceOf(MathReasoning::class, $structure); | ||
| $this->assertInstanceOf(Metadata::class, $output->getResult()->getMetadata()); | ||
| $this->assertNull($output->getResult()->getRawResult()); | ||
| $this->assertCount(5, $structure->steps); | ||
| $this->assertInstanceOf(Step::class, $structure->steps[0]); | ||
| $this->assertSame("We want to isolate the term with x. First, let's subtract 7 from both sides of the equation.", $structure->steps[0]->explanation); | ||
| $this->assertSame("8x + 7 - 7 = -23 - 7", $structure->steps[0]->output); | ||
| $this->assertInstanceOf(Step::class, $structure->steps[1]); | ||
| $this->assertInstanceOf(Step::class, $structure->steps[2]); | ||
| $this->assertInstanceOf(Step::class, $structure->steps[3]); | ||
|
|
@@ -129,13 +183,11 @@ public function testProcessOutputWithComplexResponseFormat() | |
| $this->assertSame('x = -3.75', $structure->finalAnswer); | ||
| } | ||
|
|
||
| /** | ||
| * @param class-string $expectedTimeStructure | ||
| */ | ||
| #[DataProvider('unionTimeTypeProvider')] | ||
| public function testProcessOutputWithUnionTypeResponseFormat(TextResult $result, string $expectedTimeStructure) | ||
| { | ||
| $processor = new AgentProcessor(new ConfigurableResponseFormatFactory(['some' => 'format'])); | ||
| $platformMock = $this->createPlatformMock('gpt-4'); | ||
| $processor = new AgentProcessor($platformMock, new ConfigurableResponseFormatFactory(['some' => 'format'])); | ||
|
|
||
| $options = ['output_structure' => UnionTypeDto::class]; | ||
| $input = new Input('gpt-4', new MessageBag(), $options); | ||
|
|
@@ -154,21 +206,8 @@ public function testProcessOutputWithUnionTypeResponseFormat(TextResult $result, | |
|
|
||
| public static function unionTimeTypeProvider(): array | ||
| { | ||
| $unixTimestampResult = new TextResult(<<<JSON | ||
| { | ||
| "time": { | ||
| "timestamp": 2212121 | ||
| } | ||
| } | ||
| JSON); | ||
|
|
||
| $humanReadableResult = new TextResult(<<<JSON | ||
| { | ||
| "time": { | ||
| "readableTime": "2023-10-10T10:10:10+00:00" | ||
| } | ||
| } | ||
| JSON); | ||
| $unixTimestampResult = new TextResult('{"time": {"timestamp": 2212121}}'); | ||
| $humanReadableResult = new TextResult('{"time": {"readableTime": "2023-10-10T10:10:10+00:00"}}'); | ||
|
|
||
| return [ | ||
| [$unixTimestampResult, UnixTimestampUnion::class], | ||
|
|
@@ -178,63 +217,72 @@ public static function unionTimeTypeProvider(): array | |
|
|
||
| public function testProcessOutputWithCorrectPolymorphicTypesResponseFormat() | ||
| { | ||
| $processor = new AgentProcessor(new ConfigurableResponseFormatFactory(['some' => 'format'])); | ||
| $platformMock = $this->createPlatformMock('gpt-4'); | ||
| $processor = new AgentProcessor($platformMock, new ConfigurableResponseFormatFactory(['some' => 'format'])); | ||
|
|
||
| $options = ['output_structure' => ListOfPolymorphicTypesDto::class]; | ||
| $input = new Input('gpt-4', new MessageBag(), $options); | ||
| $processor->processInput($input); | ||
|
|
||
| $result = new TextResult(<<<JSON | ||
| { | ||
| "items": [ | ||
| { | ||
| "type": "name", | ||
| "name": "John Doe" | ||
| }, | ||
| { | ||
| "type": "age", | ||
| "age": 24 | ||
| } | ||
| ] | ||
| } | ||
| {"items": [{"type": "name", "name": "John Doe"}, {"type": "age", "age": 24}]} | ||
| JSON); | ||
|
|
||
| $output = new Output('gpt-4', $result, new MessageBag(), $input->getOptions()); | ||
|
|
||
| $processor->processOutput($output); | ||
|
|
||
| $this->assertInstanceOf(ObjectResult::class, $output->getResult()); | ||
|
|
||
| /** @var ListOfPolymorphicTypesDto $structure */ | ||
| $structure = $output->getResult()->getContent(); | ||
| $this->assertInstanceOf(ListOfPolymorphicTypesDto::class, $structure); | ||
|
|
||
| $this->assertCount(2, $structure->items); | ||
|
|
||
| $nameItem = $structure->items[0]; | ||
| $ageItem = $structure->items[1]; | ||
|
|
||
| $this->assertInstanceOf(ListItemName::class, $nameItem); | ||
| $this->assertInstanceOf(ListItemAge::class, $ageItem); | ||
|
|
||
| $this->assertSame('John Doe', $nameItem->name); | ||
| $this->assertSame(24, $ageItem->age); | ||
|
|
||
| $this->assertSame('name', $nameItem->type); | ||
| $this->assertSame('age', $ageItem->type); | ||
| } | ||
|
|
||
| public function testProcessOutputWithoutResponseFormat() | ||
| { | ||
| $platformMock = $this->createMock(PlatformInterface::class); | ||
| $resultFormatFactory = new ConfigurableResponseFormatFactory(); | ||
| $serializer = self::createMock(SerializerInterface::class); | ||
| $processor = new AgentProcessor($resultFormatFactory, $serializer); | ||
| $serializer = $this->createMock(SerializerInterface::class); | ||
| $processor = new AgentProcessor($platformMock, $resultFormatFactory, $serializer); | ||
|
|
||
| $result = new TextResult(''); | ||
| $output = new Output('gpt4', $result, new MessageBag()); | ||
|
|
||
| $processor->processOutput($output); | ||
|
|
||
| $this->assertSame($result, $output->getResult()); | ||
| } | ||
|
|
||
| private function createPlatformMock(string $modelName): PlatformInterface | ||
| { | ||
| $modelMock = $this->createMock(Model::class); | ||
| $modelMock->method('getCapabilities')->willReturn([ | ||
| Capability::INPUT_MESSAGES, | ||
| Capability::OUTPUT_TEXT, | ||
| Capability::OUTPUT_STRUCTURED, | ||
| ]); | ||
| $modelMock->method('getName')->willReturn($modelName); | ||
|
|
||
| $modelCatalogMock = $this->createMock(ModelCatalogInterface::class); | ||
| $modelCatalogMock | ||
| ->method('getModel') | ||
| ->with($modelName) | ||
| ->willReturn($modelMock); | ||
|
|
||
| $platformMock = $this->createMock(PlatformInterface::class); | ||
| $platformMock | ||
| ->method('getModelCatalog') | ||
| ->willReturn($modelCatalogMock); | ||
|
|
||
| return $platformMock; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this would be enough for me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not familiar with the new pattern.
Is it ok to inject the platform every time we need the capabilities ; or would it be better to have the model instance directly in the input like it was before @chr-hertel ?