Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RestrictToTopic validator does not work in guard #719

Open
Pratekh opened this issue Apr 17, 2024 · 1 comment
Open

RestrictToTopic validator does not work in guard #719

Pratekh opened this issue Apr 17, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@Pratekh
Copy link

Pratekh commented Apr 17, 2024

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 Pratekh added the bug Something isn't working label Apr 17, 2024
@CalebCourier
Copy link
Collaborator

@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

@zsimjee zsimjee changed the title !guardrails hub install hub://tryolabs/restricttotopic RestrictToTopic validator does not work in guard Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants