本笔记本介绍了如何从以下位置检索文档 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:
- * Folder: drive.google.com/drive/u/0/folders/1yucgL9WGgWZdM1TOuKkeghlPizuzMYb5 -> 文件夹 ID 是
"1yucgL9WGgWZdM1TOuKkeghlPizuzMYb5" - * Document: docs.google.com/document/d/1bfaMQ18_i56204VaQDVeAFpqEijJTgvurupdEDiaUQw/edit -> 文档 ID 是
"1bfaMQ18_i56204VaQDVeAFpqEijJTgvurupdEDiaUQw"
特殊值 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(笔记本 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-folder | 返回中的所有兼容文件 folder_id |
gdrive-query | 搜索 query 在所有驱动器中 |
gdrive-by-name | 按名称搜索文件 query |
gdrive-query-in-folder | 搜索 query in folder_id (并在 _recursive=true) |
gdrive-mime-type | 搜索特定 mime_type |
gdrive-mime-type-in-folder | 搜索特定 mime_type in folder_id |
gdrive-query-with-mime-type | 搜索 query 以及特定 mime_type |
gdrive-query-with-mime-type-and-folder | 搜索 query 以及特定 mime_type 并在 folder_id |
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 "description" 元数据
每个 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")