Use a Runnable as an Action in Guardrails
This guide will teach you how to use a Runnable as an action inside a guardrails configuration.
Prerequisites
Set up an OpenAI API key, if not already set.
Install the LangChain x OpenAI integration package.
If you’re running this inside a notebook, you also need to patch the AsyncIO loop.
Sample Runnable
Let’s create a sample Runnable that checks if a string provided as input contains certain keyword.
Guardrails Configuration
Now, let’s create a guardrails configuration that uses the CheckKeywords runnable as part of an input rail flow. To achieve this, you need to register an instance of CheckKeywords as an action. In the snippets below, we register it as the check_keywords action. We can then use this action inside the check proprietary keywords flow, which is used as an input rail.
Testing
Let’s give this a try. If we invoke the guardrails configuration with a message that contains the “proprietary” keyword, the returned response is “I’m sorry, I can’t respond to that”.
On the other hand, a message which does not hit the input rail, will proceed as usual.
Conclusion
In this guide, you learned how to register a custom Runnable as an action and use it inside a guardrails configuration. This guide uses a basic implementation of a Runnable. However, you can register any type of Runnable, including ones that make calls to the LLM, 3rd party APIs or vector stores.