|
| 1 | +Symfony AI - Toolbox Component |
| 2 | +=============================== |
| 3 | + |
| 4 | +The Toolbox component provides a collection of third-party tools for AI agents, including web search and knowledge retrieval capabilities. |
| 5 | + |
| 6 | +Installation |
| 7 | +------------ |
| 8 | + |
| 9 | +Install the component using Composer: |
| 10 | + |
| 11 | +.. code-block:: terminal |
| 12 | +
|
| 13 | + $ composer require symfony/ai-toolbox |
| 14 | +
|
| 15 | +Available Tools |
| 16 | +--------------- |
| 17 | + |
| 18 | +The Toolbox component includes ready-to-use tools that can be integrated with AI agents: |
| 19 | + |
| 20 | +Brave Search Tool |
| 21 | +~~~~~~~~~~~~~~~~~ |
| 22 | + |
| 23 | +Provides web search functionality using the Brave Search API:: |
| 24 | + |
| 25 | + use Symfony\AI\Toolbox\Tool\Brave; |
| 26 | + use Symfony\Component\HttpClient\HttpClient; |
| 27 | + |
| 28 | + $httpClient = HttpClient::create(); |
| 29 | + $brave = new Brave($httpClient, 'your-brave-api-key'); |
| 30 | + |
| 31 | + // Search the web |
| 32 | + $results = $brave('latest news about Symfony'); |
| 33 | + |
| 34 | +The Brave tool returns an array of search results with title, description, and URL for each result. |
| 35 | + |
| 36 | +**Configuration Options** |
| 37 | + |
| 38 | +You can pass additional options to customize the search behavior:: |
| 39 | + |
| 40 | + $brave = new Brave($httpClient, $apiKey, [ |
| 41 | + 'country' => 'US', |
| 42 | + 'safesearch' => 'moderate', |
| 43 | + 'freshness' => 'pw', // Past week |
| 44 | + ]); |
| 45 | + |
| 46 | +See the `Brave Search API documentation`_ for all available options. |
| 47 | + |
| 48 | +Wikipedia Tool |
| 49 | +~~~~~~~~~~~~~~ |
| 50 | + |
| 51 | +Provides search and article retrieval from Wikipedia:: |
| 52 | + |
| 53 | + use Symfony\AI\Toolbox\Tool\Wikipedia; |
| 54 | + use Symfony\Component\HttpClient\HttpClient; |
| 55 | + |
| 56 | + $httpClient = HttpClient::create(); |
| 57 | + $wikipedia = new Wikipedia($httpClient); |
| 58 | + |
| 59 | + // Search for articles |
| 60 | + $searchResults = $wikipedia->search('artificial intelligence'); |
| 61 | + |
| 62 | + // Get article content |
| 63 | + $article = $wikipedia->article('Artificial intelligence'); |
| 64 | + |
| 65 | +The Wikipedia tool supports multiple languages by passing a locale parameter:: |
| 66 | + |
| 67 | + $wikipedia = new Wikipedia($httpClient, 'de'); // German Wikipedia |
| 68 | + |
| 69 | +**Tool Methods** |
| 70 | + |
| 71 | +The Wikipedia tool provides two methods that can be used as separate tools: |
| 72 | + |
| 73 | +* ``search``: Search for articles by query |
| 74 | +* ``article``: Retrieve full article content by title |
| 75 | + |
| 76 | +Agent Integration |
| 77 | +----------------- |
| 78 | + |
| 79 | +These tools are designed to work with the `Agent Component`_ and can be registered using the ``#[AsTool]`` attribute:: |
| 80 | + |
| 81 | + use Symfony\AI\Agent\Agent; |
| 82 | + use Symfony\AI\Agent\Toolbox\AgentProcessor; |
| 83 | + use Symfony\AI\Agent\Toolbox\Toolbox; |
| 84 | + use Symfony\AI\Toolbox\Tool\Brave; |
| 85 | + use Symfony\AI\Toolbox\Tool\Wikipedia; |
| 86 | + |
| 87 | + // Initialize HTTP client and tools |
| 88 | + $httpClient = HttpClient::create(); |
| 89 | + $brave = new Brave($httpClient, $braveApiKey); |
| 90 | + $wikipedia = new Wikipedia($httpClient); |
| 91 | + |
| 92 | + // Create toolbox and processor |
| 93 | + $toolbox = new Toolbox([$brave, $wikipedia]); |
| 94 | + $processor = new AgentProcessor($toolbox); |
| 95 | + |
| 96 | + // Create agent with tools |
| 97 | + $agent = new Agent($platform, $model, [$processor], [$processor]); |
| 98 | + |
| 99 | +Tool Configuration |
| 100 | +------------------ |
| 101 | + |
| 102 | +Each tool is automatically configured with the ``#[AsTool]`` attribute: |
| 103 | + |
| 104 | +* **Brave Search**: Registered as ``brave_search`` tool |
| 105 | +* **Wikipedia Search**: Registered as ``wikipedia_search`` tool |
| 106 | +* **Wikipedia Article**: Registered as ``wikipedia_article`` tool |
| 107 | + |
| 108 | +Requirements |
| 109 | +------------ |
| 110 | + |
| 111 | +* PHP 8.2 or higher |
| 112 | +* Symfony HTTP Client component |
| 113 | +* Brave Search API key (for Brave tool only) |
| 114 | + |
| 115 | +The Wikipedia tool requires no API key as it uses the public Wikipedia API. |
| 116 | + |
| 117 | +**Code Examples** |
| 118 | + |
| 119 | +* `Brave Search Example`_ |
| 120 | +* `Wikipedia Search Example`_ |
| 121 | + |
| 122 | +.. _`Brave Search API documentation`: https://api-dashboard.search.brave.com/app/documentation/web-search/query |
| 123 | +.. _`Agent Component`: https://github.com/symfony/ai-agent |
| 124 | +.. _`Brave Search Example`: https://github.com/symfony/ai/blob/main/examples/toolbox/brave.php |
| 125 | +.. _`Wikipedia Search Example`: https://github.com/symfony/ai/blob/main/examples/toolbox/wikipedia.php |
0 commit comments