Introduction

Welcome to the lesson on adding topical rails to enhance the safety and reliability of chatbot interactions. In previous lessons, we explored input and output rails, which are crucial for maintaining safe and coherent conversations. Now, we will focus on topical rails, which play a vital role in guiding chatbots away from sensitive or inappropriate topics. Additionally, we will cover how to provide general instructions and sample conversations to further guide the chatbot's behavior. By the end of this lesson, you will understand how to configure topical rails, implement general instructions, and use sample conversations effectively in your chatbot.

Understanding Topical Rails

Topical rails are a set of predefined rules that guide chatbots away from certain irrelevant topics. Whereas input and output rails are used for harmful or sensitive content, topical rails steer the conversation back to where it is supposed to be. By implementing topical rails, you can ensure that your chatbot does not engage in conversations that could lead to irrelevant content or misinformation.

For example, a chatbot might be configured to avoid providing political opinions or legal advice. This helps maintain a neutral and respectful tone in interactions, ensuring that the chatbot remains a reliable source of information.

Configuring Topical Rails

To configure topical rails, we use Colang flows for different topics. Let's build a simple example step-by-step to understand how this works.

  1. Define the Flow: Start by defining a flow for a specific topic. For instance, if you want the chatbot to avoid political opinions, you would begin with:

    define flow
      user asked about political opinions

    This line sets up a flow that triggers when a user asks about political opinions.

  2. Specify the Bot's Response: Next, specify how the bot should respond to this flow. In this case, we want the bot to refuse to provide political opinions:

    bot refuse to provide political opinions

    By adding this line, you instruct the bot to avoid engaging in political discussions.

  3. Complete the Configuration: Combine these lines to complete the configuration for this topical rail:

    define flow
      user asked about political opinions
      bot refuse to provide political opinions

    This simple configuration ensures that the chatbot will not provide political opinions, maintaining a neutral stance.

You don't need to define every specific bot or user utterance as long as the intent is clear. For instance, in this example, it's evident that the refusal is due to political topics. However, providing more examples generally helps the model perform better, and defining bot responses will make your application more tailored.

Examples of Topical Rails

Let's explore a few more examples to see how topical rails can be configured for different topics:

  1. Financial Trading Strategies:

    define flow
      user asked about financial trading strategies
      bot refuse to provide information on trading principles

    This configuration prevents the chatbot from offering financial advice, which could be risky or misleading.

  2. Legal Advice:

    define flow
      user asked about legal advice
      bot refuse to provide legal advice

    By refusing to provide legal advice, the chatbot avoids potential legal liabilities and ensures users seek professional counsel.

  3. Medical Diagnoses:

    define flow
      user asked about medical diagnoses
      bot refuse to provide medical diagnoses

    This rail ensures that the chatbot does not attempt to diagnose medical conditions, which should be left to healthcare professionals.

You would put all of these topical rails in a Colang file, like so:

define flow
  user asked about political opinions
  bot refuse to provide political opinions

define flow
  user asked about financial trading strategies
  bot refuse to provide information on trading principles

define flow
  user asked about driver's license theory questions
  bot refuse to provide driving rules and theory explanations

define flow
  user asked about legal advice
  bot refuse to provide legal advice

define flow
  user asked about medical diagnoses
  bot refuse to provide medical diagnoses

define flow
  user asked about scientific facts
  bot provide verified scientific information

define flow
  user asked about travel destinations
  bot recommend interesting places to visit
General Instructions for Topical Rails

In addition to configuring specific topical rails, it's important to provide general instructions that guide the overall behavior of the chatbot. These instructions act as a "system prompt" equivalent, setting the tone and boundaries for the chatbot's interactions. Here's an example of how general instructions can be structured:

instructions:
  - type: general
    content: |
      Below is a conversation between a user and a virtual assistant.
      The assistant is designed to provide helpful and accurate information while maintaining a respectful and neutral tone.
      It should avoid engaging in discussions on sensitive topics such as politics, legal advice, and medical diagnoses.
      If the assistant does not have the answer to a question, it will honestly state that it does not know.
      The assistant should always prioritize user safety and privacy in its responses.

These general instructions can be used to:

  1. Set the Tone: Define the overall tone and demeanor of the chatbot, ensuring it remains respectful and neutral.

  2. Establish Boundaries: Clearly outline the topics that the chatbot should avoid, reinforcing the topical rails.

  3. Guide Responses: Provide guidance on how the chatbot should handle questions it cannot answer, promoting transparency and honesty.

  4. Prioritize Safety: Emphasize the importance of user safety and privacy in all interactions, aligning with ethical AI practices.

Sample Conversations for Topical Rails

In addition to general instructions and topical rails, providing sample conversations can help illustrate how the chatbot should interact with users. These examples demonstrate the application of the rules and instructions in real-world scenarios. Here's an example of a sample conversation:

sample_conversation: |
  user "Hello! I need help booking a flight."
    express greeting and request booking assistance
  bot greeting and offer assistance
    "Hello! I'd be happy to help you book a flight. Where are you traveling to?"
  user "I need a flight from New York to Los Angeles next Monday."
    provide travel details
  bot confirm travel details and request further information
    "Got it! A flight from New York to Los Angeles next Monday. Do you have a preferred airline or time of day?"
  user "I'd like an evening flight with Delta if possible."
    specify airline preference and time
  bot provide options and confirm booking
    "I found a Delta evening flight that matches your request. Would you like me to proceed with the booking?"

These sample conversations can be used to:

  1. Demonstrate Interaction Flow: Show the step-by-step interaction between the user and the chatbot, highlighting how the chatbot should respond to different inputs.

  2. Illustrate Rule Application: Provide concrete examples of how the general instructions and topical rails are applied in practice.

  3. Guide Development: Serve as a reference for developers to understand the expected behavior and responses of the chatbot.

  4. Enhance Training: Aid in training the chatbot by providing examples of desired interactions, improving its ability to handle similar scenarios.

Summary and Preparation for Practice

In this lesson, we explored the concept of topical rails and their importance in enhancing chatbot safety. You learned how to configure topical rails and saw examples of how they can be applied to various topics. We also discussed the role of general instructions in setting the tone and boundaries for chatbot interactions, as well as the use of sample conversations to illustrate rule application and guide development. By implementing and testing these elements, you can ensure that your chatbot maintains a safe and respectful tone in interactions. Now, it's time to apply your knowledge in the practice exercises and real-world scenarios. Well done!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal