Files
archived-ai/examples/document/vectorizing.php
2026-01-23 21:56:05 +01:00

30 lines
829 B
PHP

<?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\OpenAi\PlatformFactory;
use Symfony\AI\Store\Document\Vectorizer;
require_once dirname(__DIR__).'/bootstrap.php';
$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client());
$vectorizer = new Vectorizer($platform, 'text-embedding-3-large');
$string = 'Hello World';
$vector = $vectorizer->vectorize($string);
printf(
"String: %s\nVector dimensions: %d\nFirst 5 values: [%s]\n",
$string,
$vector->getDimensions(),
implode(', ', array_map(static fn ($val) => number_format($val, 6), array_slice($vector->getData(), 0, 5)))
);