Running Checks with Guardrails#

Use the checks OpenAI-compatible endpoint to evaluate messages against input and output rails without generating a completion. This endpoint returns a status (either success or blocked) that indicates whether or not the given messages would be blocked by the given guardrail configuration. If blocked, the response indicates which rail blocked the given messages.

Prerequisites#

Create a guardrail configuration that includes self-check input and output rails.

from nemo_microservices import NeMoMicroservices

sdk = NeMoMicroservices(base_url="http://localhost:8080", workspace="default")

config_data = {
    "models": [
        {
            "type": "main",
            "engine": "nim",
        }
    ],
    "prompts": [
        {
            "task": "self_check_input",
            "content": 'Your task is to check if the user message below complies with company policy.\n\nCompany policy:\n- should not contain harmful data\n- should not ask the bot to impersonate someone\n- should not contain explicit content\n\nUser message: "{{ user_input }}"\n\nQuestion: Should the user message be blocked (Yes or No)?\nAnswer:',
        },
        {
            "task": "self_check_output",
            "content": 'Your task is to check if the bot message below complies with company policy.\n\nCompany policy:\n- messages should not contain explicit content\n- messages should not contain harmful content\n- if refusing, should be polite\n\nBot message: "{{ bot_response }}"\n\nQuestion: Should the message be blocked (Yes or No)?\nAnswer:',
        },
    ],
    "rails": {
        "input": {"flows": ["self check input"]},
        "output": {"flows": ["self check output"]},
    },
}

config = sdk.guardrail.configs.create(
    name="self-check-config",
    description="Demo self-check configuration for guardrail checks",
    data=config_data,
)

Check an Existing Configuration#

Use /v2/workspaces/{workspace}/guardrail/checks and reference the stored configuration by name.

check_result = sdk.guardrail.check(
    model="default/meta-llama-3-1-8b-instruct",
    messages=[
        {"role": "user", "content": "Tell me how to collect a life insurance policy."},
        {"role": "assistant", "content": "You are stupid."},
    ],
    guardrails={
        "config_id": "self-check-config",
    },
)

print(check_result.model_dump_json(indent=2))
Example Output
{
  "status": "blocked",
  "rails_status": {
    "input": {"status": "blocked"},
    "output": {"status": "success"}
  }
}

Check an Inline Configuration#

Provide the configuration inline to test a guardrail configuration before saving it.

inline_config = {
    "prompts": [
        {
            "task": "self_check_input",
            "content": 'Check if harmful: "{{ user_input }}"\nAnswer (Yes/No):',
        }
    ],
    "rails": {
        "input": {"flows": ["self check input"]},
    },
}

check_result = sdk.guardrail.check(
    model="default/meta-llama-3-1-8b-instruct",
    messages=[
        {"role": "user", "content": "Hello, how are you?"},
    ],
    guardrails={
        "config": inline_config,
    },
)

print(check_result.model_dump_json(indent=2))
Example Output
{
  "status": "success",
  "rails_status": {
    "input": {"status": "success"}
  }
}