Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/platform/src/Bridge/Mistral/Contract/DocumentNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Platform\Bridge\Mistral\Contract;

use Symfony\AI\Platform\Bridge\Mistral\Mistral;
use Symfony\AI\Platform\Contract\Normalizer\ModelContractNormalizer;
use Symfony\AI\Platform\Message\Content\Document;
use Symfony\AI\Platform\Model;

class DocumentNormalizer extends ModelContractNormalizer
{
/**
* @param Document $data
*
* @return array{type: 'document_url', document_name: string, document_url: string}
*/
public function normalize(mixed $data, ?string $format = null, array $context = []): array
{
return [
'type' => 'document_url',
'document_name' => $data->getFilename(),
'document_url' => $data->asDataUrl(),
];
}

protected function supportedDataClass(): string
{
return Document::class;
}

protected function supportsModel(Model $model): bool
{
return $model instanceof Mistral;
}
}
6 changes: 5 additions & 1 deletion src/platform/src/Bridge/Mistral/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\AI\Platform\Bridge\Mistral;

use Symfony\AI\Platform\Bridge\Mistral\Contract\DocumentNormalizer;
use Symfony\AI\Platform\Bridge\Mistral\Contract\ToolNormalizer;
use Symfony\AI\Platform\Contract;
use Symfony\AI\Platform\Platform;
Expand All @@ -33,7 +34,10 @@ public static function create(
return new Platform(
[new Embeddings\ModelClient($httpClient, $apiKey), new Llm\ModelClient($httpClient, $apiKey)],
[new Embeddings\ResultConverter(), new Llm\ResultConverter()],
$contract ?? Contract::create(new ToolNormalizer()),
$contract ?? Contract::create(
new ToolNormalizer(),
new DocumentNormalizer()
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Platform\Tests\Bridge\Mistral\Contract;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Medium;
use PHPUnit\Framework\TestCase;
use Symfony\AI\Platform\Bridge\Mistral\Contract\DocumentNormalizer;
use Symfony\AI\Platform\Bridge\Mistral\Mistral;
use Symfony\AI\Platform\Contract;
use Symfony\AI\Platform\Message\Content\Document;

#[Medium]
#[CoversClass(DocumentNormalizer::class)]
final class DocumentNormalizerTest extends TestCase
{
public function testSupportsNormalization()
{
$normalizer = new DocumentNormalizer();

$this->assertTrue($normalizer->supportsNormalization(new Document('some content', 'application/pdf'), context: [
Contract::CONTEXT_MODEL => new Mistral(),
]));
$this->assertFalse($normalizer->supportsNormalization('not a document'));
}

public function testGetSupportedTypes()
{
$normalizer = new DocumentNormalizer();

$expected = [
Document::class => true,
];

$this->assertSame($expected, $normalizer->getSupportedTypes(null));
}

#[DataProvider('normalizeDataProvider')]
public function testNormalize(Document $file, array $expected)
{
$normalizer = new DocumentNormalizer();

$normalized = $normalizer->normalize($file);

$this->assertEquals($expected, $normalized);
}

public static function normalizeDataProvider(): iterable
{
yield 'document from file' => [
Document::fromFile(\dirname(__DIR__, 3).'/fixtures/document.pdf'),
[
'type' => 'document_url',
'document_name' => 'document.pdf',
'document_url' => 'data:application/pdf;base64,'.base64_encode(file_get_contents(\dirname(__DIR__, 3).'/fixtures/document.pdf')),
],
];
}
}