Skip to main content

Overview

In this document, we explain how to set up Guardrails with MLflow Tracing. With this functionality enabled, you can collect additional insights on how your Guard, LLM, and each validator are performing directly in your own Databricks workspace. In this notebook, we’ll be using a local MLflow Tracking Server, but you can just as easily switch over to a hosted Tracking Server. For additional background information on Mlflow Tracing, see the MLflow documentation.

Installing Dependencies

Let’s start by installing the dependencies we’ll use in this exercise. First we’ll install Guardrails with the databricks extra. This will include the mlflow library and any other pip packages we’ll need.
Next, we’ll enable metrics in the Guardrails CLI. The ML-backed validator used below runs locally.
Finally, we’ll install some validators.

Starting the MLflow Tracking Server

Our next step is to start the MLflow Tracking server. This stands up both the telemetry sink we will send traces to, as well as the web interface we can use to examine them. You’ll need to run this next step is a separate terminal since, otherwise, the server’s processes will block execution of the conesecutive cells in this notebook (which is normal).

Creating and Instrumenting our Guard

Next up, we’ll instrument the Guardrails package to send traces to the MLflow Tracking Server as well as setup our LLM and Guard. As of guardrails-ai version 0.5.8, we offer a builtin instrumentor for MLflow.
This instrumentor wraps some of the key functions and flows within Guardrails and automatically captures trace data when the Guard is run. Now that the Guardrails package is instrumented, we can create our Guard.
In this example, we have created a Guard that uses two Validators: RestrictToTopic and ValidLength. The RestrictToTopic Validator ensures that the text is related to the topics we specify, while the ValidLength Guardrail ensures that the text stays within our character limit.

Testing and Tracking our Guard

Next we’ll test our our Guard by calling an LLM and letting the Guard validate the output. After each execution, we’ll look at the trace data collected by MLflow Tracking Server.
First, we’ll give the LLM an easy prompt that should result in an output that passes validation. Consider this our happy path test.
If we navigate to http://localhost:8080 in our browser we can see our experiemnt, My First Experiment, in the list on the left hand side. If we select our experiment, and then select the Traces tab, we should see one trace from the cell we just ran. Happy Path Traces Landing Page Aef0861be758abd4a6901237e6e0c7b7 If we select this trace, we see a breakdown of the various steps taken within the Guard on the left, including a timeline, and a details view for the selected span on the right. If you click on the different spans within the trace, you can see different attributes specific to that span. For example, if you click on guardrails/guard/step/call, the span that tracked the call to the LLM, you can see all of the parameters that were used to call the LLM, as well as all of the outputs from the LLM including token counts. Llm Span C98da4416a544620f65012288bb929fa Next, let’s give the LLM a prompt that instructs it to output something that should fail. Consider this our exception path test.
First note that there is only one failed validator in the logs: RestrictToTopic. This is because since we set on_fail="exception", the first failure to occur will raise an exception and interrupt the process. If we set our OnFail action to a different value, like noop, we would also see a log for ValidLength since the LLM’s output is clearly longer than the max length we specified. If navigate back to the MLflow UI in our browser, we see another trace. Since this last cell raised an exception, we see that the status is listed as Error. Exception Path Landing Page 17016c1a5701dbb147f3b030c095a3bf If we open this new trace we see, just like in the history logs, only RestrictToTopic has a recorded span. This is, again, because it raised an exception on failure exitting the validation loop early. If we click on the validator’s span, and scroll down to the bottom of its details panel, we can see the reason why validation failed: "No valid topic was found." Exception Path Trace Ffbf6a905b0b5681b44765cc147daf0b

Conclusion

With Guardrails, MLflow, and the Guardrails MLflowInstrumentor, we can easily monitor both our LLMs and the validations we’re guarding them with. To learn more, check out Guardrails AI and MLflow.