Skip to content

Commit

Permalink
chore: improve the error messages of LazyImport (#6316)
Browse files Browse the repository at this point in the history
* improve lazy import error messages

* revert changes to adaptive_model
  • Loading branch information
anakin87 authored Nov 16, 2023
1 parent 414cbcf commit c691412
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion haystack/nodes/file_converter/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
logger = logging.getLogger(__name__)

with LazyImport(
message="Run 'pip install farm-haystack[file-conversion]' or 'pip install " "azure-ai-formrecognizer>=3.2.0b2'"
message="Run 'pip install farm-haystack[file-conversion]' or 'pip install \"azure-ai-formrecognizer>=3.2.0b2\"'"
) as azure_import:
from azure.ai.formrecognizer import DocumentAnalysisClient, AnalyzeResult
from azure.core.credentials import AzureKeyCredential
Expand Down
4 changes: 2 additions & 2 deletions haystack/pipelines/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ def eval_beir(
from beir.datasets.data_loader import GenericDataLoader
from beir.retrieval.evaluation import EvaluateRetrieval
except ModuleNotFoundError as e:
raise HaystackError("beir is not installed. Please run `pip install beir`") from e
raise HaystackError("beir is not installed. Please run 'pip install beir'") from e

url = f"https://public.ukp.informatik.tu-darmstadt.de/thakur/BEIR/datasets/{dataset}.zip"
data_path = util.download_and_unzip(url, dataset_dir)
Expand Down Expand Up @@ -2126,7 +2126,7 @@ def draw(self, path: Path = Path("pipeline.png")):
except ImportError:
raise ImportError(
"Could not import `pygraphviz`. Please install via: \n"
"pip install pygraphviz\n"
"'pip install pygraphviz'\n"
"(You might need to run this first: apt install libgraphviz-dev graphviz )"
)

Expand Down
4 changes: 2 additions & 2 deletions haystack/preview/components/audio/whisper_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from haystack.preview.lazy_imports import LazyImport

with LazyImport(
"Run 'pip install transformers[torch]==4.34.1' to install torch and "
"'pip install --no-deps numba llvmlite 'openai-whisper>=20230918'' to install whisper."
"Run 'pip install transformers[torch]' to install torch and "
"'pip install --no-deps numba llvmlite \"openai-whisper>=20230918\"' to install whisper."
) as whisper_import:
import torch
import whisper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from haystack.preview.lazy_imports import LazyImport

with LazyImport(message="Run 'pip install sentence-transformers>=2.2.0'") as sentence_transformers_import:
with LazyImport(message="Run 'pip install \"sentence-transformers>=2.2.0\"'") as sentence_transformers_import:
from sentence_transformers import SentenceTransformer


Expand Down
2 changes: 1 addition & 1 deletion haystack/preview/components/file_converters/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from haystack.preview import component, Document, default_to_dict


with LazyImport(message="Run 'pip install azure-ai-formrecognizer>=3.2.0b2'") as azure_import:
with LazyImport(message="Run 'pip install \"azure-ai-formrecognizer>=3.2.0b2\"'") as azure_import:
from azure.ai.formrecognizer import DocumentAnalysisClient, AnalyzeResult
from azure.core.credentials import AzureKeyCredential

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
logger = logging.getLogger(__name__)


with LazyImport(message="Run 'pip install transformers[torch,sentencepiece]==4.34.1'") as torch_and_transformers_import:
with LazyImport(message="Run 'pip install transformers[torch,sentencepiece]'") as torch_and_transformers_import:
import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer

Expand Down
4 changes: 1 addition & 3 deletions haystack/preview/components/readers/extractive.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
from haystack.preview import component, default_to_dict, ComponentError, Document, ExtractedAnswer
from haystack.preview.lazy_imports import LazyImport

with LazyImport(
"Run 'pip install transformers[torch,sentencepiece]==4.34.1 sentence-transformers>=2.2.0'"
) as torch_and_transformers_import:
with LazyImport("Run 'pip install transformers[torch,sentencepiece]'") as torch_and_transformers_import:
from transformers import AutoModelForQuestionAnswering, AutoTokenizer
from tokenizers import Encoding
import torch
Expand Down
2 changes: 1 addition & 1 deletion haystack/preview/components/samplers/top_p.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
logger = logging.getLogger(__name__)


with LazyImport(message="Run 'pip install torch>=1.13'") as torch_import:
with LazyImport(message="Run 'pip install \"torch>=1.13\"'") as torch_import:
import torch


Expand Down
2 changes: 1 addition & 1 deletion haystack/utils/import_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def load_documents_from_hf_datasets(dataset_name: str, split: Optional[str] = "t
from datasets import load_dataset, load_dataset_builder
except ImportError:
raise ImportError(
"Failed to import `datasets`, Run 'pip install datasets>=2.6.0' "
"Failed to import `datasets`, Run 'pip install \"datasets>=2.6.0\"' "
"to install the datasets library to use this function."
)

Expand Down

0 comments on commit c691412

Please sign in to comment.