mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-01 19:12:42 +00:00
community[patch]: tools
imports (#21156)
Issue: we have several helper functions to import third-party libraries like tools.gmail.utils.import_google in [community.tools](https://api.python.langchain.com/en/latest/community_api_reference.html#id37). And we have core.utils.utils.guard_import that works exactly for this purpose. The import_<package> functions work inconsistently and rather be private functions. Change: replaced these functions with the guard_import function. Related to #21133
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
"""Gmail tool utils."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
from typing import TYPE_CHECKING, List, Optional, Tuple
|
||||
|
||||
from langchain_core.utils import guard_import
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from google.auth.transport.requests import Request
|
||||
from google.oauth2.credentials import Credentials
|
||||
@@ -21,16 +24,15 @@ def import_google() -> Tuple[Request, Credentials]:
|
||||
Returns:
|
||||
Tuple[Request, Credentials]: Request and Credentials classes.
|
||||
"""
|
||||
# google-auth-httplib2
|
||||
try:
|
||||
from google.auth.transport.requests import Request
|
||||
from google.oauth2.credentials import Credentials
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"You need to install google-auth-httplib2 to use this toolkit. "
|
||||
"Try running pip install --upgrade google-auth-httplib2"
|
||||
)
|
||||
return Request, Credentials
|
||||
return (
|
||||
guard_import(
|
||||
module_name="google.auth.transport.requests",
|
||||
pip_name="google-auth-httplib2",
|
||||
).Request,
|
||||
guard_import(
|
||||
module_name="google.oauth2.credentials", pip_name="google-auth-httplib2"
|
||||
).Credentials,
|
||||
)
|
||||
|
||||
|
||||
def import_installed_app_flow() -> InstalledAppFlow:
|
||||
@@ -39,14 +41,9 @@ def import_installed_app_flow() -> InstalledAppFlow:
|
||||
Returns:
|
||||
InstalledAppFlow: InstalledAppFlow class.
|
||||
"""
|
||||
try:
|
||||
from google_auth_oauthlib.flow import InstalledAppFlow
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"You need to install google-auth-oauthlib to use this toolkit. "
|
||||
"Try running pip install --upgrade google-auth-oauthlib"
|
||||
)
|
||||
return InstalledAppFlow
|
||||
return guard_import(
|
||||
module_name="google_auth_oauthlib.flow", pip_name="google-auth-oauthlib"
|
||||
).InstalledAppFlow
|
||||
|
||||
|
||||
def import_googleapiclient_resource_builder() -> build_resource:
|
||||
@@ -55,14 +52,9 @@ def import_googleapiclient_resource_builder() -> build_resource:
|
||||
Returns:
|
||||
build_resource: googleapiclient.discovery.build function.
|
||||
"""
|
||||
try:
|
||||
from googleapiclient.discovery import build
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"You need to install googleapiclient to use this toolkit. "
|
||||
"Try running pip install --upgrade google-api-python-client"
|
||||
)
|
||||
return build
|
||||
return guard_import(
|
||||
module_name="googleapiclient.discovery", pip_name="google-api-python-client"
|
||||
).build
|
||||
|
||||
|
||||
DEFAULT_SCOPES = ["https://mail.google.com/"]
|
||||
@@ -77,8 +69,19 @@ def get_gmail_credentials(
|
||||
) -> Credentials:
|
||||
"""Get credentials."""
|
||||
# From https://developers.google.com/gmail/api/quickstart/python
|
||||
Request, Credentials = import_google()
|
||||
InstalledAppFlow = import_installed_app_flow()
|
||||
Request, Credentials = (
|
||||
guard_import(
|
||||
module_name="google.auth.transport.requests",
|
||||
pip_name="google-auth-httplib2",
|
||||
).Request,
|
||||
guard_import(
|
||||
module_name="google.oauth2.credentials", pip_name="google-auth-httplib2"
|
||||
).Credentials,
|
||||
)
|
||||
|
||||
InstalledAppFlow = guard_import(
|
||||
module_name="google_auth_oauthlib.flow", pip_name="google-auth-oauthlib"
|
||||
).InstalledAppFlow
|
||||
creds = None
|
||||
scopes = scopes or DEFAULT_SCOPES
|
||||
token_file = token_file or DEFAULT_CREDS_TOKEN_FILE
|
||||
@@ -111,7 +114,9 @@ def build_resource_service(
|
||||
) -> Resource:
|
||||
"""Build a Gmail service."""
|
||||
credentials = credentials or get_gmail_credentials()
|
||||
builder = import_googleapiclient_resource_builder()
|
||||
builder = guard_import(
|
||||
module_name="googleapiclient.discovery", pip_name="google-api-python-client"
|
||||
).build
|
||||
return builder(service_name, service_version, credentials=credentials)
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user