This project is a demonstration of the best practices for using the symfony/ai-agent component in a Symfony 7.4 application, as inspired by the official Symfony AI initiative. It showcases how to build robust, testable, and maintainable AI features with clean architecture.
- Dependency Injection: Agents are configured in YAML and injected into services.
- Type-Safe Tools: Demonstrates the
#[AsTool]attribute with strictly typed parameters using PHP Enums. - Dynamic Context Injection: Shows how to use an
InputProcessorto dynamically add context to an AI prompt. - Testability: Includes a unit test using the
MockAgentto test AI-integrated services without making real API calls.
-
Clone the repository:
git clone https://github.com/mattleads/AIAgentSample.git cd AIAgentSample -
Install dependencies using Composer:
composer install
-
Create a local environment file: Copy the distributed environment file
.envto.env.local. This file is ignored by Git and will store your secret API keys.cp .env .env.local
-
Set your OpenAI API Key: Open the
.env.localfile and replace the placeholder value with your actual OpenAI API key.# .env.local OPENAI_API_KEY=sk-proj-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
This project includes several console commands to demonstrate the different features of the symfony/ai-agent component.
This command tests the default AI agent configured in config/packages/ai.yaml.
Command:
php bin/console app:test-agentExpected Output: You will see a direct response from the LLM based on the prompt "Hello Symfony!".
Hello there! I'm a helpful Symfony assistant, ready to assist you with your technical questions. How can I help you today?
This command demonstrates how the AI can use a custom tool to perform an action. The agent will interpret the natural language prompt, identify the correct tool (get_order_status), and call it with the provided parameters.
Command:
php bin/console app:test-tool 12345 euExpected Output:
The agent will use the OrderStatusTool to retrieve the fictional order status.
Prompting agent: What is the status of order 12345 in the eu region?
Agent response:
Order 12345 in region eu is currently: SHIPPED
This command demonstrates how the UserContextProcessor can dynamically inject information into the prompt before it is sent to the AI. The command simulates a logged-in user to provide context.
Command:
php bin/console app:test-contextExpected Output: The AI will respond with knowledge of the user's identity, which was provided by the context processor.
Simulated logging in user: cli.user@example.com
Prompting agent: Who am I?
Agent response:
You are cli.user@example.com (ID: 1).
Note: The UserContextProcessor added the user info to the AI prompt behind the scenes.
This project includes a unit test for the SupportAssistant service that uses a MockAgent to avoid making real API calls.
To run the test suite, execute the following command:
vendor/bin/phpunit