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
33 changes: 33 additions & 0 deletions examples/mistral/pdf-input-binary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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.
*/

use Symfony\AI\Agent\Agent;
use Symfony\AI\Platform\Bridge\Mistral\Mistral;
use Symfony\AI\Platform\Bridge\Mistral\PlatformFactory;
use Symfony\AI\Platform\Message\Content\Document;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;

require_once dirname(__DIR__).'/bootstrap.php';

$platform = PlatformFactory::create(env('MISTRAL_API_KEY'), httpClient: http_client());
$model = new Mistral(Mistral::MISTRAL_SMALL);

$agent = new Agent($platform, $model, logger: logger());
$messages = new MessageBag(
Message::ofUser(
Document::fromFile(dirname(__DIR__, 2).'/fixtures/document.pdf'),
'What is this document about?',
),
);
$result = $agent->call($messages);

echo $result->getContent().\PHP_EOL;
33 changes: 33 additions & 0 deletions examples/mistral/pdf-input-url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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.
*/

use Symfony\AI\Agent\Agent;
use Symfony\AI\Platform\Bridge\Mistral\Mistral;
use Symfony\AI\Platform\Bridge\Mistral\PlatformFactory;
use Symfony\AI\Platform\Message\Content\DocumentUrl;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;

require_once dirname(__DIR__).'/bootstrap.php';

$platform = PlatformFactory::create(env('MISTRAL_API_KEY'), httpClient: http_client());
$model = new Mistral(Mistral::MISTRAL_SMALL);

$agent = new Agent($platform, $model, logger: logger());
$messages = new MessageBag(
Message::ofUser(
new DocumentUrl('https://upload.wikimedia.org/wikipedia/commons/2/20/Re_example.pdf'),
'What is this document about?',
),
);
$result = $agent->call($messages);

echo $result->getContent().\PHP_EOL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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\DocumentUrl;
use Symfony\AI\Platform\Model;

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

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

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

use Symfony\AI\Platform\Bridge\Mistral\Contract\DocumentNormalizer;
use Symfony\AI\Platform\Bridge\Mistral\Contract\DocumentUrlNormalizer;
use Symfony\AI\Platform\Bridge\Mistral\Contract\ToolNormalizer;
use Symfony\AI\Platform\Contract;
use Symfony\AI\Platform\Platform;
Expand All @@ -36,7 +37,8 @@ public static function create(
[new Embeddings\ResultConverter(), new Llm\ResultConverter()],
$contract ?? Contract::create(
new ToolNormalizer(),
new DocumentNormalizer()
new DocumentNormalizer(),
new DocumentUrlNormalizer(),
),
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?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\DocumentUrlNormalizer;
use Symfony\AI\Platform\Bridge\Mistral\Mistral;
use Symfony\AI\Platform\Contract;
use Symfony\AI\Platform\Message\Content\DocumentUrl;

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

$this->assertTrue($normalizer->supportsNormalization(new DocumentUrl('https://example.com/document.pdf'), context: [
Contract::CONTEXT_MODEL => new Mistral(),
]));
$this->assertFalse($normalizer->supportsNormalization('not a document url'));
}

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

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

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

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

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

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

public static function normalizeDataProvider(): iterable
{
yield 'document with url' => [
new DocumentUrl('https://example.com/document.pdf'),
[
'type' => 'document_url',
'document_url' => 'https://example.com/document.pdf',
],
];
}
}