-
-
Notifications
You must be signed in to change notification settings - Fork 102
[Agent] Add various tools for enhanced functionality #549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
matyo91
wants to merge
1
commit into
symfony:main
Choose a base branch
from
matyo91:agent/add-tools
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+78,068
โ1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,257 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\AI\Agent\Toolbox\Tool; | ||
|
||
use Symfony\AI\Agent\Toolbox\Attribute\AsTool; | ||
use Symfony\AI\Platform\Contract\JsonSchema\Attribute\With; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
/** | ||
* @author Mathieu Ledru <[email protected]> | ||
*/ | ||
#[AsTool('arxiv_search', 'Tool that searches the ArXiv API for scientific papers')] | ||
final readonly class ArXiv | ||
{ | ||
/** | ||
* @param array<string, mixed> $options Additional search options | ||
*/ | ||
public function __construct( | ||
private HttpClientInterface $httpClient, | ||
private array $options = [], | ||
) { | ||
} | ||
|
||
/** | ||
* @param string $query search query to look up on ArXiv | ||
* @param int $maxResults The maximum number of results to return | ||
* @param int $start The starting position for results (for pagination) | ||
* | ||
* @return array<int, array{ | ||
* id: string, | ||
* title: string, | ||
* authors: array<int, string>, | ||
* summary: string, | ||
* published: string, | ||
* updated: string, | ||
* categories: array<int, string>, | ||
* link: string, | ||
* }> | ||
*/ | ||
public function __invoke( | ||
#[With(maximum: 500)] | ||
string $query, | ||
int $maxResults = 10, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minimum 1? |
||
#[With(minimum: 0)] | ||
int $start = 0, | ||
): array { | ||
try { | ||
// Check if query is an ArXiv identifier | ||
if ($this->isArxivIdentifier($query)) { | ||
return $this->searchById($query); | ||
} | ||
|
||
return $this->searchByQuery($query, $maxResults, $start); | ||
} catch (\Exception $e) { | ||
return [ | ||
[ | ||
'id' => 'error', | ||
'title' => 'Search Error', | ||
'authors' => [], | ||
'summary' => 'Unable to perform ArXiv search: '.$e->getMessage(), | ||
'published' => '', | ||
'updated' => '', | ||
'categories' => [], | ||
'link' => '', | ||
], | ||
]; | ||
} | ||
} | ||
|
||
/** | ||
* Check if a query is an ArXiv identifier. | ||
*/ | ||
private function isArxivIdentifier(string $query): bool | ||
{ | ||
// ArXiv identifier patterns | ||
$patterns = [ | ||
'/^\d{4}\.\d{4,5}(v\d+)?$/', // YYMM.NNNN or YYMM.NNNNvN | ||
'/^\d{7}\.\d+$/', // YYMMNNN.N | ||
'/^[a-z-]+\/\d{7}$/', // category/YYMMNNN | ||
]; | ||
|
||
foreach ($patterns as $pattern) { | ||
if (preg_match($pattern, $query)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Search ArXiv by specific paper ID. | ||
* | ||
* @return array<int, array{ | ||
* id: string, | ||
* title: string, | ||
* authors: array<int, string>, | ||
* summary: string, | ||
* published: string, | ||
* updated: string, | ||
* categories: array<int, string>, | ||
* link: string, | ||
* }> | ||
*/ | ||
private function searchById(string $id): array | ||
{ | ||
$response = $this->httpClient->request('GET', 'http://export.arxiv.org/api/query', [ | ||
'query' => [ | ||
'id_list' => $id, | ||
'max_results' => 1, | ||
], | ||
]); | ||
|
||
$xml = $response->getContent(); | ||
|
||
return $this->parseArxivXml($xml); | ||
} | ||
|
||
/** | ||
* Search ArXiv by query string. | ||
* | ||
* @return array<int, array{ | ||
* id: string, | ||
* title: string, | ||
* authors: array<int, string>, | ||
* summary: string, | ||
* published: string, | ||
* updated: string, | ||
* categories: array<int, string>, | ||
* link: string, | ||
* }> | ||
*/ | ||
private function searchByQuery(string $query, int $maxResults, int $start): array | ||
{ | ||
$response = $this->httpClient->request('GET', 'http://export.arxiv.org/api/query', [ | ||
'query' => array_merge($this->options, [ | ||
'search_query' => $query, | ||
'start' => $start, | ||
'max_results' => $maxResults, | ||
'sortBy' => 'relevance', | ||
'sortOrder' => 'descending', | ||
]), | ||
]); | ||
|
||
$xml = $response->getContent(); | ||
|
||
return $this->parseArxivXml($xml); | ||
} | ||
|
||
/** | ||
* Parse ArXiv XML response. | ||
* | ||
* @return array<int, array{ | ||
* id: string, | ||
* title: string, | ||
* authors: array<int, string>, | ||
* summary: string, | ||
* published: string, | ||
* updated: string, | ||
* categories: array<int, string>, | ||
* link: string, | ||
* }> | ||
*/ | ||
private function parseArxivXml(string $xml): array | ||
{ | ||
try { | ||
$dom = new \DOMDocument(); | ||
$dom->loadXML($xml); | ||
|
||
$entries = $dom->getElementsByTagName('entry'); | ||
$results = []; | ||
|
||
foreach ($entries as $entry) { | ||
$id = $this->getElementText($entry, 'id'); | ||
$title = $this->getElementText($entry, 'title'); | ||
$summary = $this->getElementText($entry, 'summary'); | ||
$published = $this->getElementText($entry, 'published'); | ||
$updated = $this->getElementText($entry, 'updated'); | ||
|
||
// Extract authors | ||
$authors = []; | ||
$authorNodes = $entry->getElementsByTagName('author'); | ||
foreach ($authorNodes as $authorNode) { | ||
$name = $this->getElementText($authorNode, 'name'); | ||
if ($name) { | ||
$authors[] = $name; | ||
} | ||
} | ||
|
||
// Extract categories | ||
$categories = []; | ||
$categoryNodes = $entry->getElementsByTagName('category'); | ||
foreach ($categoryNodes as $categoryNode) { | ||
$term = $categoryNode->getAttribute('term'); | ||
if ($term) { | ||
$categories[] = $term; | ||
} | ||
} | ||
|
||
// Extract link | ||
$link = ''; | ||
$linkNodes = $entry->getElementsByTagName('link'); | ||
foreach ($linkNodes as $linkNode) { | ||
if ('pdf' === $linkNode->getAttribute('title')) { | ||
$link = $linkNode->getAttribute('href'); | ||
break; | ||
} | ||
} | ||
|
||
$results[] = [ | ||
'id' => $id, | ||
'title' => $title, | ||
'authors' => $authors, | ||
'summary' => trim($summary), | ||
'published' => $published, | ||
'updated' => $updated, | ||
'categories' => $categories, | ||
'link' => $link, | ||
]; | ||
} | ||
|
||
return $results; | ||
} catch (\Exception $e) { | ||
return [ | ||
[ | ||
'id' => 'parse_error', | ||
'title' => 'Parse Error', | ||
'authors' => [], | ||
'summary' => 'Unable to parse ArXiv response: '.$e->getMessage(), | ||
'published' => '', | ||
'updated' => '', | ||
'categories' => [], | ||
'link' => '', | ||
], | ||
]; | ||
} | ||
} | ||
|
||
private function getElementText(\DOMElement $parent, string $tagName): string | ||
{ | ||
$elements = $parent->getElementsByTagName($tagName); | ||
if ($elements->length > 0) { | ||
return $elements->item(0)->textContent ?? ''; | ||
} | ||
|
||
return ''; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maximum length? in this case
maximum
is not right