Skip to content

Commit a9a2b22

Browse files
committed
feat(mistral): DocumentNormalizer
1 parent afe8816 commit a9a2b22

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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\Platform\Bridge\Mistral\Contract;
13+
14+
use Symfony\AI\Platform\Bridge\Mistral\Mistral;
15+
use Symfony\AI\Platform\Contract\Normalizer\ModelContractNormalizer;
16+
use Symfony\AI\Platform\Message\Content\Document;
17+
use Symfony\AI\Platform\Model;
18+
19+
class DocumentNormalizer extends ModelContractNormalizer
20+
{
21+
/**
22+
* @param Document $data
23+
*
24+
* @return array{type: 'document_url', document_name: string, document_url: string}
25+
*/
26+
public function normalize(mixed $data, ?string $format = null, array $context = []): array
27+
{
28+
return [
29+
'type' => 'document_url',
30+
'document_name' => $data->getFilename(),
31+
'document_url' => $data->asDataUrl(),
32+
];
33+
}
34+
35+
protected function supportedDataClass(): string
36+
{
37+
return Document::class;
38+
}
39+
40+
protected function supportsModel(Model $model): bool
41+
{
42+
return $model instanceof Mistral;
43+
}
44+
}

src/platform/src/Bridge/Mistral/PlatformFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\AI\Platform\Bridge\Mistral;
1313

14+
use Symfony\AI\Platform\Bridge\Mistral\Contract\DocumentNormalizer;
1415
use Symfony\AI\Platform\Bridge\Mistral\Contract\ToolNormalizer;
1516
use Symfony\AI\Platform\Contract;
1617
use Symfony\AI\Platform\Platform;
@@ -33,7 +34,10 @@ public static function create(
3334
return new Platform(
3435
[new Embeddings\ModelClient($httpClient, $apiKey), new Llm\ModelClient($httpClient, $apiKey)],
3536
[new Embeddings\ResultConverter(), new Llm\ResultConverter()],
36-
$contract ?? Contract::create(new ToolNormalizer()),
37+
$contract ?? Contract::create(
38+
new ToolNormalizer(),
39+
new DocumentNormalizer()
40+
),
3741
);
3842
}
3943
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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\Platform\Tests\Bridge\Mistral\Contract;
13+
14+
use PHPUnit\Framework\Attributes\CoversClass;
15+
use PHPUnit\Framework\Attributes\DataProvider;
16+
use PHPUnit\Framework\Attributes\Medium;
17+
use PHPUnit\Framework\TestCase;
18+
use Symfony\AI\Platform\Bridge\Mistral\Contract\DocumentNormalizer;
19+
use Symfony\AI\Platform\Bridge\Mistral\Mistral;
20+
use Symfony\AI\Platform\Contract;
21+
use Symfony\AI\Platform\Message\Content\Document;
22+
23+
#[Medium]
24+
#[CoversClass(DocumentNormalizer::class)]
25+
final class DocumentNormalizerTest extends TestCase
26+
{
27+
public function testSupportsNormalization()
28+
{
29+
$normalizer = new DocumentNormalizer();
30+
31+
$this->assertTrue($normalizer->supportsNormalization(new Document('some content', 'application/pdf'), context: [
32+
Contract::CONTEXT_MODEL => new Mistral(),
33+
]));
34+
$this->assertFalse($normalizer->supportsNormalization('not a document'));
35+
}
36+
37+
public function testGetSupportedTypes()
38+
{
39+
$normalizer = new DocumentNormalizer();
40+
41+
$expected = [
42+
Document::class => true,
43+
];
44+
45+
$this->assertSame($expected, $normalizer->getSupportedTypes(null));
46+
}
47+
48+
#[DataProvider('normalizeDataProvider')]
49+
public function testNormalize(Document $file, array $expected)
50+
{
51+
$normalizer = new DocumentNormalizer();
52+
53+
$normalized = $normalizer->normalize($file);
54+
55+
$this->assertEquals($expected, $normalized);
56+
}
57+
58+
public static function normalizeDataProvider(): iterable
59+
{
60+
yield 'document from file' => [
61+
Document::fromFile(\dirname(__DIR__, 3).'/fixtures/document.pdf'),
62+
[
63+
'type' => 'document_url',
64+
'document_name' => 'document.pdf',
65+
'document_url' => 'data:application/pdf;base64,'.base64_encode(file_get_contents(\dirname(__DIR__, 3).'/fixtures/document.pdf')),
66+
],
67+
];
68+
}
69+
}

0 commit comments

Comments
 (0)