Back to Llama Index

Google Maps Text Search Reader

docs/examples/data_connectors/GoogleMapsTextSearchReaderDemo.ipynb

0.14.212.5 KB
Original Source

<a href="https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/data_connectors/GoogleMapsTextSearchReaderDemo.ipynb" target="_parent"></a>

Google Maps Text Search Reader

This notebook demonstrates how to use the GoogleMapsTextSearchReader from the llama_index library to load and query data from the Google Maps Places API.

If you're opening this Notebook on colab, you will need to install the llama-index library.

python
!pip install llama-index llama-index-readers-google

Importing Necessary Libraries

We will import the necessary libraries including the GoogleMapsTextSearchReader from llama_index and other utility libraries.

python
import logging
import sys
from llama_index.readers.google import GoogleMapsTextSearchReader
from llama_index.core import VectorStoreIndex
from IPython.display import Markdown, display
import os

logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))

Setting Up API Key

Make sure you have your Google Maps API key ready. You can set it directly in the code or store it in an environment variable named GOOGLE_MAPS_API_KEY.

python
# Set your API key here if not using environment variable
os.environ["GOOGLE_MAPS_API_KEY"] = api_key

Loading Data from Google Maps

Using the GoogleMapsTextSearchReader, we will load data for a search query. In this example, we search for quality Turkish food in Istanbul.

python
loader = GoogleMapsTextSearchReader()
documents = loader.load_data(
    text="I want to eat quality Turkish food in Istanbul",
    number_of_results=160,
)

# Displaying the first document to understand its structure
print(documents[0])

Indexing the Loaded Data

We will now create a VectorStoreIndex from the loaded documents. This index will allow us to perform efficient queries on the data.

python
index = VectorStoreIndex.from_documents(documents)

Querying the Index

Finally, we will query the index to find the Turkish restaurant with the best reviews.

python
response = index.query("Which Turkish restaurant has the best reviews?")
display(Markdown(f"<b>{response}</b>"))

Summary

In this notebook, we demonstrated how to use the GoogleMapsTextSearchReader to load data from Google Maps, index it using the VectorStoreIndex, and perform a query to find the best-reviewed Turkish restaurant in Istanbul.