|
| 1 | +# Symfony AI Bundle |
| 2 | + |
| 3 | +Integration bundle for Symfony AI components. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +composer require symfony/ai-bundle |
| 9 | +``` |
| 10 | + |
| 11 | +## Configuration |
| 12 | + |
| 13 | +### Simple Example with OpenAI |
| 14 | + |
| 15 | +```yaml |
| 16 | +# config/packages/ai.yaml |
| 17 | +ai: |
| 18 | + platform: |
| 19 | + openai: |
| 20 | + api_key: '%env(OPENAI_API_KEY)%' |
| 21 | + agent: |
| 22 | + default: |
| 23 | + model: |
| 24 | + name: 'GPT' |
| 25 | +``` |
| 26 | +
|
| 27 | +### Advanced Example with Anthropic, Azure, Google and multiple agents |
| 28 | +```yaml |
| 29 | +# config/packages/ai.yaml |
| 30 | +ai: |
| 31 | + platform: |
| 32 | + anthropic: |
| 33 | + api_key: '%env(ANTHROPIC_API_KEY)%' |
| 34 | + azure: |
| 35 | + # multiple deployments possible |
| 36 | + gpt_deployment: |
| 37 | + base_url: '%env(AZURE_OPENAI_BASEURL)%' |
| 38 | + deployment: '%env(AZURE_OPENAI_GPT)%' |
| 39 | + api_key: '%env(AZURE_OPENAI_KEY)%' |
| 40 | + api_version: '%env(AZURE_GPT_VERSION)%' |
| 41 | + google: |
| 42 | + api_key: '%env(GOOGLE_API_KEY)%' |
| 43 | + agent: |
| 44 | + rag: |
| 45 | + platform: 'symfony_ai.platform.azure.gpt_deployment' |
| 46 | + structured_output: false # Disables support for "output_structure" option, default is true |
| 47 | + model: |
| 48 | + name: 'GPT' |
| 49 | + version: 'gpt-4o-mini' |
| 50 | + system_prompt: 'You are a helpful assistant that can answer questions.' # The default system prompt of the agent |
| 51 | + include_tools: true # Include tool definitions at the end of the system prompt |
| 52 | + tools: |
| 53 | + # Referencing a service with #[AsTool] attribute |
| 54 | + - 'Symfony\AI\Agent\Toolbox\Tool\SimilaritySearch' |
| 55 | + |
| 56 | + # Referencing a service without #[AsTool] attribute |
| 57 | + - service: 'App\Agent\Tool\CompanyName' |
| 58 | + name: 'company_name' |
| 59 | + description: 'Provides the name of your company' |
| 60 | + method: 'foo' # Optional with default value '__invoke' |
| 61 | + |
| 62 | + # Referencing a agent => agent in agent 🤯 |
| 63 | + - service: 'symfony_ai.agent.research' |
| 64 | + name: 'wikipedia_research' |
| 65 | + description: 'Can research on Wikipedia' |
| 66 | + is_agent: true |
| 67 | + research: |
| 68 | + platform: 'symfony_ai.platform.anthropic' |
| 69 | + model: |
| 70 | + name: 'Claude' |
| 71 | + tools: # If undefined, all tools are injected into the agent, use "tools: false" to disable tools. |
| 72 | + - 'Symfony\AI\Agent\Toolbox\Tool\Wikipedia' |
| 73 | + fault_tolerant_toolbox: false # Disables fault tolerant toolbox, default is true |
| 74 | + store: |
| 75 | + # also azure_search, mongodb and pinecone are supported as store type |
| 76 | + chroma_db: |
| 77 | + # multiple collections possible per type |
| 78 | + default: |
| 79 | + collection: 'my_collection' |
| 80 | + embedder: |
| 81 | + default: |
| 82 | + # platform: 'symfony_ai.platform.anthropic' |
| 83 | + # store: 'symfony_ai.store.chroma_db.default' |
| 84 | + model: |
| 85 | + name: 'Embeddings' |
| 86 | + version: 'text-embedding-ada-002' |
| 87 | +``` |
| 88 | +
|
| 89 | +## Usage |
| 90 | +
|
| 91 | +### Agent Service |
| 92 | +
|
| 93 | +Use the `Agent` service to leverage models and tools: |
| 94 | +```php |
| 95 | +use Symfony\AI\Agent\AgentInterface; |
| 96 | +use Symfony\AI\Platform\Message\Message; |
| 97 | +use Symfony\AI\Platform\Message\MessageBag; |
| 98 | +
|
| 99 | +final readonly class MyService |
| 100 | +{ |
| 101 | + public function __construct( |
| 102 | + private AgentInterface $agent, |
| 103 | + ) { |
| 104 | + } |
| 105 | +
|
| 106 | + public function submit(string $message): string |
| 107 | + { |
| 108 | + $messages = new MessageBag( |
| 109 | + Message::forSystem('Speak like a pirate.'), |
| 110 | + Message::ofUser($message), |
| 111 | + ); |
| 112 | +
|
| 113 | + return $this->agent->call($messages); |
| 114 | + } |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +### Register Tools |
| 119 | + |
| 120 | +To use existing tools, you can register them as a service: |
| 121 | +```yaml |
| 122 | +services: |
| 123 | + _defaults: |
| 124 | + autowire: true |
| 125 | + autoconfigure: true |
| 126 | +
|
| 127 | + Symfony\AI\Agent\Toolbox\Tool\Clock: ~ |
| 128 | + Symfony\AI\Agent\Toolbox\Tool\OpenMeteo: ~ |
| 129 | + Symfony\AI\Agent\Toolbox\Tool\SerpApi: |
| 130 | + $apiKey: '%env(SERP_API_KEY)%' |
| 131 | + Symfony\AI\Agent\Toolbox\Tool\SimilaritySearch: ~ |
| 132 | + Symfony\AI\Agent\Toolbox\Tool\Tavily: |
| 133 | + $apiKey: '%env(TAVILY_API_KEY)%' |
| 134 | + Symfony\AI\Agent\Toolbox\Tool\Wikipedia: ~ |
| 135 | + Symfony\AI\Agent\Toolbox\Tool\YouTubeTranscriber: ~ |
| 136 | +``` |
| 137 | + |
| 138 | +Custom tools can be registered by using the `#[AsTool]` attribute: |
| 139 | + |
| 140 | +```php |
| 141 | +use Symfony\AI\Agent\Toolbox\Attribute\AsTool; |
| 142 | +
|
| 143 | +#[AsTool('company_name', 'Provides the name of your company')] |
| 144 | +final class CompanyName |
| 145 | +{ |
| 146 | + public function __invoke(): string |
| 147 | + { |
| 148 | + return 'ACME Corp.' |
| 149 | + } |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +The agent configuration by default will inject all known tools into the agent. |
| 154 | + |
| 155 | +To disable this behavior, set the `tools` option to `false`: |
| 156 | +```yaml |
| 157 | +ai: |
| 158 | + agent: |
| 159 | + my_agent: |
| 160 | + tools: false |
| 161 | +``` |
| 162 | + |
| 163 | +To inject only specific tools, list them in the configuration: |
| 164 | +```yaml |
| 165 | +ai: |
| 166 | + agent: |
| 167 | + my_agent: |
| 168 | + tools: |
| 169 | + - 'Symfony\AI\Agent\Toolbox\Tool\SimilaritySearch' |
| 170 | +``` |
| 171 | + |
| 172 | +### Profiler |
| 173 | + |
| 174 | +The profiler panel provides insights into the agent's execution: |
| 175 | + |
| 176 | + |
0 commit comments