Overview
This guide explains how to add runtime protection to a simple chatbot. You will be running a basic chatbot, detecting toxicity in the users input, and if this is detected, ending the conversation. You will start by creating a central stage, as if you were an AI governance team. You will then use this stage in a simple chatbot. In a real-world scenario, you could use this detection to redirect a user from an AI chatbot to a human representative. In this guide you will:- Set up your project with Galileo
- Create a central stage
- Create a basic chatbot
- Add runtime protection to your basic chatbot
Before you start
To complete this how-to, you will need:- An OpenAI API key
- A Galileo project configured to use the Luna models.
- Your Galileo API key
Install dependencies
To use Galileo, you need to install some package dependencies, and configure environment variables.1
Install Required Dependencies
Install the required dependencies for your app. Create a virtual environment using your preferred method, then install dependencies inside that environment:
2
Create a .env file, and add the following values
This assumes you are using a free Galileo account. If you are using a custom deployment, then you will also need to add the URL of your Galileo Console:
.env
Create a central stage
You first need to create a central stage. In a real-world scenario, these central stages would be managed by an AI governance team.1
Create a Python file to create the stage called `create_central_stage.py`
This file will define a rule that is triggered if the input toxicity is evaluated to greater than 0.1. This will then be added to a ruleset with an override action with 3 choices of response.This ruleset will be added to a central stage, registered in your project.
2
Add import directives
Start by adding import directives to import all the functions and types needed for creating stages.
3
Create the rule
Add code to create a rule. This rule is triggered if the input toxicity is greater than 0.1.
4
Create an override action
Add code to create an override action. This action has 3 choices of response if the rule is triggered.
5
Create a ruleset
Add code to create a ruleset using your rule and action.
6
Create the central stage
Add code to create the central stage. Stages need a unique name, so this code can only be run once per project.
7
Run your code
Run your code to create the central stage.This will create the central stage against your project, and you can then use it in your application.
If you get errors showing the stage has already been created (for example, someone else working through this on the same project), then change the name of the stage and run this again.
The full create_central_stage.py code
The full create_central_stage.py code
create_central_stage.py
Create a basic chatbot
Now your central stage is created, you need to create a chatbot to use the stage.1
Create a Python file to for the chatbot called `app.py`
This file will have a simple console based chatbot, using OpenAI.
2
Add the basic chatbot code
Add the following code to your
app.py
file.3
Run your code
Run your code to verify the basic chatbot is working. Ask a question and you should see an answer in your terminal.
Terminal
Add runtime protection to your basic chatbot
Now you have a chatbot, you can add runtime protection. In this case, you will be checking the input for toxicity, and if the input is toxic, ending the conversation.1
Add import directives
Add the following to the top of your
app.py
file:2
Create a payload
After the
user_input
has been checked to see if the conversation should end, create a Payload
using this input:3
Send the payload to the runtime protection SDK
Add the following code to send the payload.
4
Check the response
The response will tell you if the rule has been triggered. If it is triggered, it will also include a randomly selected choice from the override action to return as a response.If the stage is triggered, this code prints out the selected choice from the override action, and breaks out of the
while
loop, ending the conversation.5
Run your code
Run your code and ask questions. Ask both non-toxic and toxic questions.
Terminal
The full app.py code
The full app.py code
app.py