이 노트북은 LangChain을 Google Drive API에 연결하는 과정을 안내합니다.

사전 요구사항

  1. Google Cloud 프로젝트를 생성하거나 기존 프로젝트를 사용합니다
  2. Google Drive API를 활성화합니다
  3. 데스크톱 앱용 자격 증명 승인
  4. pip install -U google-api-python-client google-auth-httplib2 google-auth-oauthlib

Google Docs 데이터 검색 지침

기본적으로 GoogleDriveToolsGoogleDriveWrappercredentials.json 파일이 ~/.credentials/credentials.json에 있을 것으로 예상하지만, GOOGLE_ACCOUNT_FILE 환경 변수를 custom/path/to/credentials.json로 설정하여 구성할 수 있습니다. token.json의 위치는 동일한 디렉토리를 사용합니다(또는 token_path 매개변수를 사용). token.json은 도구를 처음 사용할 때 자동으로 생성됩니다. GoogleDriveSearchTool은 일부 요청으로 파일 선택을 검색할 수 있습니다. 기본적으로 folder_id를 사용하는 경우, 이름이 쿼리와 일치하면 이 폴더 내의 모든 파일을 Document로 검색할 수 있습니다.
pip install -qU  google-api-python-client google-auth-httplib2 google-auth-oauthlib langchain-community
URL에서 폴더 및 문서 ID를 얻을 수 있습니다: 특수 값 root는 개인 홈을 나타냅니다.
folder_id = "root"
# folder_id='1yucgL9WGgWZdM1TOuKkeghlPizuzMYb5'
기본적으로 다음 mime-type의 모든 파일을 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)
이를 업데이트하거나 사용자 정의할 수 있습니다. GoogleDriveAPIWrapper의 문서를 참조하세요. 단, 해당 패키지가 설치되어 있어야 합니다.
pip install -qU  unstructured langchain-googledrive
import os

from langchain_googledrive.tools.google_drive.tool import GoogleDriveSearchTool
from langchain_googledrive.utilities.google_drive import 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'."

ReAct agent 내에서 도구 사용하기

Google Jobs 도구를 사용하는 agent를 생성하려면 LangGraph를 설치하세요
pip install -qU langgraph langchain-openai
그리고 create_agent 기능을 사용하여 ReAct agent를 초기화합니다. OpenAI의 chat model에 액세스하려면 OPEN_API_KEY를 설정해야 합니다(platform.openai.com 방문).
import os

from langchain.chat_models import init_chat_model
from langchain.agents import create_agent


os.environ["OPENAI_API_KEY"] = "your-openai-api-key"


model = init_chat_model("gpt-4o-mini", model_provider="openai", temperature=0)
agent = create_agent(model, tools=[tool])

events = agent.stream(
    {"messages": [("user", "Search in google drive, who is 'Yann LeCun' ?")]},
    stream_mode="values",
)
for event in events:
    event["messages"][-1].pretty_print()

Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.
I