|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\AI\Agent\Toolbox\Tool; |
| 13 | + |
| 14 | +use Symfony\AI\Agent\Toolbox\Attribute\AsTool; |
| 15 | +use Symfony\AI\Platform\Contract\JsonSchema\Attribute\With; |
| 16 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author Oskar Stark <oskarstark@googlemail.com> |
| 20 | + */ |
| 21 | +#[AsTool(name: 'geocode', description: 'Convert addresses to coordinates using Mapbox Geocoding API', method: 'geocode')] |
| 22 | +#[AsTool(name: 'reverse_geocode', description: 'Convert coordinates to addresses using Mapbox Reverse Geocoding API', method: 'reverseGeocode')] |
| 23 | +final readonly class Mapbox |
| 24 | +{ |
| 25 | + public function __construct( |
| 26 | + private HttpClientInterface $httpClient, |
| 27 | + #[\SensitiveParameter] private string $accessToken, |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @param string $address The address to geocode (e.g., "1600 Pennsylvania Ave, Washington DC") |
| 33 | + * @param int $limit Maximum number of results to return (1-10) |
| 34 | + * |
| 35 | + * @return array{ |
| 36 | + * results: array<array{ |
| 37 | + * address: string, |
| 38 | + * coordinates: array{longitude: float, latitude: float}, |
| 39 | + * relevance: float, |
| 40 | + * place_type: string[] |
| 41 | + * }>, |
| 42 | + * count: int |
| 43 | + * } |
| 44 | + */ |
| 45 | + public function geocode( |
| 46 | + string $address, |
| 47 | + #[With(minimum: 1, maximum: 10)] |
| 48 | + int $limit = 1, |
| 49 | + ): array { |
| 50 | + $response = $this->httpClient->request('GET', 'https://api.mapbox.com/geocoding/v5/mapbox.places/'.urlencode($address).'.json', [ |
| 51 | + 'query' => [ |
| 52 | + 'access_token' => $this->accessToken, |
| 53 | + 'limit' => $limit, |
| 54 | + ], |
| 55 | + ]); |
| 56 | + |
| 57 | + $data = $response->toArray(); |
| 58 | + |
| 59 | + if (!isset($data['features']) || [] === $data['features']) { |
| 60 | + return [ |
| 61 | + 'results' => [], |
| 62 | + 'count' => 0, |
| 63 | + ]; |
| 64 | + } |
| 65 | + |
| 66 | + $results = []; |
| 67 | + foreach ($data['features'] as $feature) { |
| 68 | + $center = $feature['center'] ?? [0.0, 0.0]; |
| 69 | + $results[] = [ |
| 70 | + 'address' => $feature['place_name'] ?? '', |
| 71 | + 'coordinates' => [ |
| 72 | + 'longitude' => $center[0] ?? 0.0, |
| 73 | + 'latitude' => $center[1] ?? 0.0, |
| 74 | + ], |
| 75 | + 'relevance' => $feature['relevance'] ?? 0.0, |
| 76 | + 'place_type' => $feature['place_type'] ?? [], |
| 77 | + ]; |
| 78 | + } |
| 79 | + |
| 80 | + return [ |
| 81 | + 'results' => $results, |
| 82 | + 'count' => \count($results), |
| 83 | + ]; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @param float $longitude The longitude coordinate |
| 88 | + * @param float $latitude The latitude coordinate |
| 89 | + * @param int $limit Maximum number of results to return (1-5) |
| 90 | + * |
| 91 | + * @return array{ |
| 92 | + * results: array<array{ |
| 93 | + * address: string, |
| 94 | + * coordinates: array{longitude: float, latitude: float}, |
| 95 | + * place_type: string[], |
| 96 | + * context: array<array{id: string, text: string}> |
| 97 | + * }>, |
| 98 | + * count: int |
| 99 | + * } |
| 100 | + */ |
| 101 | + public function reverseGeocode( |
| 102 | + float $longitude, |
| 103 | + float $latitude, |
| 104 | + #[With(minimum: 1, maximum: 5)] |
| 105 | + int $limit = 1, |
| 106 | + ): array { |
| 107 | + $response = $this->httpClient->request('GET', 'https://api.mapbox.com/search/geocode/v6/reverse', [ |
| 108 | + 'query' => [ |
| 109 | + 'longitude' => $longitude, |
| 110 | + 'latitude' => $latitude, |
| 111 | + 'access_token' => $this->accessToken, |
| 112 | + 'limit' => $limit, |
| 113 | + ], |
| 114 | + ]); |
| 115 | + |
| 116 | + $data = $response->toArray(); |
| 117 | + |
| 118 | + if (!isset($data['features']) || [] === $data['features']) { |
| 119 | + return [ |
| 120 | + 'results' => [], |
| 121 | + 'count' => 0, |
| 122 | + ]; |
| 123 | + } |
| 124 | + |
| 125 | + $results = []; |
| 126 | + foreach ($data['features'] as $feature) { |
| 127 | + $properties = $feature['properties'] ?? []; |
| 128 | + $coordinates = $properties['coordinates'] ?? []; |
| 129 | + |
| 130 | + $context = []; |
| 131 | + if (isset($properties['context'])) { |
| 132 | + foreach ($properties['context'] as $key => $contextItem) { |
| 133 | + if (\is_array($contextItem) && isset($contextItem['name'])) { |
| 134 | + $context[] = [ |
| 135 | + 'id' => $contextItem['id'] ?? $contextItem['mapbox_id'] ?? '', |
| 136 | + 'text' => $contextItem['name'], |
| 137 | + 'type' => $key, |
| 138 | + ]; |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + $results[] = [ |
| 144 | + 'address' => $properties['place_formatted'] ?? $properties['name'] ?? '', |
| 145 | + 'coordinates' => [ |
| 146 | + 'longitude' => $coordinates['longitude'] ?? 0.0, |
| 147 | + 'latitude' => $coordinates['latitude'] ?? 0.0, |
| 148 | + ], |
| 149 | + 'place_type' => [$properties['feature_type'] ?? 'unknown'], |
| 150 | + 'context' => $context, |
| 151 | + ]; |
| 152 | + } |
| 153 | + |
| 154 | + return [ |
| 155 | + 'results' => $results, |
| 156 | + 'count' => \count($results), |
| 157 | + ]; |
| 158 | + } |
| 159 | +} |
0 commit comments