Skip to main content
本笔记本说明如何从 Google Drive 获取文档。

前置条件

  1. 创建 Google Cloud 项目或使用现有项目
  2. 启用 Google Drive API
  3. 为桌面应用授权凭据
  4. 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: 特殊值 root 表示您的个人主页。
默认情况下,所有以下 MIME 类型文件均可转换为 Document。
  • text/text
  • text/plain
  • text/html
  • text/csv
  • text/markdown
  • image/png
  • image/jpeg
  • application/epub+zip
  • application/pdf
  • application/rtf
  • application/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 文档更新或自定义此设置。 对应包需安装。
您可自定义筛选条件选择文件。提供以下预设过滤器:
否则,可通过专用 PromptTemplate 自定义提示词

使用 Google Drive ‘description’ 元数据

每个 Google Drive 文件都有 description 字段(参考 文件详情)。 使用 snippets 模式可返回选定文件的描述。

官方原文:src/oss/python/integrations/retrievers/google_drive.mdx · 非官方翻译,译文由 AI 生成,以英文原文为准。
Last modified on November 3, 2026