12
12
namespace Symfony \AI \Agent \Toolbox \Tool ;
13
13
14
14
use Symfony \AI \Agent \Toolbox \Attribute \AsTool ;
15
+ use Symfony \AI \Agent \Toolbox \Source \Source ;
16
+ use Symfony \AI \Agent \Toolbox \Source \UsesSourcesInterface ;
17
+ use Symfony \AI \Agent \Toolbox \Source \UsesSourcesTrait ;
15
18
use Symfony \Contracts \HttpClient \HttpClientInterface ;
16
19
17
20
/**
18
21
* @author Christopher Hertel <mail@christopher-hertel.de>
19
22
*/
20
23
#[AsTool('wikipedia_search ' , description: 'Searches Wikipedia for a given query ' , method: 'search ' )]
21
24
#[AsTool('wikipedia_article ' , description: 'Retrieves a Wikipedia article by its title ' , method: 'article ' )]
22
- final readonly class Wikipedia
25
+ final class Wikipedia implements UsesSourcesInterface
23
26
{
27
+ use UsesSourcesTrait;
28
+
24
29
public function __construct (
25
30
private HttpClientInterface $ httpClient ,
26
31
private string $ locale = 'en ' ,
@@ -81,6 +86,10 @@ public function article(string $title): string
81
86
$ result .= \PHP_EOL ;
82
87
}
83
88
89
+ $ this ->addSource (
90
+ new Source ($ article ['title ' ], $ this ->getUrl ($ article ['title ' ]), $ article ['extract ' ])
91
+ );
92
+
84
93
return $ result .'This is the content of article " ' .$ article ['title ' ].'": ' .\PHP_EOL .$ article ['extract ' ];
85
94
}
86
95
@@ -96,4 +105,9 @@ private function execute(array $query, ?string $locale = null): array
96
105
97
106
return $ response ->toArray ();
98
107
}
108
+
109
+ private function getUrl (string $ title ): string
110
+ {
111
+ return \sprintf ('https://%s.wikipedia.org/wiki/%s ' , $ this ->locale , str_replace (' ' , '_ ' , $ title ));
112
+ }
99
113
}
0 commit comments