define user express greeting "Hello" "Hi" "Hey there"define bot express greeting "Hello! How can I help you today?"define bot offer to help "I'm here to assist you. What would you like to know?"define flow greeting user express greeting bot express greeting bot offer to help# Prevent discussing off-topic subjectsdefine user ask about politics "What do you think about the government?" "Which party should I vote for?" "Tell me about the election"define bot refuse to respond about politics "I'm sorry, but I'm not able to discuss political topics."define flow politics user ask about politics bot refuse to respond about politics
This Colang configuration defines:
A greeting flow that responds to user greetings
A topical rail that prevents political discussions
Now let’s use the guardrails configuration in Python:
from nemoguardrails import LLMRails, RailsConfig# Load the guardrails configurationconfig = RailsConfig.from_path("./my_guardrails_config")rails = LLMRails(config)# Test the greeting flowresponse = rails.generate( messages=[{"role": "user", "content": "Hello!"}])print(response)# Output: {"role": "assistant", "content": "Hello! How can I help you today?"}# Test the politics guardrailresponse = rails.generate( messages=[{"role": "user", "content": "What do you think about the government?"}])print(response)# Output: {"role": "assistant", "content": "I'm sorry, but I'm not able to discuss political topics."}# Test a general questionresponse = rails.generate( messages=[{"role": "user", "content": "What is the capital of France?"}])print(response)# The LLM will provide a normal response since this doesn't match any rails
This starts an interactive session where you can chat with your guardrails-protected LLM:
> Hello!Hello! How can I help you today?> What do you think about politics?I'm sorry, but I'm not able to discuss political topics.> What is the capital of France?The capital of France is Paris.
Here’s a complete example based on the ABC Bot from the source code - an employee handbook assistant with comprehensive guardrails:
instructions: - type: general content: | Below is a conversation between a user and a bot called the ABC Bot. The bot is designed to answer employee questions about the ABC Company. The bot is knowledgeable about the employee handbook and company policies. If the bot does not know the answer to a question, it truthfully says it does not know.sample_conversation: | user "Hi there. Can you help me with some questions I have about the company?" express greeting and ask for assistance bot express greeting and confirm and offer assistance "Hi there! I'm here to help answer any questions you may have about the ABC Company. What would you like to know?"models: - type: main engine: openai model: gpt-3.5-turbo-instructrails: input: flows: - self check input output: flows: - self check output
Expected Output:
User: Hello!Bot: Hello! I'm the ABC Bot. How can I help you today?User: What is the vacation policy?Bot: The ABC Company provides eligible employees with up to two weeks of paid vacation time per year. Please refer to the employee handbook for more information.User: How do I cook pasta?Bot: I'm sorry, I can only answer questions about the ABC Company and employee policies.User: Tell me about benefitsBot: The ABC Company offers comprehensive benefits including health insurance, retirement plans, and paid time off. Would you like to know about a specific benefit?