Skip to content

fix example scripts for docker, class names and env var names #79

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 1 commit into from
Jul 8, 2025
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
2 changes: 1 addition & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@
(new PhpCsFixer\Finder())
->in([__DIR__.'/demo', __DIR__.'/examples', __DIR__.'/fixtures', __DIR__.'/src'])
->append([__FILE__])
->exclude(__DIR__.'/demo/var')
->exclude('var')
)
;
16 changes: 16 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ On top, the examples are used as integration tests to ensure that the components

## Running the examples

For setting up and running the examples, you can either run them standalone or via the example runner. You find the
commands for that in this section. Make sure to change into the `examples` directory before running the commands.

```bash
cd examples
```

### Setup

#### Dependencies
Expand All @@ -25,6 +32,15 @@ corresponding example you want to run.

_Now you can run examples standalone or via the example runner._

#### Store with Docker

Some of the store examples require locally running services, meaning that you need to have Docker installed and running
to test these examples.

```bash
docker compose up -d
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
docker compose up -d
cd examples
docker compose up -d

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, yes, we should be explicit here.
I added a section above since this is also true for the composer install and example execution part.

```

### Running examples standalone

Every example script is a standalone PHP script that can be run from the command line.
Expand Down
File renamed without changes.
4 changes: 3 additions & 1 deletion examples/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"async-aws/bedrock-runtime": "^1.1",
"codewithkyrian/transformers": "^0.5.3",
"doctrine/dbal": "^3.3|^4.0",
"mrmysql/youtube-transcript": "^0.0.5",
"php-http/discovery": "^1.20",
"probots-io/pinecone-php": "^1.1",
"psr/http-factory-implementation": "*",
Expand Down Expand Up @@ -38,6 +39,7 @@
"allow-plugins": {
"codewithkyrian/transformers-libsloader": true,
"php-http/discovery": true
}
},
"sort-packages": true
}
}
11 changes: 4 additions & 7 deletions examples/google/embeddings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@

use Symfony\AI\Platform\Bridge\Google\Embeddings;
use Symfony\AI\Platform\Bridge\Google\PlatformFactory;
use Symfony\AI\Platform\Response\VectorResponse;
use Symfony\Component\Dotenv\Dotenv;

require_once dirname(__DIR__).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');

if (empty($_ENV['GOOGLE_API_KEY'])) {
echo 'Please set the GOOGLE_API_KEY environment variable.'.\PHP_EOL;
if (!isset($_ENV['GEMINI_API_KEY'])) {
echo 'Please set the GEMINI_API_KEY environment variable.'.\PHP_EOL;
exit(1);
}

$platform = PlatformFactory::create($_ENV['GOOGLE_API_KEY']);
$platform = PlatformFactory::create($_ENV['GEMINI_API_KEY']);
$embeddings = new Embeddings();

$response = $platform->request($embeddings, <<<TEXT
Expand All @@ -31,6 +30,4 @@
country was very peaceful and prosperous. The people lived happily ever after.
TEXT);

assert($response instanceof VectorResponse);

echo 'Dimensions: '.$response->getContent()[0]->getDimensions().\PHP_EOL;
echo 'Dimensions: '.$response->asVectors()[0]->getDimensions().\PHP_EOL;
26 changes: 13 additions & 13 deletions examples/google/server-tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
* file that was distributed with this source code.
*/

use PhpLlm\LlmChain\Chain\Chain;
use PhpLlm\LlmChain\Chain\Toolbox\ChainProcessor;
use PhpLlm\LlmChain\Chain\Toolbox\Tool\Clock;
use PhpLlm\LlmChain\Chain\Toolbox\Toolbox;
use PhpLlm\LlmChain\Platform\Bridge\Google\Gemini;
use PhpLlm\LlmChain\Platform\Bridge\Google\PlatformFactory;
use PhpLlm\LlmChain\Platform\Message\Message;
use PhpLlm\LlmChain\Platform\Message\MessageBag;
use Symfony\AI\Agent\Agent;
use Symfony\AI\Agent\Toolbox\AgentProcessor;
use Symfony\AI\Agent\Toolbox\Tool\Clock;
use Symfony\AI\Agent\Toolbox\Toolbox;
use Symfony\AI\Platform\Bridge\Google\Gemini;
use Symfony\AI\Platform\Bridge\Google\PlatformFactory;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;
use Symfony\Component\Dotenv\Dotenv;

require_once dirname(__DIR__, 2).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__, 2).'/.env');
require_once dirname(__DIR__).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');

if (!isset($_ENV['GEMINI_API_KEY'])) {
echo 'Please set the GEMINI_API_KEY environment variable.'.\PHP_EOL;
Expand All @@ -33,8 +33,8 @@
$llm = new Gemini('gemini-2.5-pro-preview-03-25', ['server_tools' => ['url_context' => true], 'temperature' => 1.0]);

$toolbox = Toolbox::create(new Clock());
$processor = new ChainProcessor($toolbox);
$chain = new Chain($platform, $llm);
$processor = new AgentProcessor($toolbox);
$agent = new Agent($platform, $llm);

$messages = new MessageBag(
Message::ofUser(
Expand All @@ -44,6 +44,6 @@
),
);

$response = $chain->call($messages);
$response = $agent->call($messages);

echo $response->getContent().\PHP_EOL;
4 changes: 2 additions & 2 deletions examples/google/structured-output-clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
use Symfony\Component\Clock\Clock as SymfonyClock;
use Symfony\Component\Dotenv\Dotenv;

require_once dirname(__DIR__, 2).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__, 2).'/.env');
require_once dirname(__DIR__).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');

