Google Drive 阅读器¶
演示我们的 Google Drive 数据连接器
先决条件¶
按照这些步骤设置您的环境。
- 在您的 GCP 项目中启用 Google Drive API。
- 为您的 GCP 项目配置 OAuth 同意屏幕。
- 如果您不在 Google Workspace 中,将其设置为“外部”即可。
- 为您的应用程序 (此 notebook) 创建客户端凭据。
- 确保将应用程序类型设置为“桌面应用”。
- 将这些客户端凭据移动到此 notebook 所在的目录中,并将其命名为“credentials.json”。
如果您在 colab 上打开此 Notebook,您可能需要安装 LlamaIndex 🦙。
In [ ]
已复制!
%pip install llama-index llama-index-readers-google
%pip install llama-index llama-index-readers-google
In [ ]
已复制!
import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
import logging import sys logging.basicConfig(stream=sys.stdout, level=logging.INFO) logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
In [ ]
已复制!
from llama_index.core import SummaryIndex
from llama_index.readers.google import GoogleDriveReader
from IPython.display import Markdown, display
from llama_index.core import SummaryIndex from llama_index.readers.google import GoogleDriveReader from IPython.display import Markdown, display
选择要读取的文件夹¶
您可以通过导航到 Google Drive 中的一个文件夹,然后选择 URL 的最后一部分来找到文件夹 ID。
例如,对于此 URL:https://drive.google.com/drive/u/0/folders/abcdefgh12345678
,文件夹 ID 是 abcdefgh12345678
In [ ]
已复制!
# Replace the placeholder with your chosen folder ID
folder_id = ["<your_folder_id>"]
# Make sure credentials.json file exists in the current directory (data_connectors)
documents = GoogleDriveReader().load_data(folder_id=folder_id)
# 将占位符替换为您选择的文件夹 ID folder_id = [""] # 确保 credentials.json 文件存在于当前目录 (data_connectors) 中 documents = GoogleDriveReader().load_data(folder_id=folder_id)
In [ ]
已复制!
index = SummaryIndex.from_documents(documents)
index = SummaryIndex.from_documents(documents)
In [ ]
已复制!
# Set Logging to DEBUG for more detailed outputs
query_engine = index.as_query_engine()
response = query_engine.query("<query_text>")
# 将日志级别设置为 DEBUG 以获取更详细的输出 query_engine = index.as_query_engine() response = query_engine.query("")
In [ ]
已复制!
display(Markdown(f"<b>{response}</b>"))
display(Markdown(f"{response}"))