本笔记本演示如何将 LangChain 连接到 Google Drive API。
前置条件
- 创建 Google Cloud 项目或使用现有项目
- 启用 Google Drive API
- 为桌面应用授权凭据
pip install -U google-api-python-client google-auth-httplib2 google-auth-oauthlib
获取 Google 文档数据的操作步骤
默认情况下,GoogleDriveTools 和 GoogleDriveWrapper 期望 credentials.json 文件为 ~/.credentials/credentials.json,但可通过设置 GOOGLE_ACCOUNT_FILE 环境变量为你的 custom/path/to/credentials.json 进行配置。
token.json 的位置使用相同目录(或通过 token_path 参数指定)。注意 token.json 将在首次使用工具时自动创建。
GoogleDriveSearchTool 可通过多次请求检索指定文件。
默认情况下,若使用 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)
可通过 GoogleDriveAPIWrapper 文档更新或自定义此设置。对应包需已安装。
在 ReAct 智能体内使用工具
要创建使用 Google Jobs 工具的智能体,需安装 LangGraph
并使用 @[create_agent] 功能初始化 ReAct 智能体。还需设置 OPEN_API_KEY(访问 platform.openai.com)以访问 OpenAI 的对话模型。
官方原文:src/oss/python/integrations/tools/google_drive.mdx · 非官方翻译,译文由 AI 生成,以英文原文为准。
Last modified on November 3, 2026