Skip to content

Instantly share code, notes, and snippets.

@jkfran
Created May 23, 2023 15:08
Show Gist options
  • Save jkfran/0b5e21396785f1a177ad7f356a748117 to your computer and use it in GitHub Desktop.
Save jkfran/0b5e21396785f1a177ad7f356a748117 to your computer and use it in GitHub Desktop.
LangChain + Google Drive using service account (Using an Environment variable instead of a file)
import os
import json
import tempfile
from langchain.document_loaders import GoogleDriveLoader
# Load service account key from environment variable
service_account_data = json.loads(os.getenv("GOOGLE_SERVICE_ACCOUNT"))
# Create a temporary file and write the service account data to it
with tempfile.NamedTemporaryFile(mode="w+", suffix=".json", delete=False) as temp_file:
json.dump(service_account_data, temp_file)
temp_file_path = temp_file.name
loader = GoogleDriveLoader(
service_account_key=temp_file_path,
folder_id="xxxxxxxxxxxxxxxxxxxx",
file_types=["document", "sheet"],
recursive=False,
)
docs = loader.load()
print(docs)
os.unlink(temp_file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment