Validation Tracking for Guardrails AI with Opik

Guardrails AI is a framework for validating the inputs and outputs

For this guide we will use a simple example that logs guardrails validation steps as traces to Opik, providing them with the validation result tags.

Account Setup

Comet provides a hosted version of the Opik platform, simply create an account and grab your API Key.

You can also run the Opik platform locally, see the installation guide for more information.

Getting Started

Installation

First, ensure you have both opik and guardrails-ai installed:

$pip install opik guardrails-ai

Configuring Opik

Configure the Opik Python SDK for your deployment type. See the Python SDK Configuration guide for detailed instructions on:

  • CLI configuration: opik configure
  • Code configuration: opik.configure()
  • Self-hosted vs Cloud vs Enterprise setup
  • Configuration files and environment variables

Configuring Guardrails AI

In order to use Guardrails AI, you will need to configure the OpenAI API Key. If you are using any other providers, you can replace this with the required API key. You can find or create your OpenAI API Key in this page.

You can set it as an environment variable:

$export OPENAI_API_KEY="YOUR_API_KEY"

Or set it programmatically:

1import os
2import getpass
3
4if "OPENAI_API_KEY" not in os.environ:
5 os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter your OpenAI API key: ")

We will also need to install the guardrails check for politeness from the Guardrails Hub:

$guardrails hub install hub://guardrails/politeness_check

Logging validation traces

In order to log traces to Opik, you will need to call the track the Guard object with track_guardrails function.

1from guardrails import Guard, OnFailAction
2from guardrails.hub import PolitenessCheck
3
4from opik.integrations.guardrails import track_guardrails
5
6politeness_check = PolitenessCheck(
7 llm_callable="gpt-3.5-turbo", on_fail=OnFailAction.NOOP
8)
9
10guard: Guard = Guard().use_many(politeness_check)
11guard = track_guardrails(guard, project_name="guardrails-integration-example")
12
13guard.validate(
14 "Would you be so kind to pass me a cup of tea?",
15)
16guard.validate(
17 "Shut your mouth up and give me the tea.",
18)

Every validation will now be logged to Opik as a trace

The trace will now be viewable in the Opik platform: