Google 云端硬盘
此笔记本介绍如何从Google Drive.
先决条件
- 创建 Google Cloud 项目或使用现有项目
- 启用 Google Drive API
- 为桌面应用程序授权凭据
pip install --upgrade 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:
- 文件夹:https://drive.google.com/drive/u/0/folders/1yucgL9WGgWZdM1TOuKkeghlPizuzMYb5 -> 文件夹 ID 为
"1yucgL9WGgWZdM1TOuKkeghlPizuzMYb5" - 文档:https://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/texttext/plaintext/htmltext/csvtext/markdownimage/pngimage/jpegapplication/epub+zipapplication/pdfapplication/rtfapplication/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 --upgrade --quiet unstructured
retriever.invoke("machine learning")
您可以自定义条件以选择文件。提出了一组预定义的过滤器:
| 模板 | 描述 |
|---|---|
gdrive-all-in-folder | Return all compatible files from a folder_id |
gdrive-query | Search query in all drives |
gdrive-by-name | Search file with name query |
gdrive-query-in-folder | Search query in folder_id (and sub-folders in _recursive=true) |
gdrive-mime-type | Search a specific mime_type |
gdrive-mime-type-in-folder | Search a specific mime_type in folder_id |
gdrive-query-with-mime-type | Search query with a specific mime_type |
gdrive-query-with-mime-type-and-folder | Search query with a specific mime_type and in 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] + "...")
API 参考:PromptTemplate
使用 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")