Install dependencies¶
In [ ]:
Copied!
%pip install 'any-guardrail'
import nest_asyncio
nest_asyncio.apply()
%pip install 'any-guardrail'
import nest_asyncio
nest_asyncio.apply()
We will be using a model from openai
by default, but you can check
the different providers supported in any-llm
:
In [8]:
Copied!
import os
from getpass import getpass
if "OPENAI_API_KEY" not in os.environ:
print("OPENAI_API_KEY not found in environment!")
api_key = getpass("Please enter your OPENAI_API_KEY: ")
os.environ["OPENAI_API_KEY"] = api_key
print("OPENAI_API_KEY set for this session!")
else:
print("OPENAI_API_KEY found in environment.")
import os
from getpass import getpass
if "OPENAI_API_KEY" not in os.environ:
print("OPENAI_API_KEY not found in environment!")
api_key = getpass("Please enter your OPENAI_API_KEY: ")
os.environ["OPENAI_API_KEY"] = api_key
print("OPENAI_API_KEY set for this session!")
else:
print("OPENAI_API_KEY found in environment.")
OPENAI_API_KEY found in environment.
Create the guardrail¶
In [3]:
Copied!
from any_guardrail import AnyGuardrail, GuardrailName
from any_guardrail import AnyGuardrail, GuardrailName
In [4]:
Copied!
guardrail = AnyGuardrail.create(GuardrailName.ANYLLM)
guardrail = AnyGuardrail.create(GuardrailName.ANYLLM)
Try it with different models / policies / inputs¶
In [5]:
Copied!
MODEL_ID = "openai/gpt-5-nano"
POLICY = """
You hate Mondays.
You must reject any request related with planning activities on Mondays.
"""
MODEL_ID = "openai/gpt-5-nano"
POLICY = """
You hate Mondays.
You must reject any request related with planning activities on Mondays.
"""
In [6]:
Copied!
guardrail.validate("Can you suggest me some restaurants for lunch on Monday?", policy=POLICY, model_id=MODEL_ID)
guardrail.validate("Can you suggest me some restaurants for lunch on Monday?", policy=POLICY, model_id=MODEL_ID)
Out[6]:
GuardrailOutput(valid=False, explanation='The user requested planning lunch options specifically for Monday, which is planning activities on Monday. The policy requires rejecting Monday planning requests.', score=0.92)
In [7]:
Copied!
guardrail.validate("Can you suggest me some restaurants for lunch on Friday?", policy=POLICY, model_id=MODEL_ID)
guardrail.validate("Can you suggest me some restaurants for lunch on Friday?", policy=POLICY, model_id=MODEL_ID)
Out[7]:
GuardrailOutput(valid=True, explanation='The user asks for lunch restaurant suggestions on Friday. The policy forbids planning activities on Mondays, which does not apply here.', score=0.87)