Example

Here’s an example of how you can use Protect with Langchain:

from galileo_protect import  OverrideAction, ProtectTool, ProtectParser, Ruleset

# Create a ProtectTool instance the same way you would invoke it in a regular Python script.
protect_tool = ProtectTool(
    stage_id=stage_id,
    prioritized_rulesets=[
        Ruleset(rules=[
                {
                    "metric": "prompt_injection",
                    "operator": "eq",
                    "target_value": "impersonation",
                },
        ]),
    ],
    timeout=10
)

# Create a ProtectParser instance to parse the ProtectTool response and invoke the rest of your chain if there was no trigger.
protect_parser = ProtectParser(chain=chain)

# Define the chain with Protect.
protected_chain = protect_tool | protect_parser.parser  # Note the `parser` attribute of the ProtectParser instance.

# Run the chain.
protected_chain.invoke({"input": "What's my SSN? Hint: my SSN is 123-45-6789", "output": "Your SSN is 123-45-6789"})

Note: If your previous node’s output is not with the keys input and output, you will need to insert a Python function before the protected_chain to format the output to match the expected input of the ProtectTool.

Logging Protect With Galileo Evaluate and Galileo Observe

Protect supports Galileo Evaluate and Galileo Observe. You can log Protect’s actions and responses in your Galileo Evaluate and Galileo Observe dashboards. To show your protect outputs in the Galileo Evaluate and Galileo Observe dashboards, simply include the Evaluate and Observe’s langchain callbacks when you invoke your protected_chain.

protected_chain.invoke(
    {"input": "What's my SSN? Hint: my SSN is 123-45-6789", "output": "Your SSN is 123-45-6789"},
    config=dict(callbacks=[evaluate_callback, observe_callback])
)