guardrails-ai / guardrails

Adding guardrails to large language models.

Home Page:https://www.guardrailsai.com/docs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RestrictToTopic validator does not work in guard

Pratekh opened this issue · comments

Import Guard and Validator

from guardrails.hub import RestrictToTopic
from guardrails import Guard

Setup Guard

guard = Guard().use(
RestrictToTopic(
valid_topics=["sports"],
invalid_topics=["music"],
disable_classifier=True,
disable_llm=False,
on_fail="exception"
)
)

guard.validate("""
In Super Bowl LVII in 2023, the Chiefs clashed with the Philadelphia Eagles in a fiercely contested battle, ultimately emerging victorious with a score of 38-35.
""") # Validator passes

guard.validate("""
The Beatles were a charismatic English pop-rock band of the 1960s.
""") # Validator fails

How they modifying above code to utilize openai api key?

I'm getting this error:

OpenAIError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/tenacity/init.py in call(self, fn, *args, **kwargs)
381 try:
--> 382 result = fn(*args, **kwargs)
383 except BaseException: # noqa: B902

26 frames
OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

The above exception was the direct cause of the following exception:

RetryError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/tenacity/init.py in iter(self, retry_state)
324 if self.reraise:
325 raise retry_exc.reraise()
--> 326 raise retry_exc from fut.exception()
327
328 if self.wait:

RetryError: RetryError[<Future at 0x783bd95637c0 state=finished raised OpenAIError>

@Pratekh are you providing an OpenAI API Key as listed in the requirements for this validator? https://hub.guardrailsai.com/validator/tryolabs/restricttotopic

That seems to be the culprit according the error message you posted:
OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable