Google Drive
本筆記說明如何從 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/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 --upgrade --quiet unstructured
retriever.invoke("machine learning")
您可以自訂選擇檔案的條件。 提供了一組預定義的篩選器
範本 | 描述 |
---|---|
gdrive-all-in-folder | 從 folder_id 傳回所有相容的檔案 |
gdrive-query | 在所有磁碟機中搜尋 query |
gdrive-by-name | 搜尋名稱為 query 的檔案 |
gdrive-query-in-folder | 在 folder_id 中搜尋 query (以及 _recursive=true 中的子資料夾) |
gdrive-mime-type | 搜尋特定的 mime_type |
gdrive-mime-type-in-folder | 在 folder_id 中搜尋特定的 mime_type |
gdrive-query-with-mime-type | 使用特定的 mime_type 搜尋 query |
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] + "...")
API 參考資料:PromptTemplate
使用 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")