|
| 1 | +<?php |
| 2 | + |
| 3 | +use PhpLlm\LlmChain\Bridge\OpenAI\GPT; |
| 4 | +use PhpLlm\LlmChain\Bridge\OpenAI\PlatformFactory; |
| 5 | +use PhpLlm\LlmChain\Chain; |
| 6 | +use PhpLlm\LlmChain\Chain\InputProcessor\SystemPromptInputProcessor; |
| 7 | +use PhpLlm\LlmChain\Chain\ToolBox\Tool\Clock; |
| 8 | +use PhpLlm\LlmChain\Model\Message\Message; |
| 9 | +use PhpLlm\LlmChain\Model\Message\MessageBag; |
| 10 | +use Symfony\Component\Dotenv\Dotenv; |
| 11 | +use Symfony\Component\Clock\Clock as SymfonyClock; |
| 12 | +use PhpLlm\LlmChain\Chain\ToolBox\ToolAnalyzer; |
| 13 | +use PhpLlm\LlmChain\Chain\ToolBox\ToolBox; |
| 14 | +use PhpLlm\LlmChain\Chain\ToolBox\ChainProcessor; |
| 15 | + |
| 16 | +require_once dirname(__DIR__).'/vendor/autoload.php'; |
| 17 | +(new Dotenv())->loadEnv(dirname(__DIR__).'/.env'); |
| 18 | + |
| 19 | +if (empty($_ENV['OPENAI_API_KEY'])) { |
| 20 | + echo 'Please set the OPENAI_API_KEY environment variable.'.PHP_EOL; |
| 21 | + exit(1); |
| 22 | +} |
| 23 | + |
| 24 | +$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']); |
| 25 | +$llm = new GPT(GPT::GPT_4O_MINI); |
| 26 | + |
| 27 | +$clock = new Clock(new SymfonyClock()); |
| 28 | +$toolBox = new ToolBox(new ToolAnalyzer(), [$clock]); |
| 29 | + |
| 30 | +$systemPromptProcessor = new SystemPromptInputProcessor( |
| 31 | + 'You are Yoda and write like he speaks. But short.', |
| 32 | + $toolBox, |
| 33 | + includeToolDefinitions: true |
| 34 | +); |
| 35 | + |
| 36 | +$processor = new ChainProcessor($toolBox); |
| 37 | +$chain = new Chain($platform, $llm, [$processor, $systemPromptProcessor], [$processor]); |
| 38 | +$messages = new MessageBag(Message::ofUser('What is the meaning of life?')); |
| 39 | + |
| 40 | +$response = $chain->call($messages); |
| 41 | + |
| 42 | +echo $response->getContent().PHP_EOL; |
0 commit comments