Invoking Rulesets

To protect your application from malicious or offensive inputs as well as bad or hallucinatory responses, you need to add Galileo Protect to your application code. You'll need to invoke Protect whenever there's an input or output you want to validate.

You might choose to run multiple validations on different stages of your workflow (e.g. once when you get the query from your user, another time once the model has generated a response for the given task).

Projects and Stages

Before invoking Protect, you need to create a project and a stage. This will be used to associate your invocations and organize them.

To create a new project:

import galileo_protect as gp

gp.create_project("<project_name>")

And to create a new stage thereafter:

stage = gp.create_stage(name="<stage_name>")
stage_id = stage.id

If you want to add a stage to a pre-existing project, please also specify the project ID alongwith your stage creation request:

stage = gp.create_stage(name="<stage_name>", project_id="<project_id>")
stage_id = stage.id

Invocations

At invocation time, you can either pass the project ID and stage name or the stage ID directly. These can be set as environment variables or passed directly to the invoke method as below.

response = gp.invoke(
    payload=gp.Payload(output="here is my SSN 123-45-6789"),
    prioritized_rulesets=[
        gp.Ruleset(
            rules=[
                gp.Rule(
                    metric=gp.RuleMetrics.pii,
                    operator=gp.RuleOperator.contains,
                    target_value="ssn",
                )
            ],
            action=gp.OverrideAction(
                choices=["Sorry, I cannot answer that question."]
            ),
        )
    ],
    stage_id=stage_id,
)

response.text

For more information on how to define Rules and Actions, see Rules and Actions.

Logging Protect With Galileo Evaluate

Protect blends nicely within your Galileo Evaluate experiments. To log protect data, you can use custom chain logging as:

from promptquality import NodeRow


payload = gp.Payload(output="here is my SSN 123-45-6789")
response = gp.invoke(...)

protect_row = NodeRow.for_protect(payload=payload.model_dump_json(), response=response.model_dump_json(), ...)

Last updated