mirror of
https://github.com/hwchase17/langchain.git
synced 2026-01-29 21:30:18 +00:00
311 lines
10 KiB
Plaintext
311 lines
10 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "0b02f34c",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Google Drive Loader\n",
|
|
"This notebook covers how to retrieve documents from Google Drive.\n",
|
|
"\n",
|
|
"## Prerequisites\n",
|
|
"\n",
|
|
"1. Create a Google Cloud project or use an existing project\n",
|
|
"1. Enable the [Google Drive API](https://console.cloud.google.com/flows/enableapi?apiid=drive.googleapis.com)\n",
|
|
"1. [Authorize credentials for desktop app](https://developers.google.com/drive/api/quickstart/python#authorize_credentials_for_a_desktop_application)\n",
|
|
"1. `pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib`\n",
|
|
"\n",
|
|
"## Instructions for retrieving your Google Docs data\n",
|
|
"By default, the `GoogleDriveLoader` expects the `credentials.json` file to be `~/.credentials/credentials.json`, but this is configurable using the `GOOGLE_ACCOUNT_FILE` environment variable. \n",
|
|
"The location of `token.json` use the same directory (or use the parameter `token_path`). Note that `token.json` will be created automatically the first time you use the loader.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a03b9067",
|
|
"metadata": {},
|
|
"source": [
|
|
"You can obtain your folder and document id from the URL:\n",
|
|
"* Folder: https://drive.google.com/drive/u/0/folders/1yucgL9WGgWZdM1TOuKkeghlPizuzMYb5 -> folder id is `\"1yucgL9WGgWZdM1TOuKkeghlPizuzMYb5\"`\n",
|
|
"* Document: https://docs.google.com/document/d/1bfaMQ18_i56204VaQDVeAFpqEijJTgvurupdEDiaUQw/edit -> document id is `\"1bfaMQ18_i56204VaQDVeAFpqEijJTgvurupdEDiaUQw\"`\n",
|
|
"\n",
|
|
"The special value `root` is for your personal home."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "6e40071c-3a65-4e26-b497-3e2be0bd86b9",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"#!pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "9bcb6cb1",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"folder_id='root'\n",
|
|
"#folder_id='1yucgL9WGgWZdM1TOuKkeghlPizuzMYb5'"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "878928a6-a5ae-4f74-b351-64e3b01733fe",
|
|
"metadata": {
|
|
"tags": []
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain.document_loaders import GoogleDriveLoader"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "2216c83f-68e4-4d2f-8ea2-5878fb18bbe7",
|
|
"metadata": {
|
|
"tags": []
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"loader = GoogleDriveLoader(\n",
|
|
" folder_id=folder_id,\n",
|
|
" recursive=False,\n",
|
|
" num_results=2, # Maximum number of file to load\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "de5be5d4",
|
|
"metadata": {},
|
|
"source": [
|
|
"By default, all files with these mime-type can be converted to `Document`.\n",
|
|
"- text/text\n",
|
|
"- text/plain\n",
|
|
"- text/html\n",
|
|
"- text/csv\n",
|
|
"- text/markdown\n",
|
|
"- image/png\n",
|
|
"- image/jpeg\n",
|
|
"- application/epub+zip\n",
|
|
"- application/pdf\n",
|
|
"- application/rtf\n",
|
|
"- application/vnd.google-apps.document (GDoc)\n",
|
|
"- application/vnd.google-apps.presentation (GSlide)\n",
|
|
"- application/vnd.google-apps.spreadsheet (GSheet)\n",
|
|
"- application/vnd.google.colaboratory (Notebook colab)\n",
|
|
"- application/vnd.openxmlformats-officedocument.presentationml.presentation (PPTX)\n",
|
|
"- application/vnd.openxmlformats-officedocument.wordprocessingml.document (DOCX)\n",
|
|
"\n",
|
|
"It's possible to update or customize this. See the documentation of `GDriveLoader`.\n",
|
|
"\n",
|
|
"But, the corresponding packages must be installed."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "1bca45c9",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"!pip install unstructured"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "8f3b6aa0-b45d-4e37-8c50-5bebe70fdb9d",
|
|
"metadata": {
|
|
"tags": []
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"for doc in loader.load():\n",
|
|
" print(\"---\")\n",
|
|
" print(doc.page_content.strip()[:60]+\"...\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "31170e71",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Customize the search pattern\n",
|
|
"\n",
|
|
"All parameter compatible with Google [`list()`](https://developers.google.com/drive/api/v3/reference/files/list)\n",
|
|
"API can be set.\n",
|
|
"\n",
|
|
"To specify the new pattern of the Google request, you can use a `PromptTemplate()`.\n",
|
|
"The variables for the prompt can be set with `kwargs` in the constructor.\n",
|
|
"Some pre-formated request are proposed (use `{query}`, `{folder_id}` and/or `{mime_type}`):\n",
|
|
"\n",
|
|
"You can customize the criteria to select the files. A set of predefined filter are proposed:\n",
|
|
"| template | description |\n",
|
|
"| -------------------------------------- | --------------------------------------------------------------------- |\n",
|
|
"| gdrive-all-in-folder | Return all compatible files from a `folder_id` |\n",
|
|
"| gdrive-query | Search `query` in all drives |\n",
|
|
"| gdrive-by-name | Search file with name `query` |\n",
|
|
"| gdrive-query-in-folder | Search `query` in `folder_id` (and sub-folders in `_recursive=true`) |\n",
|
|
"| gdrive-mime-type | Search a specific `mime_type` |\n",
|
|
"| gdrive-mime-type-in-folder | Search a specific `mime_type` in `folder_id` |\n",
|
|
"| gdrive-query-with-mime-type | Search `query` with a specific `mime_type` |\n",
|
|
"| gdrive-query-with-mime-type-and-folder | Search `query` with a specific `mime_type` and in `folder_id` |\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "0a47175f",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"loader = GoogleDriveLoader(\n",
|
|
" folder_id=folder_id,\n",
|
|
" recursive=False,\n",
|
|
" template=\"gdrive-query\", # Default template to use\n",
|
|
" query=\"machine learning\",\n",
|
|
" num_results=2, # Maximum number of file to load\n",
|
|
" supportsAllDrives=False, # GDrive `list()` parameter\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "100cf361",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"for doc in loader.load():\n",
|
|
" print(\"---\")\n",
|
|
" print(doc.page_content.strip()[:60]+\"...\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "74e6e3aa",
|
|
"metadata": {},
|
|
"source": [
|
|
"You can customize your pattern."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "dcf07ff7",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain.prompts.prompt import PromptTemplate\n",
|
|
"loader = GoogleDriveLoader(\n",
|
|
" folder_id=folder_id,\n",
|
|
" recursive=False,\n",
|
|
" template=PromptTemplate(\n",
|
|
" input_variables=[\"query\", \"query_name\"],\n",
|
|
" template=\"fullText contains '{query}' and name contains '{query_name}' and trashed=false\",\n",
|
|
" ), # Default template to use\n",
|
|
" query=\"machine learning\",\n",
|
|
" query_name=\"ML\", \n",
|
|
" num_results=2, # Maximum number of file to load\n",
|
|
")\n",
|
|
"for doc in loader.load():\n",
|
|
" print(\"---\")\n",
|
|
" print(doc.page_content.strip()[:60]+\"...\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "8e404472",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Modes for GSlide and GSheet\n",
|
|
"\n",
|
|
"The parameter `mode` accept differents values:\n",
|
|
"- `\"document\"`: return the body of each documents\n",
|
|
"- `\"snippets\"`: return the `description` of each files.\n",
|
|
"\n",
|
|
"\n",
|
|
"The parameter `gslide_mode` accept differents values:\n",
|
|
"- `\"single\"` : one document with `<PAGE BREAK>`\n",
|
|
"- `\"slide\"` : one document by slide\n",
|
|
"- `\"elements\"` : one document for each `elements`."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "b33d1a53",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"loader = GoogleDriveLoader(\n",
|
|
" template=\"gdrive-mime-type\",\n",
|
|
" mime_type=\"application/vnd.google-apps.presentation\", # Only GSlide files\n",
|
|
" gslide_mode=\"slide\",\n",
|
|
" num_results=2, # Maximum number of file to load\n",
|
|
")\n",
|
|
"for doc in loader.load():\n",
|
|
" print(\"---\")\n",
|
|
" print(doc.page_content.strip()[:60]+\"...\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "498f0451",
|
|
"metadata": {},
|
|
"source": [
|
|
"The parameter `gsheet_mode` accept differents values:\n",
|
|
"- `\"single\"`: Generate one document by line\n",
|
|
"- `\"elements\"` : one document with markdown array and `<PAGE BREAK>` tags."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "884c4ca6",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"loader = GoogleDriveLoader(\n",
|
|
" template=\"gdrive-mime-type\",\n",
|
|
" mime_type=\"application/vnd.google-apps.spreadsheet\", # Only GSheet files\n",
|
|
" gsheet_mode=\"elements\",\n",
|
|
" num_results=2, # Maximum number of file to load\n",
|
|
")\n",
|
|
"for doc in loader.load():\n",
|
|
" print(\"---\")\n",
|
|
" print(doc.page_content.strip()[:60]+\"...\")"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.10.9"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|