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 [ ]:
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.")
Create the guardrail¶
In [ ]:
Copied!
from any_guardrail import AnyGuardrail, GuardrailName
from any_guardrail import AnyGuardrail, GuardrailName
In [ ]:
Copied!
guardrail = AnyGuardrail.create(GuardrailName.ANYLLM)
guardrail = AnyGuardrail.create(GuardrailName.ANYLLM)
Try it with different models / policies / inputs¶
In [ ]:
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 [ ]:
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)
In [ ]:
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)