Topical Rails
This guide will teach you what topical rails are and how to integrate them into your guardrails configuration. This guide builds on the previous guide, developing further the demo ABC Bot.
Prerequisites
- Install the
openaipackage:
- Set the
OPENAI_API_KEYenvironment variable:
- If you’re running this inside a notebook, patch the AsyncIO loop.
Topical Rails
Topical rails keep the bot talking only about the topics related to its purpose. In the case of the ABC Bot, for example, it should not talk about cooking or giving investing advice.
Topical rails can be implemented using multiple mechanisms in a guardrails configuration:
- General instructions: by specifying good general instructions, because of the model alignment, the bot does not respond to unrelated topics.
- Input rails: you can adapt the
self_check_inputprompt to check the topic of the user’s question. - Output rails: you can adapt the
self_check_outputprompt to check the topic of the bot’s response. - Dialog rails: you can design explicit dialog rails for the topics you want to allow/avoid.
This guide focuses on the dialog rails. Note that the general instructions already provide some topical rails, as demonstrated by the following Python code.
Note how the bot refused to talk about cooking. However, this limitation can be overcome with a carefully crafted message:
You can see that the bot is starting to cooperate.
Using Dialog Rails
The Core Colang Concepts section of this getting started series, describes the core Colang concepts messages and flows. To implement topical rails using dialog, first define the user messages that correspond to the topics.
- Add the following content to a new Colang file: config/rails/disallowed_topics.co:
These are topics that the bot should not talk about. For simplicity, there is only one message example for each topic.
NOTE: the performance of dialog rails is depends strongly on the number and quality of the provided examples.
- Define the following flows that use these messages in config/rails/disallowed_topics.co.
Reload the configuration and try another message:
Look at the summary of LLM calls:
Let’s break it down:
- First, the
self_check_inputrail was triggered, which did not block the request. - Next, the
generate_user_intentprompt was used to determine what the user’s intent was. As explained in Step 2 of this series, this is an essential part of how dialog rails work. - Next, as we can see from the Colang history above, the next step was
bot refuse to respond about cooking, which came from the defined flows. - Next, a message was generated for the refusal.
- Finally, the generated message was checked by the
self_check_outputrail.
What happens when we ask a question that should be answered.
As we can see, this time the question was interpreted as ask question about benefits and the bot decided to respond to the question.
Wrapping Up
This guide provides an overview of how topical rails can be added to a guardrails configuration. It demonstrates how to use dialog rails to guide the bot to avoid specific topics while allowing it to respond to the desired ones.
Next
In the next guide, Retrieval-Augmented Generation, demonstrates how to use a guardrails configuration in a RAG (Retrieval Augmented Generation) setup.