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
46 changes: 46 additions & 0 deletions src/platform/src/Bridge/Mistral/Contract/ImageUrlNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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\ImageUrl;
use Symfony\AI\Platform\Model;

/**
* @author Franz Mayr-Wilding
*/
class ImageUrlNormalizer extends ModelContractNormalizer
{
/**
* @param ImageUrl $data
*
* @return array{type: 'image_url', image_url: string}
*/
public function normalize(mixed $data, ?string $format = null, array $context = []): array
{
return [
'type' => 'image_url',
'image_url' => $data->getUrl(),
];
}

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

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

use Symfony\AI\Platform\Bridge\Mistral\Contract\DocumentNormalizer;
use Symfony\AI\Platform\Bridge\Mistral\Contract\DocumentUrlNormalizer;
use Symfony\AI\Platform\Bridge\Mistral\Contract\ImageUrlNormalizer;
use Symfony\AI\Platform\Bridge\Mistral\Contract\ToolNormalizer;
use Symfony\AI\Platform\Contract;
use Symfony\AI\Platform\ModelCatalog\ModelCatalogInterface;
Expand Down Expand Up @@ -41,6 +42,7 @@ public static function create(
new ToolNormalizer(),
new DocumentNormalizer(),
new DocumentUrlNormalizer(),
new ImageUrlNormalizer(),
),
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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 Bridge\Mistral\Contract;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\AI\Platform\Bridge\Mistral\Contract\ImageUrlNormalizer;
use Symfony\AI\Platform\Bridge\Mistral\Mistral;
use Symfony\AI\Platform\Contract;
use Symfony\AI\Platform\Message\Content\ImageUrl;

final class ImageUrlNormalizerTest extends TestCase
{
public function testSupportsNormalization()
{
$normalizer = new ImageUrlNormalizer();

$this->assertTrue($normalizer->supportsNormalization(new ImageUrl('https://example.com/image.png'), context: [
Contract::CONTEXT_MODEL => new Mistral('mistral-large-latest'),
]));
$this->assertFalse($normalizer->supportsNormalization('not a image url'));
}

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

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

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

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

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

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

public static function normalizeDataProvider(): iterable
{
yield 'image with url' => [
new ImageUrl('https://example.com/image.png'),
[
'type' => 'image_url',
'image_url' => 'https://example.com/image.png',
],
];
}
}