Skip to content

Commit 3460407

Browse files
authored
feat: allow JsonSerializable objects in contract (#377)
As brought up in #371 it could be a possible extension point to the contract to reintroduce the support for `JsonSerializable` objects when there are no normalizers for an object. So it would be a possible alternative for custom `MessageBagInterface`, `MessageInterface`, `ContentInterface`, etc. by allowing them to implement the `JsonSerializable` interface.
1 parent c9e2ad9 commit 3460407

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/Platform/Contract.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PhpLlm\LlmChain\Platform\Contract\Normalizer\ToolNormalizer;
1818
use PhpLlm\LlmChain\Platform\Tool\Tool;
1919
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
20+
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
2021
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2122
use Symfony\Component\Serializer\Serializer;
2223

@@ -53,6 +54,9 @@ public static function create(NormalizerInterface ...$normalizer): self
5354
// Response
5455
$normalizer[] = new ToolCallNormalizer();
5556

57+
// JsonSerializable objects as extension point to library interfaces
58+
$normalizer[] = new JsonSerializableNormalizer();
59+
5660
return new self(
5761
new Serializer($normalizer),
5862
);

tests/Platform/ContractTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use PhpLlm\LlmChain\Platform\Message\Content\ImageUrl;
2525
use PhpLlm\LlmChain\Platform\Message\Message;
2626
use PhpLlm\LlmChain\Platform\Message\MessageBag;
27+
use PhpLlm\LlmChain\Platform\Message\MessageInterface;
28+
use PhpLlm\LlmChain\Platform\Message\Role;
2729
use PhpLlm\LlmChain\Platform\Message\SystemMessage;
2830
use PhpLlm\LlmChain\Platform\Message\UserMessage;
2931
use PhpLlm\LlmChain\Platform\Model;
@@ -33,6 +35,7 @@
3335
use PHPUnit\Framework\Attributes\Test;
3436
use PHPUnit\Framework\Attributes\UsesClass;
3537
use PHPUnit\Framework\TestCase;
38+
use Symfony\Component\Uid\Uuid;
3639

3740
#[Large]
3841
#[CoversClass(Contract::class)]
@@ -191,6 +194,37 @@ public static function providePayloadTestCases(): iterable
191194
'model' => 'gpt-4o',
192195
],
193196
];
197+
198+
$customSerializableMessage = new class implements MessageInterface, \JsonSerializable {
199+
public function getRole(): Role
200+
{
201+
return Role::User;
202+
}
203+
204+
public function getId(): Uuid
205+
{
206+
return Uuid::v7();
207+
}
208+
209+
public function jsonSerialize(): array
210+
{
211+
return [
212+
'role' => 'user',
213+
'content' => 'This is a custom serializable message.',
214+
];
215+
}
216+
};
217+
218+
yield 'MessageBag with custom message from GPT' => [
219+
'model' => new GPT(),
220+
'input' => new MessageBag($customSerializableMessage),
221+
'expected' => [
222+
'messages' => [
223+
['role' => 'user', 'content' => 'This is a custom serializable message.'],
224+
],
225+
'model' => 'gpt-4o',
226+
],
227+
];
194228
}
195229

196230
#[Test]

0 commit comments

Comments
 (0)