Created
May 23, 2023 15:08
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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