-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Added Google Maps toolkit. #1689
base: main
Are you sure you want to change the base?
Conversation
""" | ||
from os import getenv | ||
from phi.tools import Toolkit | ||
import googlemaps |
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.
move this into a try and catch block. Take inspiration from here https://github.com/phidatahq/phidata/blob/main/phi/tools/slack.py#L8
from phi.tools import Toolkit | ||
import googlemaps | ||
|
||
_google_map_client = googlemaps.Client(key=getenv('GOOGLE_MAPS_API_KEY')) |
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.
move this inside the class in __init__
function and also a check if the key is set or not else raise an error
def search_google_maps(self, query: str) -> str: | ||
""" | ||
Search for businesses using Google Maps Places API. | ||
This tool takes a search query and returns detailed business information. | ||
|
||
Args: | ||
query (str): The query string to search for using Google Maps Search API. (e.g., "dental clinics in Noida") | ||
|
||
Returns: | ||
Stringified list of dictionaries containing business information like name, address, phone, website, rating, and reviews etc. | ||
""" | ||
try: | ||
# Perform places search | ||
places_result = _google_map_client.places(query) | ||
|
||
if not places_result or 'results' not in places_result: | ||
return [] | ||
|
||
businesses = [] | ||
for place in places_result['results']: | ||
business = { | ||
'name': place.get('name', ''), | ||
'address': place.get('formatted_address', ''), | ||
'rating': place.get('rating', 0.0), | ||
'reviews': place.get('user_ratings_total', 0), | ||
'place_id': place.get('place_id', ''), | ||
} | ||
|
||
# Get place details for additional information | ||
if place.get('place_id'): | ||
details = _google_map_client.place(place['place_id']) | ||
if details and 'result' in details: | ||
result = details['result'] | ||
business.update({ | ||
'phone': result.get('formatted_phone_number', ''), | ||
'website': result.get('website', ''), | ||
'hours': result.get('opening_hours', {}).get('weekday_text', []) | ||
}) | ||
|
||
businesses.append(business) | ||
|
||
return str(businesses) |
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.
Is this function only for search businesses? If yes then we should either make it generalised to find any place on the map else rename the function to search_businesses_google_maps. What do you think?
Description
Please include:
Fixes # (issue)
Type of change
Please check the options that are relevant:
Checklist
Additional Notes
Tested with below test script:
Here is the output: