Google Drive 检索文档。
前提条件
- 创建一个 Google Cloud 项目或使用现有项目
- 启用 Google Drive API
- 为桌面应用授权凭证
pip install -U google-api-python-client google-auth-httplib2 google-auth-oauthlib
检索 Google 文档
默认情况下,GoogleDriveRetriever 期望 credentials.json 文件位于 ~/.credentials/credentials.json,但可以通过 GOOGLE_ACCOUNT_FILE 环境变量进行配置。
token.json 的位置使用相同目录(或使用参数 token_path)。注意,token.json 将在第一次使用检索器时自动创建。
GoogleDriveRetriever 可以通过一些请求检索选定的文件。
默认情况下,如果使用 folder_id,该文件夹内的所有文件都可以被检索为 Document。
可以从 URL 获取文件夹和文档 ID:
- 文件夹:drive.google.com/drive/u/0/folders/1yucgL9WGgWZdM1TOuKkeghlPizuzMYb5 -> 文件夹 ID 为
"1yucgL9WGgWZdM1TOuKkeghlPizuzMYb5" - 文档:docs.google.com/document/d/1bfaMQ18_i56204VaQDVeAFpqEijJTgvurupdEDiaUQw/edit -> 文档 ID 为
"1bfaMQ18_i56204VaQDVeAFpqEijJTgvurupdEDiaUQw"
root 表示个人主目录。
Document:
text/texttext/plaintext/htmltext/csvtext/markdownimage/pngimage/jpegapplication/epub+zipapplication/pdfapplication/rtfapplication/vnd.google-apps.document(GDoc)application/vnd.google-apps.presentation(GSlide)application/vnd.google-apps.spreadsheet(GSheet)application/vnd.google.colaboratory(Notebook colab)application/vnd.openxmlformats-officedocument.presentationml.presentation(PPTX)application/vnd.openxmlformats-officedocument.wordprocessingml.document(DOCX)
GoogleDriveRetriever 的文档。
但相应的包必须已安装:
| 模板 | 描述 |
|---|---|
gdrive-all-in-folder | 返回 folder_id 中所有兼容文件 |
gdrive-query | 在所有云盘中搜索 query |
gdrive-by-name | 搜索名称为 query 的文件 |
gdrive-query-in-folder | 在 folder_id 中搜索 query(_recursive=true 时包括子文件夹) |
gdrive-mime-type | 搜索特定 mime_type |
gdrive-mime-type-in-folder | 在 folder_id 中搜索特定 mime_type |
gdrive-query-with-mime-type | 搜索具有特定 mime_type 的 query |
gdrive-query-with-mime-type-and-folder | 搜索具有特定 mime_type 且在 folder_id 中的 query |
PromptTemplate 自定义提示:
使用 Google Drive 的”描述”元数据
每个 Google Drive 文件在元数据中都有一个description 字段(见文件详情)。
使用 snippets 模式返回所选文件的描述。
将这些文档连接 到 Claude、VSCode 等,通过 MCP 获取实时答案。

