|
5 | 5 | from dreadnode.metric import Metric |
6 | 6 | from dreadnode.scorers.base import Scorer |
7 | 7 | from dreadnode.scorers.util import cosine_similarity |
8 | | -from dreadnode.util import warn_at_user_stacklevel |
| 8 | +from dreadnode.util import generate_import_error_msg, warn_at_user_stacklevel |
9 | 9 |
|
10 | 10 | if t.TYPE_CHECKING: |
11 | 11 | from sentence_transformers import ( # type: ignore[import-not-found] |
@@ -88,7 +88,7 @@ def similarity_with_rapidfuzz( |
88 | 88 | score_cutoff: Optional score cutoff below which to return 0.0. |
89 | 89 | name: Name of the scorer. |
90 | 90 | """ |
91 | | - rapidfuzz_import_error_msg = "RapidFuzz dependency is not installed. Please install it with: pip install rapidfuzz or dreadnode[text]" |
| 91 | + rapidfuzz_import_error_msg = generate_import_error_msg("rapidfuzz", "text") |
92 | 92 | try: |
93 | 93 | from rapidfuzz import fuzz, utils # type: ignore[import-not-found] |
94 | 94 | except ImportError: |
@@ -182,7 +182,7 @@ def string_distance( |
182 | 182 | normalize: Normalize distances and convert to similarity scores. |
183 | 183 | name: Name of the scorer. |
184 | 184 | """ |
185 | | - rapidfuzz_import_error_msg = "RapidFuzz dependency is not installed. Please install it with: pip install rapidfuzz or dreadnode[text]" |
| 185 | + rapidfuzz_import_error_msg = generate_import_error_msg("rapidfuzz", "text") |
186 | 186 |
|
187 | 187 | try: |
188 | 188 | from rapidfuzz import distance # type: ignore[import-not-found] |
@@ -251,7 +251,7 @@ def similarity_with_tf_idf(reference: str, *, name: str = "similarity") -> "Scor |
251 | 251 | reference: The reference text (e.g., expected output). |
252 | 252 | name: Name of the scorer. |
253 | 253 | """ |
254 | | - sklearn_import_error_msg = "scikit-learn dependency is not installed. Please install it with: pip install scikit-learn or dreadnode[text]" |
| 254 | + sklearn_import_error_msg = generate_import_error_msg("scikit-learn", "text") |
255 | 255 |
|
256 | 256 | try: |
257 | 257 | from sklearn.feature_extraction.text import ( # type: ignore[import-not-found] |
@@ -303,7 +303,7 @@ def similarity_with_sentence_transformers( |
303 | 303 | model_name: The name of the sentence-transformer model to use. |
304 | 304 | name: Name of the scorer. |
305 | 305 | """ |
306 | | - sentence_transformers_error_msg = "Sentence transformers dependency is not installed. Please install it with: pip install sentence-transformers or dreadnode[training]" |
| 306 | + sentence_transformers_error_msg = generate_import_error_msg("sentence-transformers", "training") |
307 | 307 |
|
308 | 308 | try: |
309 | 309 | from sentence_transformers import ( # type: ignore[import-not-found] |
@@ -367,7 +367,7 @@ def similarity_with_litellm( |
367 | 367 | or self-hosted models. |
368 | 368 | name: Name of the scorer. |
369 | 369 | """ |
370 | | - litellm_import_error_msg = "litellm dependency is not installed. Please install it with: pip install litellm or dreadnode[text]" |
| 370 | + litellm_import_error_msg = generate_import_error_msg("litellm", "text") |
371 | 371 | try: |
372 | 372 | import litellm |
373 | 373 | except ImportError: |
@@ -430,7 +430,7 @@ def bleu( |
430 | 430 | weights: Weights for unigram, bigram, etc. Must sum to 1. |
431 | 431 | name: Name of the scorer. |
432 | 432 | """ |
433 | | - nltk_import_error_msg = "NLTK dependency is not installed. Install with: pip install nltk && python -m nltk.downloader punkt" |
| 433 | + nltk_import_error_msg = generate_import_error_msg("nltk", "text") |
434 | 434 |
|
435 | 435 | try: |
436 | 436 | import nltk # type: ignore[import-not-found] |
|
0 commit comments