Skip to content

Commit c8ff4ec

Browse files
committed
feature #160 [Examples] Improve bootstrapping and logging in examples (chr-hertel)
This PR was merged into the main branch. Discussion ---------- [Examples] Improve bootstrapping and logging in examples | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Docs? | no | Issues | | License | MIT Brings to things: * Easier bootstrapping and env var checks * Enables logging of HTTP calls with `-vv` or tool calls with `-vvv` Sometimes I'd like to see more details of the execution of an example. This would be possible now: <img width="1915" height="594" alt="image" src="https://github.com/user-attachments/assets/25de4bfe-69bb-459b-8331-dd01482040e6" /> What do you think? does it make the examples too complex to read & write - or helpful? (only a draft, would roll that out to all examples if feedback is positive) Commits ------- 611d636 Improve bootstrapping and logging in examples
2 parents d2df16e + 611d636 commit c8ff4ec

File tree

102 files changed

+386
-959
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+386
-959
lines changed

examples/README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ Every example script is a standalone PHP script that can be run from the command
5555
You can run an example by executing the following command:
5656

5757
```bash
58-
php openai/openai/chat.php
58+
php openai/chat.php
59+
```
60+
61+
To get more insights into what is happening at runtime, e.g. HTTP and tool calls, you can add `-vv` or `-vvv`:
62+
63+
```bash
64+
php openai/toolcall-stream.php -vvv
5965
```
6066

