-
-
Notifications
You must be signed in to change notification settings - Fork 116
[Platform] Support multimodal embeddings with Voyage AI #811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| Voyage AI | ||
| ========= | ||
|
|
||
| Voyage AI offers a number of models for embedding text, contextualized chunks, interleaved multimodal data, and reranking. | ||
| The bundle currently supports text embedding and multimodal embedding. | ||
|
|
||
| For comprehensive information about Voyage AI, see the `Voyage AI API reference`_ | ||
|
|
||
| Setup | ||
| ----- | ||
|
|
||
| Authentication | ||
| ~~~~~~~~~~~~~~ | ||
|
|
||
| Voyage AI requires an API key, which you can set up in `Voyage AI dashboard`_. | ||
|
|
||
| Usage | ||
| ----- | ||
|
|
||
| Basic text embedding usage example:: | ||
|
|
||
| use Symfony\AI\Platform\Bridge\Voyage\PlatformFactory; | ||
|
|
||
| $platform = PlatformFactory::create($_ENV['VOYAGE_API_KEY'], $httpClient); | ||
|
|
||
| $result = $platform->invoke('voyage-3', <<<TEXT | ||
| Once upon a time, there was a country called Japan. It was a beautiful country with a lot of mountains and | ||
| rivers. The people of Japan were very kind and hardworking. They loved their country very much and took care of | ||
| it. The country was very peaceful and prosperous. The people lived happily ever after. | ||
| TEXT); | ||
|
|
||
| echo $result->getContent(); | ||
|
|
||
| Voyage AI supports text, base64 image data, and image URLs in its multimodal embedding model. It also allows for | ||
| multiple data types per vector embedding. To do this, wrap the data in a `Collection` as shown in the example below. | ||
|
|
||
| Basic multimodal embedding usage example:: | ||
|
|
||
| use Symfony\AI\Platform\Bridge\Voyage\PlatformFactory; | ||
| use Symfony\AI\Platform\Message\Content\Collection; | ||
| use Symfony\AI\Platform\Message\Content\ImageUrl; | ||
| use Symfony\AI\Platform\Message\Content\Text; | ||
|
|
||
| $platform = PlatformFactory::create($_ENV['VOYAGE_API_KEY'], $httpClient); | ||
|
|
||
| $result = $platform->invoke( | ||
| 'voyage-multimodal-3', | ||
| new ImageUrl('https://example.com/image1.jpg'), | ||
| new Collection(new Text('Hello, world!'), new ImageUrl('https://example.com/image2.jpg') | ||
| ); | ||
|
|
||
| echo $result->getContent(); | ||
|
|
||
|
|
||
| Examples | ||
| -------- | ||
|
|
||
| See the ``examples/voyage/`` directory for complete working examples: | ||
|
|
||
| * ``text-embeddings.php`` - Basic text embedding example | ||
| * ``multiple-text-embeddings.php`` - Embedding multiple text values | ||
| * ``multimodal-embeddings.php`` - Embedding multimodal data (single and multiple values) | ||
|
|
||
| .. _Voyage AI API reference: https://docs.voyageai.com/reference/embeddings-api | ||
| .. _Voyage AI dashboard: https://dashboard.voyageai.com/organization/api-keys |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <?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\Platform\Bridge\Voyage\PlatformFactory; | ||
| use Symfony\AI\Platform\Message\Content\Collection; | ||
| use Symfony\AI\Platform\Message\Content\Image; | ||
| use Symfony\AI\Platform\Message\Content\Text; | ||
|
|
||
| require_once dirname(__DIR__).'/bootstrap.php'; | ||
|
|
||
| $platform = PlatformFactory::create(env('VOYAGE_API_KEY'), http_client()); | ||
|
|
||
| $image = Image::fromFile(dirname(__DIR__, 2).'/fixtures/image.jpg'); | ||
|
|
||
| // Single value | ||
| $result1 = $platform->invoke('voyage-multimodal-3', | ||
| new Text('Lorem ipsum dolor sit amet, consectetur adipiscing elit.'), | ||
| ); | ||
|
|
||
| // Multiple values | ||
| $result2 = $platform->invoke('voyage-multimodal-3', [ | ||
| new Collection(new Text('Photo of a sunrise'), $image), | ||
| new Collection(new Text('Photo of a sunset'), $image), | ||
| ]); | ||
|
|
||
| echo 'Dimensions for text: '.$result1->asVectors()[0]->getDimensions().\PHP_EOL; | ||
|
|
||
| echo 'Dimensions for sunrise image and description: '.$result2->asVectors()[0]->getDimensions().\PHP_EOL; | ||
| echo 'Dimensions for sunset image and description: '.$result2->asVectors()[1]->getDimensions().\PHP_EOL; |
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/platform/src/Bridge/Voyage/Contract/Multimodal/CollectionNormalizer.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| <?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\Voyage\Contract\Multimodal; | ||
|
|
||
| use Symfony\AI\Platform\Bridge\Voyage\Voyage; | ||
| use Symfony\AI\Platform\Capability; | ||
| use Symfony\AI\Platform\Contract; | ||
| use Symfony\AI\Platform\Message\Content\Collection; | ||
| use Symfony\AI\Platform\Model; | ||
| use Symfony\Component\Serializer\Exception\ExceptionInterface; | ||
| use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; | ||
| use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; | ||
|
|
||
| final class CollectionNormalizer extends Contract\Normalizer\ModelContractNormalizer implements NormalizerAwareInterface | ||
| { | ||
| use NormalizerAwareTrait; | ||
|
|
||
| public const KEY_CONTENT = 'content'; | ||
|
|
||
| /** | ||
| * @param Collection $data | ||
| * | ||
| * @throws ExceptionInterface | ||
| */ | ||
| public function normalize(mixed $data, ?string $format = null, array $context = []): array | ||
| { | ||
| $content = []; | ||
| foreach ($data->getContent() as $item) { | ||
| $normalized = $this->normalizer->normalize($item, $format, $context); | ||
| $content = array_merge($content, array_pop($normalized)[self::KEY_CONTENT]); | ||
| } | ||
|
|
||
| return [['content' => $content]]; | ||
| } | ||
|
|
||
| protected function supportedDataClass(): string | ||
| { | ||
| return Collection::class; | ||
| } | ||
|
|
||
| protected function supportsModel(Model $model): bool | ||
| { | ||
| return $model instanceof Voyage && $model->supports(Capability::INPUT_MULTIMODAL); | ||
| } | ||
| } |
50 changes: 50 additions & 0 deletions
50
src/platform/src/Bridge/Voyage/Contract/Multimodal/ImageNormalizer.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?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\Voyage\Contract\Multimodal; | ||
|
|
||
| use Symfony\AI\Platform\Bridge\Voyage\Voyage; | ||
| use Symfony\AI\Platform\Capability; | ||
| use Symfony\AI\Platform\Contract\Normalizer\ModelContractNormalizer; | ||
| use Symfony\AI\Platform\Message\Content\Image; | ||
| use Symfony\AI\Platform\Model; | ||
|
|
||
| use function Symfony\Component\String\u; | ||
|
|
||
| final class ImageNormalizer extends ModelContractNormalizer | ||
| { | ||
| /** | ||
| * @param Image $data | ||
| */ | ||
| public function normalize(mixed $data, ?string $format = null, array $context = []): array | ||
| { | ||
| return [[ | ||
| CollectionNormalizer::KEY_CONTENT => [[ | ||
| 'type' => 'image_base64', | ||
| 'image_base64' => \sprintf( | ||
| 'data:%s;base64,%s', | ||
| u($data->getFormat())->replace('jpg', 'jpeg'), | ||
| $data->asBase64() | ||
| ), | ||
| ]], | ||
| ]]; | ||
| } | ||
|
|
||
| protected function supportedDataClass(): string | ||
| { | ||
| return Image::class; | ||
| } | ||
|
|
||
| protected function supportsModel(Model $model): bool | ||
| { | ||
| return $model instanceof Voyage && $model->supports(Capability::INPUT_MULTIMODAL); | ||
| } | ||
| } | ||
44 changes: 44 additions & 0 deletions
44
src/platform/src/Bridge/Voyage/Contract/Multimodal/ImageUrlNormalizer.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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\Voyage\Contract\Multimodal; | ||
|
|
||
| use Symfony\AI\Platform\Bridge\Voyage\Voyage; | ||
| use Symfony\AI\Platform\Capability; | ||
| use Symfony\AI\Platform\Contract\Normalizer\ModelContractNormalizer; | ||
| use Symfony\AI\Platform\Message\Content\ImageUrl; | ||
| use Symfony\AI\Platform\Model; | ||
|
|
||
| final class ImageUrlNormalizer extends ModelContractNormalizer | ||
| { | ||
| /** | ||
| * @param ImageUrl $data | ||
| */ | ||
| public function normalize(mixed $data, ?string $format = null, array $context = []): array | ||
| { | ||
| return [[ | ||
| CollectionNormalizer::KEY_CONTENT => [[ | ||
| 'type' => 'image_url', | ||
| 'image_url' => $data->getUrl(), | ||
| ]], | ||
| ]]; | ||
| } | ||
|
|
||
| protected function supportedDataClass(): string | ||
| { | ||
| return ImageUrl::class; | ||
| } | ||
|
|
||
| protected function supportsModel(Model $model): bool | ||
| { | ||
| return $model instanceof Voyage && $model->supports(Capability::INPUT_MULTIMODAL); | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
src/platform/src/Bridge/Voyage/Contract/Multimodal/MultimodalNormalizer.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <?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\Voyage\Contract\Multimodal; | ||
|
|
||
| use Symfony\AI\Platform\Bridge\Voyage\Voyage; | ||
| use Symfony\AI\Platform\Capability; | ||
| use Symfony\AI\Platform\Contract; | ||
| use Symfony\AI\Platform\Message\Content\ContentInterface; | ||
| use Symfony\Component\Serializer\Exception\ExceptionInterface; | ||
| use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; | ||
| use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; | ||
| use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
|
|
||
| final class MultimodalNormalizer implements NormalizerInterface, NormalizerAwareInterface | ||
| { | ||
| use NormalizerAwareTrait; | ||
|
|
||
| /** | ||
| * @param ContentInterface[] $data | ||
| * | ||
| * @throws ExceptionInterface | ||
| */ | ||
| public function normalize(mixed $data, ?string $format = null, array $context = []): array | ||
| { | ||
| return array_map( | ||
| function (ContentInterface $item) use ($format, $context) { | ||
| $normalized = $this->normalizer->normalize($item, $format, $context); | ||
|
|
||
| return array_pop($normalized); | ||
| }, | ||
| $data | ||
| ); | ||
| } | ||
|
|
||
| public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool | ||
| { | ||
| $model = $context[Contract::CONTEXT_MODEL] ?? null; | ||
| if (!$model instanceof Voyage || !$model->supports(Capability::INPUT_MULTIMODAL)) { | ||
| return false; | ||
| } | ||
|
|
||
| return \is_array($data) && [] === array_filter($data, fn ($item) => !$item instanceof ContentInterface); | ||
| } | ||
|
|
||
| public function getSupportedTypes(?string $format): array | ||
| { | ||
| return [ | ||
| 'native-array' => true, | ||
| ]; | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
src/platform/src/Bridge/Voyage/Contract/Multimodal/TextNormalizer.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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\Voyage\Contract\Multimodal; | ||
|
|
||
| use Symfony\AI\Platform\Bridge\Voyage\Voyage; | ||
| use Symfony\AI\Platform\Capability; | ||
| use Symfony\AI\Platform\Contract\Normalizer\ModelContractNormalizer; | ||
| use Symfony\AI\Platform\Message\Content\Text; | ||
| use Symfony\AI\Platform\Model; | ||
|
|
||
| final class TextNormalizer extends ModelContractNormalizer | ||
| { | ||
| /** | ||
| * @param Text $data | ||
| */ | ||
| public function normalize(mixed $data, ?string $format = null, array $context = []): array | ||
| { | ||
| return [[ | ||
| CollectionNormalizer::KEY_CONTENT => [[ | ||
| 'type' => 'text', | ||
| 'text' => $data->getText(), | ||
| ]], | ||
| ]]; | ||
| } | ||
|
|
||
| protected function supportedDataClass(): string | ||
| { | ||
| return Text::class; | ||
| } | ||
|
|
||
| protected function supportsModel(Model $model): bool | ||
| { | ||
| return $model instanceof Voyage && $model->supports(Capability::INPUT_MULTIMODAL); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.