if (!isset($_ENV['GEMINI_API_KEY'])) {
echo 'Please set the GEMINI_API_KEY environment variable.'.\PHP_EOL;
Expand Down
4 changes: 2 additions & 2 deletions examples/google/structured-output-math.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
use Symfony\AI\Platform\Message\MessageBag;
use Symfony\Component\Dotenv\Dotenv;

require_once dirname(__DIR__, 2).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__, 2).'/.env');
require_once dirname(__DIR__).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');

if (!isset($_ENV['GEMINI_API_KEY'])) {
echo 'Please set the GEMINI_API_KEY environment variable.'.\PHP_EOL;
Expand Down
4 changes: 2 additions & 2 deletions examples/store/mariadb-similarity-search-gemini.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\Uid\Uuid;

require_once dirname(__DIR__, 2).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__, 2).'/.env');
require_once dirname(__DIR__).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');

if (!isset($_ENV['GEMINI_API_KEY'], $_ENV['MARIADB_URI'])) {
echo 'Please set GEMINI_API_KEY and MARIADB_URI environment variables.'.\PHP_EOL;
Expand Down
12 changes: 7 additions & 5 deletions examples/store/mariadb-similarity-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Tools\DsnParser;
use Symfony\AI\Agent\Agent;
use Symfony\AI\Agent\Toolbox\AgentProcessor;
use Symfony\AI\Agent\Toolbox\Tool\SimilaritySearch;
use Symfony\AI\Agent\Toolbox\Toolbox;
use Symfony\AI\Platform\Bridge\OpenAI\Embeddings;
Expand All @@ -26,8 +28,8 @@
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\Uid\Uuid;

require_once dirname(__DIR__, 2).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__, 2).'/.env');
require_once dirname(__DIR__).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');

if (!isset($_ENV['OPENAI_API_KEY'], $_ENV['MARIADB_URI'])) {
echo 'Please set OPENAI_API_KEY and MARIADB_URI environment variables.'.\PHP_EOL;
Expand Down Expand Up @@ -71,13 +73,13 @@

$similaritySearch = new SimilaritySearch($platform, $embeddings, $store);
$toolbox = Toolbox::create($similaritySearch);
$processor = new ChainProcessor($toolbox);
$chain = new Chain($platform, $model, [$processor], [$processor]);
$processor = new AgentProcessor($toolbox);
$agent = new Agent($platform, $model, [$processor], [$processor]);

$messages = new MessageBag(
Message::forSystem('Please answer all user questions only using SimilaritySearch function.'),
Message::ofUser('Which movie fits the theme of the mafia?')
);
$response = $chain->call($messages);
$response = $agent->call($messages);

echo $response->getContent().\PHP_EOL;
6 changes: 4 additions & 2 deletions examples/store/postgres-similarity-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
use Symfony\AI\Store\Bridge\Postgres\Store;
use Symfony\AI\Store\Document\Metadata;
use Symfony\AI\Store\Document\TextDocument;
use Symfony\AI\Store\Document\Vectorizer;
use Symfony\AI\Store\Indexer;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\Uid\Uuid;

require_once dirname(__DIR__).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');

if (empty($_ENV['OPENAI_API_KEY']) || empty($_ENV['POSTGRES_URI'])) {
if (!isset($_ENV['OPENAI_API_KEY'], $_ENV['POSTGRES_URI'])) {
echo 'Please set OPENAI_API_KEY and POSTGRES_URI environment variables.'.\PHP_EOL;
exit(1);
}
Expand Down Expand Up @@ -64,7 +65,8 @@

// create embeddings for documents
$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']);
$indexer = new Indexer($platform, $embeddings = new Embeddings(), $store);
$vectorizer = new Vectorizer($platform, $embeddings = new Embeddings());
$indexer = new Indexer($vectorizer, $store);
$indexer->index($documents);

$model = new GPT(GPT::GPT_4O_MINI);
Expand Down
2 changes: 1 addition & 1 deletion src/platform/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ This provides several benefits:

::

use PhpLlm\LlmChain\Platform\Message\Message;
use Symfony\AI\Platform\Message\Message;

$message = Message::ofUser('Hello, AI!');

Expand Down
3 changes: 3 additions & 0 deletions src/store/src/Bridge/Postgres/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ private function toPgvector(VectorInterface $vector): string
return '['.implode(',', $vector->getData()).']';
}

/**
* @return float[]
*/
private function fromPgvector(string $vector): array
{
return json_decode($vector, true, 512, \JSON_THROW_ON_ERROR);
Expand Down