6167
### Running examples via the example runner
@@ -70,8 +76,14 @@ You can run the example runner by executing the following command:
7076
./runner
7177
```
7278

73-
If you want to run only examples of a specific subdirectory, you can pass the subdirectory name as an argument:
79+
If you only want to run examples of one or multiple specific subdirectories, you can pass the name as an argument:
80+
81+
```bash
82+
./runner openai mistral
83+
```
84+
85+
If you only want to run a specific subset of examples, you can use a filter option:
7486

7587
```bash
76-
./runner openai
88+
./runner --filter=toolcall
7789
```

examples/albert/chat.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,12 @@
1515
use Symfony\AI\Platform\Message\Message;
1616
use Symfony\AI\Platform\Message\MessageBag;
1717

18-
require_once dirname(__DIR__).'/../vendor/autoload.php';
18+
require_once dirname(__DIR__).'/bootstrap.php';
1919

20-
if (!isset($_SERVER['ALBERT_API_KEY'], $_SERVER['ALBERT_API_URL'])) {
21-
echo 'Please set the ALBERT_API_KEY and ALBERT_API_URL environment variable (e.g., https://your-albert-instance.com/v1).'.\PHP_EOL;
22-
exit(1);
23-
}
24-
25-
$platform = PlatformFactory::create($_SERVER['ALBERT_API_KEY'], $_SERVER['ALBERT_API_URL']);
20+
$platform = PlatformFactory::create(env('ALBERT_API_KEY'), env('ALBERT_API_URL'), http_client());
2621

2722
$model = new GPT('gpt-4o');
28-
$agent = new Agent($platform, $model);
23+
$agent = new Agent($platform, $model, logger: logger());
2924

3025
$documentContext = <<<'CONTEXT'
3126
Document: AI Strategy of France

examples/anthropic/chat.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,13 @@
1414
use Symfony\AI\Platform\Bridge\Anthropic\PlatformFactory;
1515
use Symfony\AI\Platform\Message\Message;
1616
use Symfony\AI\Platform\Message\MessageBag;
17-
use Symfony\Component\Dotenv\Dotenv;
1817

19-
require_once dirname(__DIR__).'/vendor/autoload.php';
20-
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
18+
require_once dirname(__DIR__).'/bootstrap.php';
2119

22-
if (!isset($_SERVER['ANTHROPIC_API_KEY'])) {
23-
echo 'Please set the ANTHROPIC_API_KEY environment variable.'.\PHP_EOL;
24-
exit(1);
25-
}
26-
27-
$platform = PlatformFactory::create($_SERVER['ANTHROPIC_API_KEY']);
20+
$platform = PlatformFactory::create(env('ANTHROPIC_API_KEY'), httpClient: http_client());
2821
$model = new Claude(Claude::SONNET_37);
2922

30-
$agent = new Agent($platform, $model);
23+
$agent = new Agent($platform, $model, logger: logger());
3124
$messages = new MessageBag(
3225
Message::forSystem('You are a pirate and you write funny.'),
3326
Message::ofUser('What is the Symfony framework?'),

examples/anthropic/image-input-binary.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,13 @@
1515
use Symfony\AI\Platform\Message\Content\Image;
1616
use Symfony\AI\Platform\Message\Message;
1717
use Symfony\AI\Platform\Message\MessageBag;
18-
use Symfony\Component\Dotenv\Dotenv;
1918

20-
require_once dirname(__DIR__).'/vendor/autoload.php';
21-
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
19+
require_once dirname(__DIR__).'/bootstrap.php';
2220

23-
if (!isset($_SERVER['ANTHROPIC_API_KEY'])) {
24-
echo 'Please set the ANTHROPIC_API_KEY environment variable.'.\PHP_EOL;
25-
exit(1);
26-
}
27-
28-
$platform = PlatformFactory::create($_SERVER['ANTHROPIC_API_KEY']);
21+
$platform = PlatformFactory::create(env('ANTHROPIC_API_KEY'), httpClient: http_client());
2922
$model = new Claude(Claude::SONNET_37);
3023

31-
$agent = new Agent($platform, $model);
24+
$agent = new Agent($platform, $model, logger: logger());
3225
$messages = new MessageBag(
3326
Message::forSystem('You are an image analyzer bot that helps identify the content of images.'),
3427
Message::ofUser(

examples/anthropic/image-input-url.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,13 @@
1515
use Symfony\AI\Platform\Message\Content\ImageUrl;
1616
use Symfony\AI\Platform\Message\Message;
1717
use Symfony\AI\Platform\Message\MessageBag;
18-
use Symfony\Component\Dotenv\Dotenv;
1918

20-
require_once dirname(__DIR__).'/vendor/autoload.php';
21-
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
19+
require_once dirname(__DIR__).'/bootstrap.php';
2220

23-
if (!isset($_SERVER['ANTHROPIC_API_KEY'])) {
24-
echo 'Please set the ANTHROPIC_API_KEY environment variable.'.\PHP_EOL;
25-
exit(1);
26-
}
27-
28-
$platform = PlatformFactory::create($_SERVER['ANTHROPIC_API_KEY']);
21+
$platform = PlatformFactory::create(env('ANTHROPIC_API_KEY'), httpClient: http_client());
2922
$model = new Claude(Claude::SONNET_37);
3023

31-
$agent = new Agent($platform, $model);
24+
$agent = new Agent($platform, $model, logger: logger());
3225
$messages = new MessageBag(
3326
Message::forSystem('You are an image analyzer bot that helps identify the content of images.'),
3427
Message::ofUser(

examples/anthropic/pdf-input-binary.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,13 @@
1515
use Symfony\AI\Platform\Message\Content\Document;
1616
use Symfony\AI\Platform\Message\Message;
1717
use Symfony\AI\Platform\Message\MessageBag;
18-
use Symfony\Component\Dotenv\Dotenv;
1918

20-
require_once dirname(__DIR__).'/vendor/autoload.php';
21-
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
19+
require_once dirname(__DIR__).'/bootstrap.php';
2220

23-
if (!isset($_SERVER['ANTHROPIC_API_KEY'])) {
24-
echo 'Please set the ANTHROPIC_API_KEY environment variable.'.\PHP_EOL;
25-
exit(1);
26-
}
27-
28-
$platform = PlatformFactory::create($_SERVER['ANTHROPIC_API_KEY']);
21+
$platform = PlatformFactory::create(env('ANTHROPIC_API_KEY'), httpClient: http_client());
2922
$model = new Claude(Claude::SONNET_37);
3023

31-
$agent = new Agent($platform, $model);
24+
$agent = new Agent($platform, $model, logger: logger());
3225
$messages = new MessageBag(
3326
Message::ofUser(
3427
Document::fromFile(dirname(__DIR__, 2).'/fixtures/document.pdf'),

examples/anthropic/pdf-input-url.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,13 @@
1515
use Symfony\AI\Platform\Message\Content\DocumentUrl;
1616
use Symfony\AI\Platform\Message\Message;
1717
use Symfony\AI\Platform\Message\MessageBag;
18-
use Symfony\Component\Dotenv\Dotenv;
1918

20-
require_once dirname(__DIR__).'/vendor/autoload.php';
21-
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
19+
require_once dirname(__DIR__).'/bootstrap.php';
2220

23-
if (!isset($_SERVER['ANTHROPIC_API_KEY'])) {
24-
echo 'Please set the ANTHROPIC_API_KEY environment variable.'.\PHP_EOL;
25-
exit(1);
26-
}
27-
28-
$platform = PlatformFactory::create($_SERVER['ANTHROPIC_API_KEY']);
21+
$platform = PlatformFactory::create(env('ANTHROPIC_API_KEY'), httpClient: http_client());
2922
$model = new Claude(Claude::SONNET_37);
3023

31-
$agent = new Agent($platform, $model);
24+
$agent = new Agent($platform, $model, logger: logger());
3225
$messages = new MessageBag(
3326
Message::ofUser(
3427
new DocumentUrl('https://upload.wikimedia.org/wikipedia/commons/2/20/Re_example.pdf'),

examples/anthropic/stream.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,13 @@
1414
use Symfony\AI\Platform\Bridge\Anthropic\PlatformFactory;
1515
use Symfony\AI\Platform\Message\Message;
1616
use Symfony\AI\Platform\Message\MessageBag;
17-
use Symfony\Component\Dotenv\Dotenv;
1817

19-
require_once dirname(__DIR__).'/vendor/autoload.php';
20-
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
18+
require_once dirname(__DIR__).'/bootstrap.php';
2119

22-
if (!isset($_SERVER['ANTHROPIC_API_KEY'])) {
23-
echo 'Please set the ANTHROPIC_API_KEY environment variable.'.\PHP_EOL;
24-
exit(1);
25-
}
26-
27-
$platform = PlatformFactory::create($_SERVER['ANTHROPIC_API_KEY']);
20+
$platform = PlatformFactory::create(env('ANTHROPIC_API_KEY'), httpClient: http_client());
2821
$model = new Claude();
2922

30-
$agent = new Agent($platform, $model);
23+
$agent = new Agent($platform, $model, logger: logger());
3124
$messages = new MessageBag(
3225
Message::forSystem('You are a thoughtful philosopher.'),
3326
Message::ofUser('What is the purpose of an ant?'),

examples/anthropic/toolcall.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,16 @@
1717
use Symfony\AI\Platform\Bridge\Anthropic\PlatformFactory;
1818
use Symfony\AI\Platform\Message\Message;
1919
use Symfony\AI\Platform\Message\MessageBag;
20-
use Symfony\Component\Dotenv\Dotenv;
21-
use Symfony\Component\HttpClient\HttpClient;
2220

23-
require_once dirname(__DIR__).'/vendor/autoload.php';
24-
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
21+
require_once dirname(__DIR__).'/bootstrap.php';
2522

26-
if (!isset($_SERVER['ANTHROPIC_API_KEY'])) {
27-
echo 'Please set the ANTHROPIC_API_KEY environment variable.'.\PHP_EOL;
28-
exit(1);
29-
}
30-
31-
$platform = PlatformFactory::create($_SERVER['ANTHROPIC_API_KEY']);
23+
$platform = PlatformFactory::create(env('ANTHROPIC_API_KEY'), httpClient: http_client());
3224
$model = new Claude();
3325

34-
$wikipedia = new Wikipedia(HttpClient::create());
35-
$toolbox = new Toolbox([$wikipedia]);
26+
$wikipedia = new Wikipedia(http_client());
27+
$toolbox = new Toolbox([$wikipedia], logger: logger());
3628
$processor = new AgentProcessor($toolbox);
37-
$agent = new Agent($platform, $model, [$processor], [$processor]);
29+
$agent = new Agent($platform, $model, [$processor], [$processor], logger());
3830

3931
$messages = new MessageBag(Message::ofUser('Who is the current chancellor of Germany?'));
4032
$response = $agent->call($messages);

examples/azure/audio-transcript.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,15 @@
1212
use Symfony\AI\Platform\Bridge\Azure\OpenAI\PlatformFactory;
1313
use Symfony\AI\Platform\Bridge\OpenAI\Whisper;
1414
use Symfony\AI\Platform\Message\Content\Audio;
15-
use Symfony\Component\Dotenv\Dotenv;
1615

17-
require_once dirname(__DIR__).'/vendor/autoload.php';
18-
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');
19-
20-
if (!isset($_SERVER['AZURE_OPENAI_BASEURL'], $_SERVER['AZURE_OPENAI_WHISPER_DEPLOYMENT'], $_SERVER['AZURE_OPENAI_WHISPER_API_VERSION'], $_SERVER['AZURE_OPENAI_KEY'])) {
21-
echo 'Please set the AZURE_OPENAI_BASEURL, AZURE_OPENAI_WHISPER_DEPLOYMENT, AZURE_OPENAI_WHISPER_API_VERSION, and AZURE_OPENAI_KEY environment variables.'.\PHP_EOL;
22-
exit(1);
23-
}
16+
require_once dirname(__DIR__).'/bootstrap.php';
2417

2518
$platform = PlatformFactory::create(
26-
$_SERVER['AZURE_OPENAI_BASEURL'],
27-
$_SERVER['AZURE_OPENAI_WHISPER_DEPLOYMENT'],
28-
$_SERVER['AZURE_OPENAI_WHISPER_API_VERSION'],
29-
$_SERVER['AZURE_OPENAI_KEY'],
19+
env('AZURE_OPENAI_BASEURL'),
20+
env('AZURE_OPENAI_WHISPER_DEPLOYMENT'),
21+
env('AZURE_OPENAI_WHISPER_API_VERSION'),
22+
env('AZURE_OPENAI_KEY'),
23+
http_client(),
3024
);
3125
$model = new Whisper();
3226
$file = Audio::fromFile(dirname(__DIR__, 2).'/fixtures/audio.mp3');

0 commit comments

Comments
 (0)