Prerequisites: Setup optimodel here

Now you’re ready to start adding guardrails to your calls. Here is the general syntax:

from optimodel import queryModel

response = await queryModel(
    ....
    messages=[
        ModelMessage(
            role="system",
            content="You are a helpful assistant. Always respond in JSON syntax",
        ),
        ModelMessage(role="user", content=prompt),
    ],
    guards=[...] # Optional guards param
)

Blocking Requests

In addition to the guard itself, you can also use the blockRequest flag to block requests when a guard is active and give a custom message to return instead.

guards=[MicrosoftPresidioConfig(
    guardName="MICROSOFT_PRESIDIO_GUARD",
    guardType="preQuery",
    entitiesToCheck=["EMAIL_ADDRESS"],
    blockRequest=True, # Pass this to block the request
    blockRequestMessage="You are not allowed to ask about this email address" # Pass this to give a custom message
)]

Available Guardrails

meta-llama/Prompt-Guard-86M

Utilize Meta’s prompt guard to protect against jailbreaks and injection attacks. See the model card here for more information.

Note: We recommend starting with only jailbreak with a value of 0.999 unless you know what you are doing

guards=[LLamaPromptGuardConfig(
    guardName="LLamaPromptGuard",
    jailbreakThreshold=0.9999,
    guardType="preQuery", # You'll likely only want to guard the input here
)]

microsoft/Presidio-Guard

Utilize Microsoft’s Presidio Guard to protect against PII. See the model card here for more information.

guards=[MicrosoftPresidioConfig(
    guardName="MICROSOFT_PRESIDIO_GUARD",
    guardType="preQuery",
    entitiesToCheck=["EMAIL_ADDRESS"], # See the model card for the full list of entities to check
)]

lytix/Regex-Guard

Simple regex guard to protect against given regex patterns. See here for source code on how its implemented.

guards=[LytixRegexConfig(
    guardName="LYTIX_REGEX_GUARD",
    regex="secrets",
    guardType="preQuery",
)]