Tutorials

Chat with Google Maps using GPTScript

May 29, 2024 by grant linville

GPTScript‘s support for OpenAPI documents makes it easy to interact with public APIs without writing any custom tools. In this tutorial, I will demonstrate how to use GPTScript to chat with Google Maps.

Prerequisite: Google Maps API key

You will need to have an API key in order to access the Google Maps API. You can follow Google’s guide here in order to get one.

Once you have it, export it as an environment variable:

export GPTSCRIPT_MAPS_GOOGLEAPIS_COM_APIKEYAUTH=<YOUR API KEY>

Finding the OpenAPI Document

Google Maps OpenAPI documents are available in the googlemaps/openapi-specification GitHub repo. The specific file that we are going to use is in the dist folder, and can be found here. Since this is a plain text file, we can reference it directly by URL in our GPTScript.

Listing the Tools

You can use GPTScript to list the tools available in this OpenAPI definition.

gptscript --list-tools https://raw.githubusercontent.com/googlemaps/openapi-specification/main/dist/google-maps-platform-openapi3.yml

The output is pretty long, so I will just show the top-level tool which exports the rest of the tools in the document:

Name: https://raw.githubusercontent.com/googlemaps/openapi-specification/main/dist/google-maps-platform-openapi3.yml
Description: This is a tool set for the Google Maps Platform OpenAPI spec
Export: geolocate, directions, distanceMatrix, elevation, geocode, autocomplete, placeDetails, findPlaceFromText, nearbySearch, placePhoto, queryAutocomplete, textSearch, streetView, streetViewMetadata, timezone, nearestRoads, snapToRoads
Model: gpt-4o

Just from the tool names alone, we can see that we have the ability to get information about roads, places, elevation, and more.

Chatting with the API

This is the GPTScript we will use to chat with the Google Maps API:

tools: https://raw.githubusercontent.com/googlemaps/openapi-specification/main/dist/google-maps-platform-openapi3.yml
chat: true

You are a helpful assistant with access to the Google Maps API. Please ask me what I would like to do.

Save this in a file called maps.gpt. Now let’s run it with GPTScript:

gptscript maps.gpt
14:43:51 started  [main]
14:43:51 sent     [main]
14:43:54 ended    [main] [output=What would you like to do today?]
14:43:54 usage    [total=29451] [prompt=29442] [completion=9]
< What would you like to do today?

Now we can begin chatting with the API. Let’s ask it for some coffee shop recommendations in Oslo.

maps.gpt> What are some good coffee shops in Oslo?
< Here are some good coffee shops in Oslo:

1. **Fuglen**
   - Address: Universitetsgata 2, 0164 Oslo, Norway
   - Rating: 4.5
   - ![Fuglen](https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/cafe-71.png)

2. **Starbucks**
   - Address: Stenersgata 1, 0050 Oslo, Norway
   - Rating: 3.7
   - ![Starbucks](https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/cafe-71.png)

3. **Espresso House**
   - Address: Olav Vs gate 2, 0166 Oslo, Norway
   - Rating: 4.1
   - ![Espresso House](https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/cafe-71.png)

4. **Cafe Sør**
   - Address: Torggata 11, 0181 Oslo, Norway
   - Rating: 4.1
   - ![Cafe Sør](https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/cafe-71.png)

5. **Cafe Nero**
   - Address: Thereses gate 47, 0354 Oslo, Norway
   - Rating: 4.5
   - ![Cafe Nero](https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/cafe-71.png)

6. **Tim Wendelboe**
   - Address: Grüners gate 1, 0552 Oslo, Norway
   - Rating: 4.7
   - ![Tim Wendelboe](https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/cafe-71.png)

7. **Orient Kaffehus**
   - Address: Storgata 29, 0184 Oslo, Norway
   - Rating: 3.9
   - ![Orient Kaffehus](https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/cafe-71.png)

8. **Café Sara**
   - Address: Hausmanns gate 29, 0182 Oslo, Norway
   - Rating: 4.3
   - ![Café Sara](https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/cafe-71.png)

9. **Starbucks**
   - Address: Jernbanetorget, 0154 Oslo, Norway
   - Rating: 3.7
   - ![Starbucks](https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/cafe-71.png)

10. **Espresso House**
    - Address: Jernbanetorget 1 1 etasje Flytogterminalen, 0191 Oslo, Norway
    - Rating: 3.5
    - ![Espresso House](https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/cafe-71.png)

Would you like more information on any of these coffee shops?
maps.gpt> Yes, what are the reviews like for Tim Wendelboe?
< **Tim Wendelboe** has an impressive rating of 4.7 based on 1893 user ratings. Here are some reviews from customers:

