|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +use Symfony\AI\Agent\Agent; |
| 13 | +use Symfony\AI\Agent\InputProcessor\SystemPromptInputProcessor; |
| 14 | +use Symfony\AI\Agent\MultiAgent\Handoff; |
| 15 | +use Symfony\AI\Agent\MultiAgent\MultiAgent; |
| 16 | +use Symfony\AI\Agent\StructuredOutput\AgentProcessor; |
| 17 | +use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory; |
| 18 | +use Symfony\AI\Platform\Message\Message; |
| 19 | +use Symfony\AI\Platform\Message\MessageBag; |
| 20 | + |
| 21 | +require_once dirname(__DIR__).'/bootstrap.php'; |
| 22 | + |
| 23 | +$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client()); |
| 24 | + |
| 25 | +// Create structured output processor for the orchestrator |
| 26 | +$structuredOutputProcessor = new AgentProcessor(); |
| 27 | + |
| 28 | +// Create orchestrator agent for routing decisions |
| 29 | +$orchestrator = new Agent( |
| 30 | + $platform, |
| 31 | + 'gpt-4o-mini', |
| 32 | + [new SystemPromptInputProcessor('You are an intelligent agent orchestrator that routes user questions to specialized agents.'), $structuredOutputProcessor], |
| 33 | + [$structuredOutputProcessor], |
| 34 | + logger: logger() |
| 35 | +); |
| 36 | + |
| 37 | +// Create technical agent for handling technical issues |
| 38 | +$technical = new Agent( |
| 39 | + $platform, |
| 40 | + 'gpt-4o-mini?max_tokens=150', // set max_tokens here to be faster and cheaper |
| 41 | + [new SystemPromptInputProcessor('You are a technical support specialist. Help users resolve bugs, problems, and technical errors.')], |
| 42 | + name: 'technical', |
| 43 | + logger: logger() |
| 44 | +); |
| 45 | + |
| 46 | +// Create general agent for handling any other questions |
| 47 | +$fallback = new Agent( |
| 48 | + $platform, |
| 49 | + 'gpt-4o-mini', |
| 50 | + [new SystemPromptInputProcessor('You are a helpful general assistant. Assist users with any questions or tasks they may have. You should never ever answer technical question.')], |
| 51 | + name: 'fallback', |
| 52 | + logger: logger() |
| 53 | +); |
| 54 | + |
| 55 | +$multiAgent = new MultiAgent( |
| 56 | + orchestrator: $orchestrator, |
| 57 | + handoffs: [ |
| 58 | + new Handoff(to: $technical, when: ['bug', 'problem', 'technical', 'error']), |
| 59 | + ], |
| 60 | + fallback: $fallback, |
| 61 | + logger: logger() |
| 62 | +); |
| 63 | + |
| 64 | +echo "=== Technical Question ===\n"; |
| 65 | +$technicalQuestion = 'I get this error in my php code: "Call to undefined method App\Controller\UserController::getName()" - this is my line of code: $user->getName() where $user is an instance of User entity.'; |
| 66 | +echo "Question: $technicalQuestion\n\n"; |
| 67 | +$messages = new MessageBag(Message::ofUser($technicalQuestion)); |
| 68 | +$result = $multiAgent->call($messages); |
| 69 | +echo 'Answer: '.substr($result->getContent(), 0, 300).'...'.\PHP_EOL.\PHP_EOL; |
| 70 | + |
| 71 | +echo "=== General Question ===\n"; |
| 72 | +$generalQuestion = 'Can you give me a lasagne recipe?'; |
| 73 | +echo "Question: $generalQuestion\n\n"; |
| 74 | +$messages = new MessageBag(Message::ofUser($generalQuestion)); |
| 75 | +$result = $multiAgent->call($messages); |
| 76 | +echo 'Answer: '.substr($result->getContent(), 0, 300).'...'.\PHP_EOL; |
0 commit comments