Step 1 : Setting API key

Set up your Future AGI account and get started with the Future AGI Python client, follow steps here: https://docs.futureagi.com/future-agi/products/evaluations/quickstart

Step 2 : Installation and Setup

import os
from fi.evals import EvalClient
from fi.evals import ProtectClient

evaluator = EvalClient(fi_api_key="FI_API_KEY", 
                       fi_secret_key="FI_SECRET_KEY", 
                       fi_base_url="http://localhost:8000/")

protector = ProtectClient(evaluator=evaluator)

Step 3 : Setting up Protect Metrics

The protect class contains several arguments, these are listed as follows :

ArgumentTypeDefault ValueSignificance
inputstring-The input string that will be checked against the rules
protect_rulesList[Dict]-Specifies the evaluation rules to apply to the input string
actionstringResponse cannot be generated as the input fails the checksSpecifies the message to return when a rule check fails
reasonboolFalseDetermines whether to include the failure reason in the output
timeoutinteger30Maximum time in seconds allowed for rule checking

Defining Rules :

We define rules as a list of custom Protect metrics. Each Protect metric is a dictionary with some fixed keys.

Each Protect Metric is made up of different parameters as defined below :

KeyRequirementInput TypePossible ValuesDefault ValueFunction
metricRequiredstringToxicity, Tone, Sexism, Prompt Injection, Data PrivacyDefines the evaluation metric to be used
containsRequired for Tone onlylist[string]For Tone: neutral, joy, love, fear, surprise, sadness, anger, annoyance, confusionChecks if specified values are present in evaluation response
typeRequired for Tone onlystringFor Tone: any / allanyDetermines if rule passes when any or all contained values are present

Example:

{ 
		'metric': 'Metric Name',
		'contains': ['Output Type'], // Needed only for Tone 
		'type': 'any', // Needed only for Tone
}

You can define multiple rules. The evaluation stops as soon as 1 rule fails.

[
		{ 
				'metric': 'Tone',
				'contains': ['anger','fear'], 
				'type': 'any', 
		},
		{ 
				'metric': 'Toxicity',
		}
}

Understanding the Outputs

The returned output is in form of a dictionary with different keys, each of which convey various details related to performance of the input query when passed through the defined protection evaluations.

KeyPossible ValuesSignificance
statuspassed / failedIndicates if the input passed all protection rules. For timeouts, status reflects results of completed checks only.
messagesstringShows the action message if status is “Failed”; otherwise displays the original input text.
completed_ruleslist of completed rule checksLists all metrics evaluated within the timeout period.
uncompleted_ruleslist of uncompleted rule checksLists metrics not evaluated due to earlier failure or timeout.
failed_rulestringNames the rule check that failed. Returns None if all checks pass.
reasonstringProvides explanation for why the input failed the rule checks.
time_takenintegerTotal duration of protection evaluation process.
Output Format : 
{'status': 'passed', 'completed_rules': ['Prompt Injection', 'Toxicity', 'Sexism', 'Data Privacy', 'Tone'], 'uncompleted_rules': [], 'messages': 'I like apples', 'reason': "All checks passed" 'time_taken': 9.5367431640625e-07}

{'status': 'failed', 'completed_rules': ['Toxicity', 'Data Privacy'], 'uncompleted_rules': ['Prompt Injection', 'Tone', 'Sexism'], 'messages': 'I cannot process this request', 'reasons': "Text contains personal name 'Jack', violating data privacy standards under GDPR and CCPA.", 'time_taken': 9.5367431640625e-07}

Examples

Toxicity

// Example 1 : 

rules=[
						{
						        'metric': 'Toxicity',
						}
       ]
action = "This message cannot be displayed"

protection = protector.protect
								(
								    "This is a test message", protect_rules = rules, 
								    action = action, reason = True, timeout = 25  
								)
print(protection)

Tone

// Example 1 : 

rules=[
						{
						        'metric': 'Tone',
						        'contains': ["fear", "sadness", "anger", "annoyance"]
						        'type': 'any',
						}
       ]
action = "This message cannot be displayed"

protection = protector.protect
								(
								    "This is a test message", protect_rules = rules, 
								    action = action, reason = True, timeout = 25  
								)
print(protection)

Sexism

// Example 1 : 

rules=[
						{
						        'metric': 'Sexism',
						}
       ]
action = "This message cannot be displayed"

protection = protector.protect
								(
								    "This is a test message", protect_rules = rules, 
								    action = action, reason = True, timeout = 25  
								)
print(protection)

Prompt Injection

// Example 1 : 

rules=[
						{
						        'metric': 'Prompt Injection',
						}
       ]
action = "This message cannot be displayed"

protection = protector.protect
								(
								    "This is a test message", protect_rules = rules, 
								    action = action, reason = True, timeout = 25  
								)
print(protection)

Data Privacy

// Example 1 : 

rules=[
						{
						        'metric': 'Data Privacy',
						}
       ]
action = "This message cannot be displayed"

protection = protector.protect
								(
								    "This is a test message", protect_rules = rules, 
								    action = action, reason = True, timeout = 25  
								)
print(protection)

Defining Multiple rules together

rules = [
								{
						        'metric': 'Toxicity',
						    },
						    {
						        'metric': 'Prompt Injection',
						    },
						    {
						        'metric': 'Tone',
						        'contains': ['anger','annoyance'],
						        'type': 'all'
						    },
						    {
						        'metric': 'Data Privacy',
						    },
						    {
						        'metric': 'Sexism',
						    }
    ]

protection = protector.protect(
    "This is my input string",
    protect_rules=rules, action="I cannot process this request", reason=True, timeout=50
)
print(protection)