1. **Roger Zhang** (5 stars, 3 months ago)
   - "Have you had coffee made by a world champ? Well this was first from me and I greatly enjoyed the experience. Definitely fruit forward and low acid taste profile - aeropress instead of a V60 was interesting choice. Espresso drinks were good too. Also got a coffee berry fizz which tasted wine like! Would love to come back for their summer menu!"
   - ![Roger Zhang](https://lh3.googleusercontent.com/a-/ALV-UjU1WmNoWljTMH1C_fuYf35LmKYmutyCjUlTrLOezblN6BM51gg=s128-c0x00000000-cc-rp-mo-ba4)

2. **Eva Smorodina** (5 stars, 1 month ago)
   - "Wonderful place! Great coffee and atmosphere. You can also buy supplies for a coffee machine at home. But be aware of the queue: on a sunny day, many people will want to come."
   - ![Eva Smorodina](https://lh3.googleusercontent.com/a-/ALV-UjVuwW0L_Q-DPXcajz9qcc6xVCppPb6XpuuTleWi-6UkIr5a4pH-=s128-c0x00000000-cc-rp-mo)

3. **Arlei Wermuth** (5 stars, 1 month ago)
   - "Incredibly fantastic, without a doubt one of the best coffees I've ever tasted in my life. The place is very small and intimate, but super cozy. Mandatory place when in Oslo."
   - ![Arlei Wermuth](https://lh3.googleusercontent.com/a-/ALV-UjVanJTEVikgnkRrCSlk9xRFZcIcY01MJA0HmeTM58KZTUhCip5m1g=s128-c0x00000000-cc-rp-mo-ba5)

4. **Tom Bouwhuis** (5 stars, 2 months ago)
   - "This is an amazing coffee experience. Did the coffee tasting menu by myself (you can try 4 of their coffees) and tasted coffee like I never tasted before. The staff is very knowledgeable and you get an explanation about all the coffees you try. If you are a coffee fanatic, this place must be on your list."
   - ![Tom Bouwhuis](https://lh3.googleusercontent.com/a-/ALV-UjW07zzf2JA8lOx96gCmb5gMTh8DCcLkZgKlHZUbCwO4UOZ3ivnYUw=s128-c0x00000000-cc-rp-mo-ba4)

5. **Rebecca Lisa Lim** (5 stars, 4 months ago)
   - "Visited Tim Wendelboe during our holiday here in Oslo and we absolutely loved it. Damian who served us had excellent level of knowledge for coffee and explained everything so well to us which made the experience even better. If you’re a coffee lover or merely looking for a good place to have a good cup, this is it!"
   - ![Rebecca Lisa Lim](https://lh3.googleusercontent.com/a/ACg8ocIHcJIiKgUwW-T852zOi1R9beAxaujVq5PDu-b_ifAbiTlPKF0=s128-c0x00000000-cc-rp-mo)

It seems like Tim Wendelboe is highly recommended for its excellent coffee and knowledgeable staff. Would you like to know more about any other coffee shops?
maps.gpt> How long would it take to drive there from the airport?
< The drive from Oslo Airport, Gardermoen to Tim Wendelboe in Oslo is approximately 49.0 km and takes about 44 minutes.

Here are the driving directions:

1. Head northeast on Lufthavnvegen.
2. Keep right to stay on Lufthavnvegen.
3. Continue onto E16.
4. Take the exit onto E6 toward Oslo/E16/Kongsvinger.
5. Keep left to stay on E6.
6. Slight left to stay on E6 (signs for Kristiansand/E18/Sentrum).
7. Keep left to stay on E6.
8. Take exit 64 toward Válerenga/Ensjø/Etterstad.
9. At the roundabout, take the 1st exit onto Ensjøveien.
10. Turn left onto Økernveien.
11. Turn right onto Finnmarkgata/Fv161.
12. Turn left onto Helgesens gate.
13. Turn left onto Thorvald Meyers gate.
14. Turn right onto Grüners gate. Your destination will be on the left.

Safe travels! If you need more information or assistance, feel free to ask.

(You can press Ctrl+C to end the chat at any time.)

If you want to ask more questions, you can continue the conversation. Do keep in mind that the API responses from Google Maps can be quite large, depending on what you ask, and gpt-4o’s context window is 128,000 tokens. If you go over the context window, the chat will end.

If you want to ask just one question and not start a chat session, you can do that with this script:

tools: https://raw.githubusercontent.com/googlemaps/openapi-specification/main/dist/google-maps-platform-openapi3.yml

You are a helpful assistant with access to the Google Maps API. Please answer my question.

And then you can run it like this:

gptscript maps.gpt "What is the elevation of Bern, Switzerland?"
14:53:17 started  [main] [input=What is the elevation of Bern, Switzerland?]
14:53:18 sent     [main]
14:53:21 started  [elevation(2)] [input={"locations":["46.94809,7.44744"]}]
14:53:21 ended    [elevation(2)] [output={n   "results" : n   [n      {n         "elevation" : 545.3124389648438,n         "location" : n...]
14:53:21 continue [main]
14:53:21 sent     [main]
14:53:24 ended    [main] [output=The elevation of Bern, Switzerland is approximately 545.31 meters.]
14:53:24 usage    [total=59210] [prompt=59172] [completion=38]

INPUT:

What is the elevation of Bern, Switzerland?

OUTPUT:

The elevation of Bern, Switzerland is approximately 545.31 meters.

There are many more tools available in the Google Maps OpenAPI document, so feel free to explore and ask questions about different locations, distances, and more. Happy scripting!

Related Articles