跳到主要内容
本笔记本将介绍如何将 LangChain 连接到 Google Drive API

先决条件

  1. 创建 Google Cloud 项目或使用现有项目
  2. 启用 Google Drive API
  3. 授权桌面应用程序的凭据
  4. pip install -U google-api-python-client google-auth-httplib2 google-auth-oauthlib

检索 Google Docs 数据说明

默认情况下,GoogleDriveToolsGoogleDriveWrapper 期望 credentials.json 文件位于 ~/.credentials/credentials.json,但这可以通过设置 GOOGLE_ACCOUNT_FILE 环境变量为 custom/path/to/credentials.json 来配置。token.json 的位置使用相同的目录(或使用参数 token_path)。请注意,token.json 在您首次使用该工具时会自动创建。 GoogleDriveSearchTool 可以通过一些请求检索选定的文件。 默认情况下,如果您使用 folder_id,则该文件夹中的所有文件都可以检索到 Document,如果名称与查询匹配。
pip install -qU  google-api-python-client google-auth-httplib2 google-auth-oauthlib langchain-community
您可以从 URL 获取您的文件夹和文档 ID 特殊值 root 表示您的个人主页。
folder_id = "root"
# folder_id='1yucgL9WGgWZdM1TOuKkeghlPizuzMYb5'
默认情况下,所有具有这些 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 的文档。 但是,必须安装相应的软件包。
pip install -qU  unstructured langchain-googledrive
import os

from langchain_googledrive.tools.google_drive.tool import GoogleDriveSearchTool
from langchain_googledrive.utilities.google_drive import GoogleDriveAPIWrapper

os.environ["GOOGLE_ACCOUNT_FILE"] = "custom/path/to/credentials.json"

# By default, search only in the filename.
tool = GoogleDriveSearchTool(
    api_wrapper=GoogleDriveAPIWrapper(
        folder_id=folder_id,
        num_results=2,
        template="gdrive-query-in-folder",  # Search in the body of documents
    )
)
import logging

logging.basicConfig(level=logging.INFO)
tool.run("machine learning")
tool.description
"A wrapper around Google Drive Search. Useful for when you need to find a document in google drive. The input should be formatted as a list of entities separated with a space. As an example, a list of keywords is 'hello word'."

在 ReAct 代理中使用工具

为了创建使用 Google Jobs 工具的代理,请安装 LangGraph
pip install -qU langgraph langchain-openai
并使用 create_agent 功能初始化 ReAct 代理。您还需要设置您的 OPEN_API_KEY(访问 platform.openai.com)才能访问 OpenAI 的聊天模型。
import os

from langchain.chat_models import init_chat_model
from langchain.agents import create_agent


os.environ["OPENAI_API_KEY"] = "your-openai-api-key"


model = init_chat_model("gpt-4o-mini", model_provider="openai", temperature=0)
agent = create_agent(model, tools=[tool])

events = agent.stream(
    {"messages": [("user", "Search in google drive, who is 'Yann LeCun' ?")]},
    stream_mode="values",
)
for event in events:
    event["messages"][-1].pretty_print()

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.