Google Maps 文本搜索阅读器¶
本笔记本演示了如何使用 llama_index 库中的 GoogleMapsTextSearchReader 从 Google Maps Places API 加载并查询数据。
如果您在 Colab 上打开此 Notebook,需要先安装 llama-index 库。
In [ ]:
Copied!
!pip install llama-index llama-index-readers-google
!pip install llama-index llama-index-readers-google
导入必要库¶
我们将导入所需的库,包括来自llama_index的GoogleMapsTextSearchReader以及其他工具库。
In [ ]:
Copied!
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))
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))
设置 API 密钥¶
请确保已准备好您的 Google Maps API 密钥。您可以直接在代码中设置该密钥,或将其存储在名为 GOOGLE_MAPS_API_KEY
的环境变量中。
In [ ]:
Copied!
# Set your API key here if not using environment variable
os.environ["GOOGLE_MAPS_API_KEY"] = api_key
# Set your API key here if not using environment variable
os.environ["GOOGLE_MAPS_API_KEY"] = api_key
从 Google Maps 加载数据¶
使用 GoogleMapsTextSearchReader
,我们将为搜索查询加载数据。在本示例中,我们搜索伊斯坦布尔的高品质土耳其美食。
In [ ]:
Copied!
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])
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])
为加载数据建立索引¶
接下来我们将基于已加载的文档创建 VectorStoreIndex。该索引将支持我们对数据进行高效查询。
In [ ]:
Copied!
index = VectorStoreIndex.from_documents(documents)
index = VectorStoreIndex.from_documents(documents)
查询索引¶
最后,我们将通过查询索引来找到评价最佳的土耳其餐厅。
In [ ]:
Copied!
response = index.query("Which Turkish restaurant has the best reviews?")
display(Markdown(f"<b>{response}</b>"))
response = index.query("Which Turkish restaurant has the best reviews?")
display(Markdown(f"{response}"))
摘要¶
在本笔记本中,我们演示了如何使用 GoogleMapsTextSearchReader 从谷歌地图加载数据,通过 VectorStoreIndex 建立索引,并执行查询以查找伊斯坦布尔评分最高的土耳其餐厅。