跳到主要内容
本笔记本涵盖了如何从 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 表示您的个人主页。
from langchain_googledrive.retrievers import GoogleDriveRetriever

folder_id = "root"
# folder_id='1yucgL9WGgWZdM1TOuKkeghlPizuzMYb5'

retriever = GoogleDriveRetriever(
    num_results=2,
)
默认情况下,所有具有这些 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 的文档。 但是,必须安装相应的包。
pip install -qU  unstructured
retriever.invoke("machine learning")
您可以自定义选择文件的标准。提供了一组预定义的过滤器
模板描述
gdrive-all-in-folderfolder_id 返回所有兼容文件
gdrive-query在所有驱动器中搜索 query
gdrive-by-name搜索名为 query 的文件
gdrive-query-in-folderfolder_id 中搜索 query(以及 _recursive=true 中的子文件夹)
gdrive-mime-type搜索特定的 mime_type
gdrive-mime-type-in-folderfolder_id 中搜索特定的 mime_type
gdrive-query-with-mime-type搜索带有特定 mime_typequery
gdrive-query-with-mime-type-and-folder搜索带有特定 mime_type 并在 folder_id 中的 query
retriever = GoogleDriveRetriever(
    template="gdrive-query",  # Search everywhere
    num_results=2,  # But take only 2 documents
)
for doc in retriever.invoke("machine learning"):
    print("---")
    print(doc.page_content.strip()[:60] + "...")
否则,您可以使用专门的 PromptTemplate 自定义提示。
from langchain_core.prompts import PromptTemplate

retriever = GoogleDriveRetriever(
    template=PromptTemplate(
        input_variables=["query"],
        # See https://developers.google.com/drive/api/guides/search-files
        template="(fullText contains '{query}') "
        "and mimeType='application/vnd.google-apps.document' "
        "and modifiedTime > '2000-01-01T00:00:00' "
        "and trashed=false",
    ),
    num_results=2,
    # See https://developers.google.com/drive/api/v3/reference/files/list
    includeItemsFromAllDrives=False,
    supportsAllDrives=False,
)
for doc in retriever.invoke("machine learning"):
    print(f"{doc.metadata['name']}:")
    print("---")
    print(doc.page_content.strip()[:60] + "...")

使用 Google Drive 的“描述”元数据

每个 Google Drive 在元数据中都有一个 description 字段(参见文件详细信息)。使用 snippets 模式返回所选文件的描述。
retriever = GoogleDriveRetriever(
    template="gdrive-mime-type-in-folder",
    folder_id=folder_id,
    mime_type="application/vnd.google-apps.document",  # Only Google Docs
    num_results=2,
    mode="snippets",
    includeItemsFromAllDrives=False,
    supportsAllDrives=False,
)
retriever.invoke("machine learning")

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