Google Drive
This notebook walks through connecting a LangChain to theGoogle Drive API
.
Prerequisites
- Create a Google Cloud project or use an existing project
- Enable theGoogle Drive API
- Authorize credentials for desktop app
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
Instructions for retrieving your Google Docs data
By default, theGoogleDriveTools
andGoogleDriveWrapper
expects thecredentials.json
file to be~/.credentials/credentials.json
, but this is configurable by setting theGOOGLE_ACCOUNT_FILE
environment variable to yourcustom/path/to/credentials.json
.The location oftoken.json
use the same directory (or use the parametertoken_path
). Note thattoken.json
will be created automatically the first time you use the tool.
GoogleDriveSearchTool
can retrieve a selection of files with some requests.
By default, If you use afolder_id
, all the files inside this folder can be retrieved toDocument
, if the name match the query.
%pip install--upgrade--quiet google-api-python-client google-auth-httplib2 google-auth-oauthlib langchain-community
You can obtain your folder and document id from the URL:
- Folder:https://drive.google.com/drive/u/0/folders/1yucgL9WGgWZdM1TOuKkeghlPizuzMYb5 -> folder id is
"1yucgL9WGgWZdM1TOuKkeghlPizuzMYb5"
- Document:https://docs.google.com/document/d/1bfaMQ18_i56204VaQDVeAFpqEijJTgvurupdEDiaUQw/edit -> document id is
"1bfaMQ18_i56204VaQDVeAFpqEijJTgvurupdEDiaUQw"
The special valueroot
is for your personal home.
folder_id="root"
# folder_id='1yucgL9WGgWZdM1TOuKkeghlPizuzMYb5'
By default, all files with these mime-type can be converted toDocument
.
- 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)
It's possible to update or customize this. See the documentation ofGoogleDriveAPIWrapper
.
But, the corresponding packages must installed.
%pip install--upgrade--quiet unstructured langchain-googledrive
import os
from langchain_googledrive.tools.google_drive.toolimport GoogleDriveSearchTool
from langchain_googledrive.utilities.google_driveimport GoogleDriveAPIWrapper
os.environ["GOOGLE_ACCOUNT_FILE"]="custom/path/to/credentials.json"
# By default, search only in the filename.
tool= GoogleDriveSearchTool(
api_wrapper=GoogleDriveAPIWrapper(
folder_id=folder_id,
num_results=2,
template="gdrive-query-in-folder",# Search in the body of documents
)
)
import logging
logging.basicConfig(level=logging.INFO)
tool.run("machine learning")
tool.description
"A wrapper around Google Drive Search. Useful for when you need to find a document in google drive. The input should be formatted as a list of entities separated with a space. As an example, a list of keywords is 'hello word'."
Use the tool within a ReAct agent
In order to create an agent that uses the Google Jobs tool install Langgraph
%pip install--upgrade--quiet langgraph langchain-openai
and use thecreate_react_agent
functionality to initialize a ReAct agent. You will also need to set up your OPEN_API_KEY (visithttps://platform.openai.com) in order to access OpenAI's chat models.
import os
from langchain.chat_modelsimport init_chat_model
from langgraph.prebuiltimport create_react_agent
os.environ["OPENAI_API_KEY"]="your-openai-api-key"
llm= init_chat_model("gpt-4o-mini", model_provider="openai", temperature=0)
agent= create_react_agent(llm, tools=[tool])
events= agent.stream(
{"messages":[("user","Search in google drive, who is 'Yann LeCun' ?")]},
stream_mode="values",
)
for eventin events:
event["messages"][-1].pretty_print()
Related
- Toolconceptual guide
- Toolhow-to guides