File tree Expand file tree Collapse file tree 2 files changed +14
-3
lines changed Expand file tree Collapse file tree 2 files changed +14
-3
lines changed Original file line number Diff line number Diff line change 44
55import openai
66from tenacity import retry , stop_after_attempt , wait_random_exponential
7- from transformers import pipeline
87
98from guardrails .utils .casting_utils import to_int
109from guardrails .utils .openai_utils import OpenAIClient
1615 register_validator ,
1716)
1817
18+ try :
19+ from transformers import pipeline
20+ except ImportError :
21+ pipeline = None
22+
1923
2024@register_validator (name = "on_topic" , data_type = "string" )
2125class OnTopic (Validator ):
@@ -94,6 +98,13 @@ def __init__(
9498 )
9599 self ._valid_topics = valid_topics
96100
101+ if pipeline is None :
102+ raise ValueError (
103+ "You must install transformers in order to "
104+ "use the OnTopic validator."
105+ "Install it using `pip install transformers`."
106+ )
107+
97108 if invalid_topics is None :
98109 self ._invalid_topics = []
99110 else :
@@ -209,7 +220,7 @@ def openai_callable(text: str, topics: List[str]) -> str:
209220 def get_topic_zero_shot (
210221 self , text : str , candidate_topics : List [str ]
211222 ) -> Tuple [str , float ]:
212- classifier = pipeline (
223+ classifier = pipeline ( # type: ignore
213224 "zero-shot-classification" ,
214225 model = self ._model ,
215226 device = self ._device ,
Original file line number Diff line number Diff line change @@ -53,7 +53,7 @@ pydash = "^7.0.6"
5353[tool .poetry .extras ]
5454sql = [" sqlvalidator" , " sqlalchemy" , " sqlglot" ]
5555summary = [" thefuzz" , " nltk" ]
56- vectordb = [" faiss-cpu" , " tiktoken " , " numpy" ]
56+ vectordb = [" faiss-cpu" , " numpy" ]
5757profanity = [" alt-profanity-check" ]
5858detect-secrets = [" detect-secrets" ]
5959manifest = [" manifest-ml" ]
You can’t perform that action at this time.
0 commit comments