AI Agents
Google Maps Agent
Build a location-aware AI agent that answers questions like 'Find the best rated sushi near Times Square' by combining Google Maps API with OneInfer's LLM via tool calling.
OneInfer Chat APIGoogle Maps APITool CallingPython
Step-by-step guide
1
Define the Maps search tool
python
import openai
import requests
import json
client = openai.OpenAI(
api_key="your-oneinfer-api-key",
base_url="https://api.oneinfer.ai/v1"
)
MAPS_API_KEY = "your-google-maps-api-key"
tools = [
{
"type": "function",
"function": {
"name": "search_places",
"description": "Search for places near a location using Google Maps.",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search query, e.g. 'sushi restaurants'"},
"location": {"type": "string", "description": "Address or landmark, e.g. 'Times Square, NYC'"},
"radius_meters": {"type": "integer", "description": "Search radius in meters (default 1000)"}
},
"required": ["query", "location"]
}
}
}
]2
Implement the tool execution
python
def search_places(query: str, location: str, radius_meters: int = 1000) -> str:
# Geocode the location
geo = requests.get(
"https://maps.googleapis.com/maps/api/geocode/json",
params={"address": location, "key": MAPS_API_KEY}
).json()
loc = geo["results"][0]["geometry"]["location"]
lat, lng = loc["lat"], loc["lng"]
# Search for places
places = requests.get(
"https://maps.googleapis.com/maps/api/place/nearbysearch/json",
params={
"location": f"{lat},{lng}",
"radius": radius_meters,
"keyword": query,
"key": MAPS_API_KEY,
"rankby": "prominence"
}
).json()
results = places.get("results", [])[:5]
formatted = []
for p in results:
formatted.append({
"name": p.get("name"),
"rating": p.get("rating"),
"address": p.get("vicinity"),
"open_now": p.get("opening_hours", {}).get("open_now")
})
return json.dumps(formatted, indent=2)3
Run the agentic loop
python
def run_agent(user_query: str) -> str:
messages = [
{"role": "system", "content": "You are a helpful local guide. Use the search_places tool to answer location questions."},
{"role": "user", "content": user_query}
]
while True:
response = client.chat.completions.create(
model="meta-llama/Llama-3.3-70B-Instruct-Turbo",
messages=messages,
tools=tools,
tool_choice="auto"
)
msg = response.choices[0].message
if msg.tool_calls:
messages.append(msg)
for call in msg.tool_calls:
args = json.loads(call.function.arguments)
result = search_places(**args)
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": result
})
else:
return msg.content
print(run_agent("Find the top 3 rated sushi restaurants near Times Square that are open now"))