Skip to content

Commit

Permalink
community[minor]: import fix (#20995)
Browse files Browse the repository at this point in the history
Issue: When the third-party package is not installed, whenever we need
to `pip install <package>` the ImportError is raised.
But sometimes, the `ValueError` or `ModuleNotFoundError` is raised. It
is bad for consistency.
Change: replaced the `ValueError` or `ModuleNotFoundError` with
`ImportError` when we raise an error with the `pip install <package>`
message.
Note: Ideally, we replace all `try: import... except... raise ... `with
helper functions like `import_aim` or just use the existing
[langchain_core.utils.utils.guard_import](https://api.python.langchain.com/en/latest/utils/langchain_core.utils.utils.guard_import.html#langchain_core.utils.utils.guard_import)
But it would be much bigger refactoring. @baskaryan Please, advice on
this.
  • Loading branch information
leo-gan committed Apr 29, 2024
1 parent 2ddac9a commit dc7c06b
Show file tree
Hide file tree
Showing 65 changed files with 103 additions and 103 deletions.
10 changes: 5 additions & 5 deletions libs/community/langchain_community/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def __init__(self, redis_: Any, *, ttl: Optional[int] = None):
try:
from upstash_redis import Redis
except ImportError:
raise ValueError(
raise ImportError(
"Could not import upstash_redis python package. "
"Please install it with `pip install upstash_redis`."
)
Expand Down Expand Up @@ -461,7 +461,7 @@ def __init__(self, redis_: Any, *, ttl: Optional[int] = None):
try:
from redis import Redis
except ImportError:
raise ValueError(
raise ImportError(
"Could not import `redis` python package. "
"Please install it with `pip install redis`."
)
Expand Down Expand Up @@ -528,7 +528,7 @@ def __init__(self, redis_: Any, *, ttl: Optional[int] = None):
try:
from redis.asyncio import Redis
except ImportError:
raise ValueError(
raise ImportError(
"Could not import `redis.asyncio` python package. "
"Please install it with `pip install redis`."
)
Expand Down Expand Up @@ -1069,7 +1069,7 @@ def __init__(
try:
from cassio.table import ElasticCassandraTable
except (ImportError, ModuleNotFoundError):
raise ValueError(
raise ImportError(
"Could not import cassio python package. "
"Please install it with `pip install cassio`."
)
Expand Down Expand Up @@ -1192,7 +1192,7 @@ def __init__(
try:
from cassio.table import MetadataVectorCassandraTable
except (ImportError, ModuleNotFoundError):
raise ValueError(
raise ImportError(
"Could not import cassio python package. "
"Please install it with `pip install cassio`."
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(
try:
import neo4j
except ImportError:
raise ValueError(
raise ImportError(
"Could not import neo4j python package. "
"Please install it with `pip install neo4j`."
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(
try:
from xata.client import XataClient # noqa: F401
except ImportError:
raise ValueError(
raise ImportError(
"Could not import xata python package. "
"Please install it with `pip install xata`."
)
Expand Down
2 changes: 1 addition & 1 deletion libs/community/langchain_community/chat_models/anyscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def validate_environment(cls, values: dict) -> dict:
import openai

except ImportError as e:
raise ValueError(
raise ImportError(
"Could not import openai python package. "
"Please install it with `pip install openai`.",
) from e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def validate_environment(cls, values: Dict) -> Dict:

values["client"] = qianfan.ChatCompletion(**params)
except ImportError:
raise ValueError(
raise ImportError(
"qianfan package not found, please install it with "
"`pip install qianfan`"
)
Expand Down
2 changes: 1 addition & 1 deletion libs/community/langchain_community/chat_models/everlyai.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def validate_environment_override(cls, values: dict) -> dict:
import openai

except ImportError as e:
raise ValueError(
raise ImportError(
"Could not import openai python package. "
"Please install it with `pip install openai`.",
) from e
Expand Down
2 changes: 1 addition & 1 deletion libs/community/langchain_community/chat_models/jinachat.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def validate_environment(cls, values: Dict) -> Dict:
import openai

except ImportError:
raise ValueError(
raise ImportError(
"Could not import openai python package. "
"Please install it with `pip install openai`."
)
Expand Down
2 changes: 1 addition & 1 deletion libs/community/langchain_community/chat_models/konko.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def validate_environment(cls, values: Dict) -> Dict:
import konko

except ImportError:
raise ValueError(
raise ImportError(
"Could not import konko python package. "
"Please install it with `pip install konko`."
)
Expand Down
2 changes: 1 addition & 1 deletion libs/community/langchain_community/chat_models/mlx.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def _stream(
from mlx_lm.utils import generate_step

except ImportError:
raise ValueError(
raise ImportError(
"Could not import mlx_lm python package. "
"Please install it with `pip install mlx_lm`."
)
Expand Down
2 changes: 1 addition & 1 deletion libs/community/langchain_community/chat_models/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def _import_tiktoken() -> Any:
try:
import tiktoken
except ImportError:
raise ValueError(
raise ImportError(
"Could not import tiktoken python package. "
"This is needed in order to calculate get_token_ids. "
"Please install it with `pip install tiktoken`."
Expand Down
2 changes: 1 addition & 1 deletion libs/community/langchain_community/chat_models/yuan2.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def validate_environment(cls, values: Dict) -> Dict:
import openai

except ImportError:
raise ValueError(
raise ImportError(
"Could not import openai python package. "
"Please install it with `pip install openai`."
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, **kwargs: Any):
try:
from optimum.intel.openvino import OVModelForSequenceClassification
except ImportError as e:
raise ValueError(
raise ImportError(
"Could not import optimum-intel python package. "
"Please install it with: "
"pip install -U 'optimum[openvino,nncf]'"
Expand All @@ -47,7 +47,7 @@ def __init__(self, **kwargs: Any):
try:
from huggingface_hub import HfApi
except ImportError as e:
raise ValueError(
raise ImportError(
"Could not import huggingface_hub python package. "
"Please install it with: "
"`pip install -U huggingface_hub`."
Expand Down
4 changes: 2 additions & 2 deletions libs/community/langchain_community/document_loaders/athena.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __init__(
try:
import boto3
except ImportError:
raise ModuleNotFoundError(
raise ImportError(
"Could not import boto3 python package. "
"Please install it with `pip install boto3`."
)
Expand Down Expand Up @@ -115,7 +115,7 @@ def _get_result_set(self, query_execution_id: str) -> Any:
try:
import pandas as pd
except ImportError:
raise ModuleNotFoundError(
raise ImportError(
"Could not import pandas python package. "
"Please install it with `pip install pandas`."
)
Expand Down
6 changes: 3 additions & 3 deletions libs/community/langchain_community/document_loaders/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ def __init__(
try:
import textractcaller as tc # noqa: F401
except ImportError:
raise ModuleNotFoundError(
raise ImportError(
"Could not import amazon-textract-caller python package. "
"Please install it with `pip install amazon-textract-caller`."
)
Expand Down Expand Up @@ -662,7 +662,7 @@ def __init__(
client = session.client("textract", **client_params)

except ImportError:
raise ModuleNotFoundError(
raise ImportError(
"Could not import boto3 python package. "
"Please install it with `pip install boto3`."
)
Expand Down Expand Up @@ -710,7 +710,7 @@ def _get_number_of_pages(blob: Blob) -> int: # type: ignore[valid-type]
from PIL import Image, ImageSequence

except ImportError:
raise ModuleNotFoundError(
raise ImportError(
"Could not import pypdf or Pilloe python package. "
"Please install it with `pip install pypdf Pillow`."
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(
try:
import unstructured # noqa:F401
except ImportError:
raise ValueError(
raise ImportError(
"unstructured package not found, please install it with "
"`pip install unstructured`"
)
Expand Down
8 changes: 4 additions & 4 deletions libs/community/langchain_community/embeddings/aleph_alpha.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def validate_environment(cls, values: Dict) -> Dict:
nice=values["nice"],
)
except ImportError:
raise ValueError(
raise ImportError(
"Could not import aleph_alpha_client python package. "
"Please install it with `pip install aleph_alpha_client`."
)
Expand All @@ -121,7 +121,7 @@ def embed_documents(self, texts: List[str]) -> List[List[float]]:
SemanticRepresentation,
)
except ImportError:
raise ValueError(
raise ImportError(
"Could not import aleph_alpha_client python package. "
"Please install it with `pip install aleph_alpha_client`."
)
Expand Down Expand Up @@ -161,7 +161,7 @@ def embed_query(self, text: str) -> List[float]:
SemanticRepresentation,
)
except ImportError:
raise ValueError(
raise ImportError(
"Could not import aleph_alpha_client python package. "
"Please install it with `pip install aleph_alpha_client`."
)
Expand Down Expand Up @@ -209,7 +209,7 @@ def _embed(self, text: str) -> List[float]:
SemanticRepresentation,
)
except ImportError:
raise ValueError(
raise ImportError(
"Could not import aleph_alpha_client python package. "
"Please install it with `pip install aleph_alpha_client`."
)
Expand Down
2 changes: 1 addition & 1 deletion libs/community/langchain_community/embeddings/bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def validate_environment(cls, values: Dict) -> Dict:
values["client"] = session.client("bedrock-runtime", **client_params)

except ImportError:
raise ModuleNotFoundError(
raise ImportError(
"Could not import boto3 python package. "
"Please install it with `pip install boto3`."
)
Expand Down
2 changes: 1 addition & 1 deletion libs/community/langchain_community/embeddings/cohere.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def validate_environment(cls, values: Dict) -> Dict:
client_name=client_name,
)
except ImportError:
raise ValueError(
raise ImportError(
"Could not import cohere python package. "
"Please install it with `pip install cohere`."
)
Expand Down
2 changes: 1 addition & 1 deletion libs/community/langchain_community/embeddings/llamacpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def validate_environment(cls, values: Dict) -> Dict:

values["client"] = Llama(model_path, embedding=True, **model_params)
except ImportError:
raise ModuleNotFoundError(
raise ImportError(
"Could not import llama-cpp-python library. "
"Please install the llama-cpp-python library to "
"use this embedding model: pip install llama-cpp-python"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def make_security_token_signer(oci_config): # type: ignore[no-untyped-def]
)

except ImportError as ex:
raise ModuleNotFoundError(
raise ImportError(
"Could not import oci python package. "
"Please make sure you have the oci package installed."
) from ex
Expand Down
4 changes: 2 additions & 2 deletions libs/community/langchain_community/embeddings/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ def _get_len_safe_embeddings(
try:
from transformers import AutoTokenizer
except ImportError:
raise ValueError(
raise ImportError(
"Could not import transformers python package. "
"This is needed in order to for OpenAIEmbeddings without "
"`tiktoken`. Please install it with `pip install transformers`. "
Expand Down Expand Up @@ -557,7 +557,7 @@ async def _aget_len_safe_embeddings(
try:
from transformers import AutoTokenizer
except ImportError:
raise ValueError(
raise ImportError(
"Could not import transformers python package. "
"This is needed in order to for OpenAIEmbeddings without "
" `tiktoken`. Please install it with `pip install transformers`."
Expand Down
4 changes: 2 additions & 2 deletions libs/community/langchain_community/embeddings/openvino.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(self, **kwargs: Any):
try:
from optimum.intel.openvino import OVModelForFeatureExtraction
except ImportError as e:
raise ValueError(
raise ImportError(
"Could not import optimum-intel python package. "
"Please install it with: "
"pip install -U 'optimum[openvino,nncf]'"
Expand All @@ -60,7 +60,7 @@ def __init__(self, **kwargs: Any):
try:
from huggingface_hub import HfApi
except ImportError as e:
raise ValueError(
raise ImportError(
"Could not import huggingface_hub python package. "
"Please install it with: "
"`pip install -U huggingface_hub`."
Expand Down
2 changes: 1 addition & 1 deletion libs/community/langchain_community/graphs/age_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def __init__(
try:
import psycopg2
except ImportError:
raise ValueError(
raise ImportError(
"Could not import psycopg2 python package. "
"Please install it with `pip install psycopg2`."
)
Expand Down
2 changes: 1 addition & 1 deletion libs/community/langchain_community/graphs/gremlin_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(
if sys.platform == "win32":
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
except ImportError:
raise ValueError(
raise ImportError(
"Please install gremlin-python first: " "`pip3 install gremlinpython"
)

Expand Down
2 changes: 1 addition & 1 deletion libs/community/langchain_community/graphs/hugegraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(
try:
from hugegraph.connection import PyHugeGraph
except ImportError:
raise ValueError(
raise ImportError(
"Please install HugeGraph Python client first: "
"`pip3 install hugegraph-python`"
)
Expand Down
2 changes: 1 addition & 1 deletion libs/community/langchain_community/graphs/nebula_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(
import nebula3 # noqa: F401
import pandas # noqa: F401
except ImportError:
raise ValueError(
raise ImportError(
"Please install NebulaGraph Python client and pandas first: "
"`pip install nebula3-python pandas`"
)
Expand Down
2 changes: 1 addition & 1 deletion libs/community/langchain_community/graphs/neo4j_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def __init__(
try:
import neo4j
except ImportError:
raise ValueError(
raise ImportError(
"Could not import neo4j python package. "
"Please install it with `pip install neo4j`."
)
Expand Down
8 changes: 4 additions & 4 deletions libs/community/langchain_community/graphs/neptune_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,13 @@ def __init__(
self.client = session.client("neptune-graph")

except ImportError:
raise ModuleNotFoundError(
raise ImportError(
"Could not import boto3 python package. "
"Please install it with `pip install boto3`."
)
except Exception as e:
if type(e).__name__ == "UnknownServiceError":
raise ModuleNotFoundError(
raise ImportError(
"NeptuneGraph requires a boto3 version 1.34.40 or greater."
"Please install it with `pip install -U boto3`."
) from e
Expand Down Expand Up @@ -345,13 +345,13 @@ def __init__(
)

except ImportError:
raise ModuleNotFoundError(
raise ImportError(
"Could not import boto3 python package. "
"Please install it with `pip install boto3`."
)
except Exception as e:
if type(e).__name__ == "UnknownServiceError":
raise ModuleNotFoundError(
raise ImportError(
"NeptuneGraph requires a boto3 version 1.28.38 or greater."
"Please install it with `pip install -U boto3`."
) from e
Expand Down

0 comments on commit dc7c06b

Please sign in to comment.