-
-
Notifications
You must be signed in to change notification settings - Fork 116
[Agent][Toolbox] Add Mapbox.com geocoding tool for address-to-coordinates conversion #500
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
Merged
OskarStark
merged 1 commit into
symfony:main
from
OskarStark:feature/add-mapbox-geocoding-tool
Sep 10, 2025
Merged
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
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
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
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,34 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| use Symfony\AI\Agent\Agent; | ||
| use Symfony\AI\Agent\Toolbox\AgentProcessor; | ||
| use Symfony\AI\Agent\Toolbox\Tool\Mapbox; | ||
| use Symfony\AI\Agent\Toolbox\Toolbox; | ||
| use Symfony\AI\Platform\Bridge\OpenAi\Gpt; | ||
| use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory; | ||
| use Symfony\AI\Platform\Message\Message; | ||
| use Symfony\AI\Platform\Message\MessageBag; | ||
|
|
||
| require_once dirname(__DIR__).'/bootstrap.php'; | ||
|
|
||
| $platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client()); | ||
| $model = new Gpt(Gpt::GPT_4O_MINI); | ||
|
|
||
| $mapbox = new Mapbox(http_client(), env('MAPBOX_ACCESS_TOKEN')); | ||
| $toolbox = new Toolbox([$mapbox], logger: logger()); | ||
| $processor = new AgentProcessor($toolbox); | ||
| $agent = new Agent($platform, $model, [$processor], [$processor], logger()); | ||
|
|
||
| $messages = new MessageBag(Message::ofUser('What are the coordinates of Brandenburg Gate in Berlin?')); | ||
| $result = $agent->call($messages); | ||
|
|
||
| echo $result->getContent().\PHP_EOL; |
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,34 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| use Symfony\AI\Agent\Agent; | ||
| use Symfony\AI\Agent\Toolbox\AgentProcessor; | ||
| use Symfony\AI\Agent\Toolbox\Tool\Mapbox; | ||
| use Symfony\AI\Agent\Toolbox\Toolbox; | ||
| use Symfony\AI\Platform\Bridge\OpenAi\Gpt; | ||
| use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory; | ||
| use Symfony\AI\Platform\Message\Message; | ||
| use Symfony\AI\Platform\Message\MessageBag; | ||
|
|
||
| require_once dirname(__DIR__).'/bootstrap.php'; | ||
|
|
||
| $platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client()); | ||
| $model = new Gpt(Gpt::GPT_4O_MINI); | ||
|
|
||
| $mapbox = new Mapbox(http_client(), env('MAPBOX_ACCESS_TOKEN')); | ||
| $toolbox = new Toolbox([$mapbox], logger: logger()); | ||
| $processor = new AgentProcessor($toolbox); | ||
| $agent = new Agent($platform, $model, [$processor], [$processor], logger()); | ||
|
|
||
| $messages = new MessageBag(Message::ofUser('What address is at coordinates longitude -73.985131, latitude 40.758895?')); | ||
| $result = $agent->call($messages); | ||
|
|
||
| echo $result->getContent().\PHP_EOL; | ||
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
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
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,159 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * 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 Oskar Stark <oskarstark@googlemail.com> | ||
| */ | ||
| #[AsTool(name: 'geocode', description: 'Convert addresses to coordinates using Mapbox Geocoding API', method: 'geocode')] | ||
| #[AsTool(name: 'reverse_geocode', description: 'Convert coordinates to addresses using Mapbox Reverse Geocoding API', method: 'reverseGeocode')] | ||
| final readonly class Mapbox | ||
| { | ||
| public function __construct( | ||
| private HttpClientInterface $httpClient, | ||
| #[\SensitiveParameter] private string $accessToken, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @param string $address The address to geocode (e.g., "1600 Pennsylvania Ave, Washington DC") | ||
| * @param int $limit Maximum number of results to return (1-10) | ||
| * | ||
| * @return array{ | ||
| * results: array<array{ | ||
| * address: string, | ||
| * coordinates: array{longitude: float, latitude: float}, | ||
| * relevance: float, | ||
| * place_type: string[] | ||
| * }>, | ||
| * count: int | ||
| * } | ||
| */ | ||
| public function geocode( | ||
| string $address, | ||
| #[With(minimum: 1, maximum: 10)] | ||
| int $limit = 1, | ||
| ): array { | ||
| $response = $this->httpClient->request('GET', 'https://api.mapbox.com/geocoding/v5/mapbox.places/'.urlencode($address).'.json', [ | ||
| 'query' => [ | ||
| 'access_token' => $this->accessToken, | ||
| 'limit' => $limit, | ||
| ], | ||
| ]); | ||
|
|
||
| $data = $response->toArray(); | ||
|
|
||
| if (!isset($data['features']) || [] === $data['features']) { | ||
| return [ | ||
| 'results' => [], | ||
| 'count' => 0, | ||
| ]; | ||
| } | ||
|
|
||
| $results = []; | ||
| foreach ($data['features'] as $feature) { | ||
| $center = $feature['center'] ?? [0.0, 0.0]; | ||
| $results[] = [ | ||
| 'address' => $feature['place_name'] ?? '', | ||
| 'coordinates' => [ | ||
| 'longitude' => $center[0] ?? 0.0, | ||
| 'latitude' => $center[1] ?? 0.0, | ||
| ], | ||
| 'relevance' => $feature['relevance'] ?? 0.0, | ||
| 'place_type' => $feature['place_type'] ?? [], | ||
| ]; | ||
| } | ||
|
|
||
| return [ | ||
| 'results' => $results, | ||
| 'count' => \count($results), | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @param float $longitude The longitude coordinate | ||
| * @param float $latitude The latitude coordinate | ||
| * @param int $limit Maximum number of results to return (1-5) | ||
| * | ||
| * @return array{ | ||
| * results: array<array{ | ||
| * address: string, | ||
| * coordinates: array{longitude: float, latitude: float}, | ||
| * place_type: string[], | ||
| * context: array<array{id: string, text: string}> | ||
| * }>, | ||
| * count: int | ||
| * } | ||
| */ | ||
| public function reverseGeocode( | ||
| float $longitude, | ||
| float $latitude, | ||
| #[With(minimum: 1, maximum: 5)] | ||
| int $limit = 1, | ||
| ): array { | ||
| $response = $this->httpClient->request('GET', 'https://api.mapbox.com/search/geocode/v6/reverse', [ | ||
| 'query' => [ | ||
| 'longitude' => $longitude, | ||
| 'latitude' => $latitude, | ||
| 'access_token' => $this->accessToken, | ||
| 'limit' => $limit, | ||
| ], | ||
| ]); | ||
|
|
||
| $data = $response->toArray(); | ||
|
|
||
| if (!isset($data['features']) || [] === $data['features']) { | ||
| return [ | ||
| 'results' => [], | ||
| 'count' => 0, | ||
| ]; | ||
| } | ||
|
|
||
| $results = []; | ||
| foreach ($data['features'] as $feature) { | ||
| $properties = $feature['properties'] ?? []; | ||
| $coordinates = $properties['coordinates'] ?? []; | ||
|
|
||
| $context = []; | ||
| if (isset($properties['context'])) { | ||
| foreach ($properties['context'] as $key => $contextItem) { | ||
| if (\is_array($contextItem) && isset($contextItem['name'])) { | ||
| $context[] = [ | ||
| 'id' => $contextItem['id'] ?? $contextItem['mapbox_id'] ?? '', | ||
| 'text' => $contextItem['name'], | ||
| 'type' => $key, | ||
| ]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $results[] = [ | ||
| 'address' => $properties['place_formatted'] ?? $properties['name'] ?? '', | ||
| 'coordinates' => [ | ||
| 'longitude' => $coordinates['longitude'] ?? 0.0, | ||
| 'latitude' => $coordinates['latitude'] ?? 0.0, | ||
| ], | ||
| 'place_type' => [$properties['feature_type'] ?? 'unknown'], | ||
| 'context' => $context, | ||
| ]; | ||
| } | ||
|
|
||
| return [ | ||
| 'results' => $results, | ||
| 'count' => \count($results), | ||
| ]; | ||
| } | ||
| } |
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.
those example coordinates result in an "couldn't retrieve" response for me.
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.
Same for me, but I can't find a coordinate which brings up a real address. shall we remove the reverse thing?
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.
No, it's actually with your example as well that it brings back the right address in New York, but the array handling of the response data in
Symfony\AI\Agent\Toolbox\Tool\Mapbox::reverseGeocodeis wrong.