Skip to content

Commit 3ab2764

Browse files
authored
chore: improve example execution with make and dotenv (#54)
1 parent 9977e78 commit 3ab2764

16 files changed

+81
-18
lines changed

.env

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# You only need to fill in the values when running the examples, see examples/
2+
3+
# For using GPT on OpenAI
4+
OPENAI_API_KEY=
5+
6+
# For using Claude on Anthropic
7+
ANTHROPIC_API_KEY=
8+
9+
# For using GPT on Azure
10+
AZURE_OPENAI_BASEURL=
11+
AZURE_OPENAI_DEPLOYMENT=
12+
AZURE_OPENAI_VERSION=
13+
AZURE_OPENAI_KEY=
14+
15+
# For using SerpApi (tool)
16+
SERP_API_KEY=

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ vendor
33
.php-cs-fixer.cache
44
.phpunit.cache
55
coverage
6+
.env.local

Makefile

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,19 @@ qa-lowest:
99
vendor/bin/php-cs-fixer fix --diff --verbose
1010
vendor/bin/phpstan
1111
vendor/bin/phpunit
12-
git restore composer.lock
1312

1413
coverage:
1514
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html=coverage
15+
16+
run-all-examples:
17+
php ./examples/chat-claude-anthropic.php
18+
php ./examples/chat-gpt-azure.php
19+
php ./examples/chat-gpt-openai.php
20+
php ./examples/image-describer.php
21+
php ./examples/reasoning-openai.php
22+
php ./examples/structured-output-math.php
23+
php ./examples/toolbox-clock.php
24+
php ./examples/toolbox-serpapi.php
25+
php ./examples/toolbox-weather.php
26+
php ./examples/toolbox-wikipedia.php
27+
php ./examples/toolbox-youtube.php

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ Usage Examples
6262
--------------
6363

6464
See [examples](examples) to run example implementations using this library.
65-
Depending on the example you need to export different environment variables for API keys or deployment configurations:
65+
Depending on the example you need to export different environment variables
66+
for API keys or deployment configurations or create a `.env.local` based on `.env` file.
67+
68+
To run all examples, just use `make run-all-examples`.
6669

6770
### Chat Examples
6871

@@ -120,13 +123,21 @@ Depending on the example you need to export different environment variables for
120123
php examples/toolbox-youtube.php
121124
```
122125

123-
### Structured Output
126+
### Structured Output Example
124127

125128
1. Structured Output Example: OpenAI's GPT
126129
```bash
127130
export OPENAI_API_KEY=sk-...
128131
php examples/structured-output-math.php
129132
```
133+
134+
### Reasoning Example
135+
136+
1. Reasoning Example: OpenAI's o1
137+
```bash
138+
export OPENAI_API_KEY=sk-...
139+
php examples/reasoning-openai.php
140+
```
130141

131142
Contributions
132143
-------------

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"symfony/console": "^6.4 || ^7.1",
3535
"symfony/css-selector": "^6.4 || ^7.1",
3636
"symfony/dom-crawler": "^6.4 || ^7.1",
37+
"symfony/dotenv": "^6.4 || ^7.1",
3738
"symfony/var-dumper": "^6.4 || ^7.1"
3839
},
3940
"suggest": {

examples/chat-claude-anthropic.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
use PhpLlm\LlmChain\Chain;
66
use PhpLlm\LlmChain\Message\Message;
77
use PhpLlm\LlmChain\Message\MessageBag;
8+
use Symfony\Component\Dotenv\Dotenv;
89
use Symfony\Component\HttpClient\HttpClient;
910

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

12-
$platform = new Anthropic(HttpClient::create(), getenv('ANTHROPIC_API_KEY'));
14+
$platform = new Anthropic(HttpClient::create(), $_ENV['ANTHROPIC_API_KEY']);
1315
$llm = new Claude($platform);
1416

1517
$chain = new Chain($llm);

examples/chat-gpt-azure.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
use PhpLlm\LlmChain\OpenAI\Model\Gpt;
77
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version;
88
use PhpLlm\LlmChain\OpenAI\Platform\Azure;
9+
use Symfony\Component\Dotenv\Dotenv;
910
use Symfony\Component\HttpClient\HttpClient;
1011

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

1315
$platform = new Azure(HttpClient::create(),
14-
getenv('AZURE_OPENAI_BASEURL'),
15-
getenv('AZURE_OPENAI_DEPLOYMENT'),
16-
getenv('AZURE_OPENAI_VERSION'),
17-
getenv('AZURE_OPENAI_KEY')
16+
$_ENV['AZURE_OPENAI_BASEURL'],
17+
$_ENV['AZURE_OPENAI_DEPLOYMENT'],
18+
$_ENV['AZURE_OPENAI_VERSION'],
19+
$_ENV['AZURE_OPENAI_KEY'],
1820
);
1921
$llm = new Gpt($platform, Version::gpt4oMini());
2022

examples/chat-gpt-openai.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
use PhpLlm\LlmChain\OpenAI\Model\Gpt;
77
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version;
88
use PhpLlm\LlmChain\OpenAI\Platform\OpenAI;
9+
use Symfony\Component\Dotenv\Dotenv;
910
use Symfony\Component\HttpClient\HttpClient;
1011

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

13-
$platform = new OpenAI(HttpClient::create(), getenv('OPENAI_API_KEY'));
15+
$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
1416
$llm = new Gpt($platform, Version::gpt4oMini(), [
1517
'temperature' => 0.5, // default options for the model
1618
]);

examples/image-describer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
use PhpLlm\LlmChain\OpenAI\Model\Gpt;
88
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version;
99
use PhpLlm\LlmChain\OpenAI\Platform\OpenAI;
10+
use Symfony\Component\Dotenv\Dotenv;
1011
use Symfony\Component\HttpClient\HttpClient;
1112

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

14-
$platform = new OpenAI(HttpClient::create(), getenv('OPENAI_API_KEY'));
16+
$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
1517
$llm = new Gpt($platform, Version::gpt4oMini());
1618

1719
$chain = new Chain($llm);

examples/reasoning-openai.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
use PhpLlm\LlmChain\OpenAI\Model\Gpt;
77
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version;
88
use PhpLlm\LlmChain\OpenAI\Platform\OpenAI;
9+
use Symfony\Component\Dotenv\Dotenv;
910
use Symfony\Component\HttpClient\HttpClient;
1011

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

13-
$platform = new OpenAI(HttpClient::create(), getenv('OPENAI_API_KEY'));
15+
$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
1416
$llm = new Gpt($platform, Version::o1Preview());
1517

1618
$prompt = <<<PROMPT

examples/structured-output-math.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99
use PhpLlm\LlmChain\StructuredOutput\ResponseFormatFactory;
1010
use PhpLlm\LlmChain\StructuredOutput\SchemaFactory;
1111
use PhpLlm\LlmChain\Tests\StructuredOutput\Data\MathReasoning;
12+
use Symfony\Component\Dotenv\Dotenv;
1213
use Symfony\Component\HttpClient\HttpClient;
1314
use Symfony\Component\Serializer\Encoder\JsonEncoder;
1415
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
1516
use Symfony\Component\Serializer\Serializer;
1617

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

19-
$platform = new OpenAI(HttpClient::create(), getenv('OPENAI_API_KEY'));
21+
$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
2022
$llm = new Gpt($platform, Version::gpt4oMini());
2123
$responseFormatFactory = new ResponseFormatFactory(SchemaFactory::create());
2224
$serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]);

examples/toolbox-clock.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
use PhpLlm\LlmChain\ToolBox\ToolAnalyzer;
1111
use PhpLlm\LlmChain\ToolBox\ToolBox;
1212
use Symfony\Component\Clock\Clock as SymfonyClock;
13+
use Symfony\Component\Dotenv\Dotenv;
1314
use Symfony\Component\HttpClient\HttpClient;
1415

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

17-
$platform = new OpenAI(HttpClient::create(), getenv('OPENAI_API_KEY'));
19+
$platform = new OpenAI(HttpClient::create(), $_ENV['OPENAI_API_KEY']);
1820
$llm = new Gpt($platform, Version::gpt4oMini());
1921

2022
$clock = new Clock(new SymfonyClock());

examples/toolbox-serpapi.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@
99
use PhpLlm\LlmChain\ToolBox\Tool\SerpApi;
1010
use PhpLlm\LlmChain\ToolBox\ToolAnalyzer;
1111
use PhpLlm\LlmChain\ToolBox\ToolBox;
12+
use Symfony\Component\Dotenv\Dotenv;
1213
use Symfony\Component\HttpClient\HttpClient;
1314

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

1618
$httpClient = HttpClient::create();
17-
$platform = new OpenAI($httpClient, getenv('OPENAI_API_KEY'));
19+
$platform = new OpenAI($httpClient, $_ENV['OPENAI_API_KEY']);
1820
$llm = new Gpt($platform, Version::gpt4oMini());
1921

20-
$serpApi = new SerpApi($httpClient, getenv('SERP_API_KEY'));
22+
$serpApi = new SerpApi($httpClient, $_ENV['SERP_API_KEY']);
2123
$toolBox = new ToolBox(new ToolAnalyzer(), [$serpApi]);
2224
$chain = new Chain($llm, $toolBox);
2325

examples/toolbox-weather.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
use PhpLlm\LlmChain\ToolBox\Tool\OpenMeteo;
1010
use PhpLlm\LlmChain\ToolBox\ToolAnalyzer;
1111
use PhpLlm\LlmChain\ToolBox\ToolBox;
12+
use Symfony\Component\Dotenv\Dotenv;
1213
use Symfony\Component\HttpClient\HttpClient;
1314

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

1618
$httpClient = HttpClient::create();
17-
$platform = new OpenAI($httpClient, getenv('OPENAI_API_KEY'));
19+
$platform = new OpenAI($httpClient, $_ENV['OPENAI_API_KEY']);
1820
$llm = new Gpt($platform, Version::gpt4oMini());
1921

2022
$wikipedia = new OpenMeteo($httpClient);

examples/toolbox-wikipedia.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
use PhpLlm\LlmChain\ToolBox\Tool\Wikipedia;
1010
use PhpLlm\LlmChain\ToolBox\ToolAnalyzer;
1111
use PhpLlm\LlmChain\ToolBox\ToolBox;
12+
use Symfony\Component\Dotenv\Dotenv;
1213
use Symfony\Component\HttpClient\HttpClient;
1314

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

1618
$httpClient = HttpClient::create();
17-
$platform = new OpenAI($httpClient, getenv('OPENAI_API_KEY'));
19+
$platform = new OpenAI($httpClient, $_ENV['OPENAI_API_KEY']);
1820
$llm = new Gpt($platform, Version::gpt4oMini());
1921

2022
$wikipedia = new Wikipedia($httpClient);

examples/toolbox-youtube.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
use PhpLlm\LlmChain\ToolBox\Tool\YouTubeTranscriber;
1010
use PhpLlm\LlmChain\ToolBox\ToolAnalyzer;
1111
use PhpLlm\LlmChain\ToolBox\ToolBox;
12+
use Symfony\Component\Dotenv\Dotenv;
1213
use Symfony\Component\HttpClient\HttpClient;
1314

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

1618
$httpClient = HttpClient::create();
17-
$platform = new OpenAI($httpClient, getenv('OPENAI_API_KEY'));
19+
$platform = new OpenAI($httpClient, $_ENV['OPENAI_API_KEY']);
1820
$llm = new Gpt($platform, Version::gpt4oMini());
1921

2022
$transcriber = new YouTubeTranscriber($httpClient);

0 commit comments

Comments
 (0)