Skip to content

Commit 36f5bb4

Browse files
HaKIMusOskarStark
authored andcommitted
[Agent][Platform] Add support for native union types and list of polymporphic* types by using DiscriminatorMap
1 parent 3645393 commit 36f5bb4

File tree

14 files changed

+524
-3
lines changed

14 files changed

+524
-3
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\AI\Agent\Agent;
13+
use Symfony\AI\Agent\StructuredOutput\AgentProcessor;
14+
use Symfony\AI\Fixtures\StructuredOutput\PolymorphicType\ListOfPolymorphicTypesDto;
15+
use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
16+
use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory;
17+
use Symfony\AI\Platform\Message\Message;
18+
use Symfony\AI\Platform\Message\MessageBag;
19+
20+
require_once dirname(__DIR__).'/bootstrap.php';
21+
22+
$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client());
23+
$model = new Gpt(Gpt::GPT_4O_MINI);
24+
25+
$processor = new AgentProcessor();
26+
$agent = new Agent($platform, $model, [$processor], [$processor], logger: logger());
27+
$messages = new MessageBag(
28+
Message::forSystem('You are a persona data collector! Return all the data you can gather from the user input.'),
29+
Message::ofUser('Hi! My name is John Doe, I am 30 years old and I live in Paris.'),
30+
);
31+
$result = $agent->call($messages, ['output_structure' => ListOfPolymorphicTypesDto::class]);
32+
33+
dump($result->getContent());
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\AI\Agent\Agent;
13+
use Symfony\AI\Agent\StructuredOutput\AgentProcessor;
14+
use Symfony\AI\Fixtures\StructuredOutput\UnionType\UnionTypeDto;
15+
use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
16+
use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory;
17+
use Symfony\AI\Platform\Message\Message;
18+
use Symfony\AI\Platform\Message\MessageBag;
19+
20+
require_once dirname(__DIR__).'/bootstrap.php';
21+
22+
$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client());
23+
$model = new Gpt(Gpt::GPT_4O_MINI);
24+
25+
$processor = new AgentProcessor();
26+
$agent = new Agent($platform, $model, [$processor], [$processor], logger: logger());
27+
$messages = new MessageBag(
28+
Message::forSystem(<<<PROMPT
29+
You are a time assistant! You can provide time either as a unix timestamp or as a human readable time format.
30+
If you don't know the time, return null.
31+
PROMPT),
32+
Message::ofUser('What is the current time?'),
33+
);
34+
$result = $agent->call($messages, ['output_structure' => UnionTypeDto::class]);
35+
36+
dump($result->getContent());
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Fixtures\StructuredOutput\PolymorphicType;
13+
14+
use Symfony\AI\Platform\Contract\JsonSchema\Attribute\With;
15+
16+
final class ListItemAge implements ListItemDiscriminator
17+
{
18+
public function __construct(
19+
public int $age,
20+
#[With(pattern: '^age$')]
21+
public string $type = 'age',
22+
) {
23+
}
24+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Fixtures\StructuredOutput\PolymorphicType;
13+
14+
use Symfony\Component\Serializer\Attribute\DiscriminatorMap;
15+
16+
#[DiscriminatorMap(
17+
typeProperty: 'type',
18+
mapping: [
19+
'name' => ListItemName::class,
20+
'age' => ListItemAge::class,
21+
]
22+
)]
23+
/**
24+
* @property string $type
25+
*
26+
* With the PHP 8.4^ you can replace the property annotation with a property hook:
27+
* public string $type {
28+
* get;
29+
* }
30+
*/
31+
interface ListItemDiscriminator
32+
{
33+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Fixtures\StructuredOutput\PolymorphicType;
13+
14+
use Symfony\AI\Platform\Contract\JsonSchema\Attribute\With;
15+
16+
class ListItemName implements ListItemDiscriminator
17+
{
18+
public function __construct(
19+
public string $name,
20+
#[With(pattern: '^name$')]
21+
public string $type = 'name',
22+
) {
23+
}
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Fixtures\StructuredOutput\PolymorphicType;
13+
14+
/**
15+
* Useful when you need to tell an agent that any of the items are acceptable types.
16+
* Real life example could be a list of possible analytical data visualization like charts or tables.
17+
*/
18+
final class ListOfPolymorphicTypesDto
19+
{
20+
/**
21+
* @param list<ListItemDiscriminator> $items
22+
*/
23+
public function __construct(public array $items)
24+
{
25+
}
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Fixtures\StructuredOutput\UnionType;
13+
14+
final class HumanReadableTimeUnion
15+
{
16+
public function __construct(public string $readableTime)
17+
{
18+
}
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Fixtures\StructuredOutput\UnionType;
13+
14+
final class UnionTypeDto
15+
{
16+
public function __construct(
17+
public UnixTimestampUnion|HumanReadableTimeUnion|null $time,
18+
) {
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Fixtures\StructuredOutput\UnionType;
13+
14+
final class UnixTimestampUnion
15+
{
16+
public function __construct(public int $timestamp)
17+
{
18+
}
19+
}

src/agent/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
0.1
55
---
66

7+
* Add support for union types and polymorphic types via DiscriminatorMap
78
* Add Agent class as central orchestrator for AI interactions through the Platform component
89
* Add input/output processing pipeline:
910
- `InputProcessorInterface` for pre-processing messages and options

0 commit comments

Comments
 (0)