Skip to content

Commit

Permalink
Simplify SchemaFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
OskarStark committed Oct 2, 2024
1 parent e7fb68e commit 5158c13
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 21 deletions.
3 changes: 1 addition & 2 deletions examples/structured-output-math.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@

$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
$llm = new Gpt($platform, Version::gpt4oMini());
$responseFormatFactory = new ResponseFormatFactory(SchemaFactory::create());
$serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);

$processor = new ChainProcessor($responseFormatFactory, $serializer);
$processor = new ChainProcessor(new ResponseFormatFactory(), $serializer);
$chain = new Chain($llm, [$processor], [$processor]);
$messages = new MessageBag(
Message::forSystem('You are a helpful math tutor. Guide the user through the solution step by step.'),
Expand Down
2 changes: 1 addition & 1 deletion src/StructuredOutput/ResponseFormatFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
final readonly class ResponseFormatFactory implements ResponseFormatFactoryInterface
{
public function __construct(
private SchemaFactory $schemaFactory,
private SchemaFactory $schemaFactory = new SchemaFactory(),
) {
}

Expand Down
29 changes: 13 additions & 16 deletions src/StructuredOutput/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,23 @@
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\PropertyInfo\Type;

final readonly class SchemaFactory
final class SchemaFactory
{
public function __construct(
private PropertyInfoExtractor $propertyInfo,
private ?PropertyInfoExtractor $propertyInfo = null,
) {
}
if (null === $propertyInfo) {
$phpDocExtractor = new PhpDocExtractor();
$reflectionExtractor = new ReflectionExtractor();

$this->propertyInfo = new PropertyInfoExtractor(
[$reflectionExtractor],
[$phpDocExtractor, $reflectionExtractor],
[$phpDocExtractor],
[$reflectionExtractor],
);
}

public static function create(): self
{
$phpDocExtractor = new PhpDocExtractor();
$reflectionExtractor = new ReflectionExtractor();

return new self(
new PropertyInfoExtractor(
[$reflectionExtractor],
[$phpDocExtractor, $reflectionExtractor],
[$phpDocExtractor],
[$reflectionExtractor],
)
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/StructuredOutput/ResponseFormatFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class ResponseFormatFactoryTest extends TestCase

protected function setUp(): void
{
$this->responseFormatFactory = new ResponseFormatFactory(SchemaFactory::create());
$this->responseFormatFactory = new ResponseFormatFactory();
}

#[Test]
Expand Down
2 changes: 1 addition & 1 deletion tests/StructuredOutput/SchemaFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class SchemaFactoryTest extends TestCase

protected function setUp(): void
{
$this->schemaFactory = SchemaFactory::create();
$this->schemaFactory = new SchemaFactory();
}

#[Test]
Expand Down

0 comments on commit 5158c13

Please sign in to comment.