You can checkout the colab notebook to quickly get started with the FutureAGI Protect.
Installing FutureAGI SDK
Initializing FutureAGI Protect
from fi.evals import Protect
protector = Protect(fi_api_key="<your_api_key>",
fi_secret_key="<your_api_secret>") # Optional, if you want to set the API key and secret key manually
Click here to learn how to access your API keys.
It’s recommended to set the API key and secret key as environment variables.
Define Protect Rules and the Action to take
# Example Ruleset
rules = [
{
"metric": "Tone",
"contains": ["anger", "fear"],
"type": "any"
},
{
"metric": "Toxicity"
}
]
action = "This message cannot be displayed"
Apply Protect on a text
# Apply the rules to a text
response = protector.protect("Hello, world!",
protect_rules=rules,
action=action,
reason=True,
timeout=25)
print(response)
Example Script using Anthropic Client and FutureAGI Protect
# Define the environment variables
# export ANTHROPIC_API_KEY=<your_api_key>
# export FI_API_KEY=<your_api_key>
# export FI_SECRET_KEY=<your_api_secret>
from anthropic import Anthropic
from fi.evals import Protect
anthropic = Anthropic()
protector = Protect()
response = anthropic.messages.create(
max_tokens=1000,
model="claude-3-5-sonnet-20240620",
messages=[
{"role": "user", "content": "Hi, I am a student, Can you help me with my homework?"}
]
)
rules = [
{
"metric": "Tone",
"contains": ["anger", "fear"],
"type": "any"
},
{
"metric": "Toxicity"
}
]
action = "This message cannot be displayed"
response_to_protect = response.content[0].text
protect_response = protector.protect(response_to_protect,
protect_rules=rules,
action=action,
reason=True,
timeout=25)
print(protect_response)
print(response_to_protect)
Optionally you can just use the protect
function from the FutureAGI SDK, without initializing the Protect
class.
from fi.evals import protect
rules = [
{
"metric": "Tone",
"contains": ["anger", "fear"],
"type": "any"
},
]
action = "This message cannot be displayed"
protected_response = protect("Hello, world!",
protect_rules=rules,
action=action,
reason=True,
timeout=25)
print(protected_response)