Google Cloud端硬盘
这个笔记本介绍了如何从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:
- Folder: https://drive.google.com/drive/u/0/folders/1yucgL9WGgWZdM1TOuKkeghlPizuzMYb5 -> folder id is
"1yucgL9WGgWZdM1TOuKkeghlPizuzMYb5" - 文档: https://docs.google.com/document/d/1bfaMQ18_i56204VaQDVeAFpqEijJTgvurupdEDiaUQw/edit -> 文档 ID 为
"1bfaMQ18_i56204VaQDVeAFpqEijJTgvurupdEDiaUQw"
The special value root is for your personal home.
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] + "...")
else,您可以使用专门的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 参考:提示模板
使用GoogleDrive的'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")