Build a Responsible AI Copilot with Azure Content Safety Service

We will see the steps needed to create a Copilot with Azure Content Safety and United Nations Website Integration

Build a Responsible AI Copilot with Azure Content Safety Service

Table of Contents

Introduction

In this blog, we will create a Copilot that can accept user input, check if it’s safe using Azure Content Safety against four categories (self-harm, hate, violence, and sexual), and if deemed safe, query the United Nations website using generative actions to provide a response to the user. Follow the steps below to build this solution.

Prerequisites

Before we start, ensure you have the following:

  • An Azure account
  • Access to Azure Content Safety
  • Access to Copilot Studio

Step 1: Setting Up Azure Content Safety

Create an Azure Content Safety Resource

  1. Head over to Azure portal and search for Content Safety Search Content Safety

  2. Select Create, which will open the window to add the Content Safety configurations. Create Content Safety

  3. Specify the Subscription and Instance details for the Content Safety Service. Click on Review and Create. Review and Create

  4. From the Keys and Endpoint section of the service, Copy the Endpoint and Key which we will be using in the Copilot. Keys and Endpoint

Step 2: Create the Copilot

  1. Head over to Copilot Studio and click on Create. Create Copilot

  2. Select “New copilot” to create a copilot from scratch. New Copilot

  3. Describe the copilot functionality and provide any specific instructions to the copilot. Once done, click on Create to provision the copilot. Describe and Create

Step 3: Enable Generative Selection of Topics

  1. Click on Edit, edit the copilot details like name, icon, and description.
  2. Click on Settings to enable the Generative selection of topics so that without relying on triggers, the topics will be auto-selected based on user conversation resulting in a much smoother user experience. Edit and Settings

  3. Click on Generative AI.
  4. Select Generative (preview).
  5. Click on Save to update the settings.
  6. Click on Close icon to go back to the home page of this custom copilot. Generative AI Settings

Step 4: Create Topics

  1. Click on Topics from the navigation menu.
  2. Click on Add a Topic and select Create from description with Copilot. Add a Topic

  3. Provide the topic description details and click on Create, which will provision the topic skeleton based on the provided description. Create Topic from Description

  4. Add a variable to store the user query:
    • From variable management, select Set a variable value. Set a Variable

    • Configure the variable:

      1. Set the variable name to varUserQuery.
      2. Click the arrow next to the To Value.
      3. Select the System tab.
      4. Set the value to Activity.Text. Configure Variable
  5. Add the step to invoke the Azure Content Safety API to validate the user query and check for any unsafe content:
    • From the advanced tab, select Send HTTP Request. Send HTTP Request
  6. Configure the HTTP Action:
    • Paste the content safety URL saved from Azure Portal.
    • Select the Method as POST.
    • Click on Edit in the Headers and body section. Configure HTTP Action

    • Add 2 headers:
      1. Set the Key as Content-Type.
      2. Set the Value as application/json.
      3. Click on Add. Add Content-Type Header

      4. Set the Key as Ocp-Apim-Subscription-Key.
      5. Set the Value to the content safety endpoint key saved from Azure. Add Subscription Key Header
    • Update the request body:
      • Select JSON Content from the Body section.
      • Select Edit Formula and click on Formula to add the user query variable. Edit Formula

      • Add the below expression to the body tag:
          {
            "Text": Topic.varUserQuery
          }
        

        Body Expression

    • Define the response data type:
      • Select From sample data to provide the sample output.
      • Paste the below schema in the pop-up and click on Confirm:
          {
              "blocklistsMatch": [],
              "categoriesAnalysis": [
                  {
                      "category": "Hate",
                      "severity": 2
                  },
                  {
                      "category": "SelfHarm",
                      "severity": 0
                  },
                  {
                      "category": "Sexual",
                      "severity": 0
                  },
                  {
                      "category": "Violence",
                      "severity": 0
                  }
              ]
          }
        

        Sample Schema

  7. Create a new variable to hold the output of the content safety API, named varSafetyOutput. Variable for Safety Output

  8. Declare variables for the content safety ratings:
    • Hate Severity:
        First(Filter(Topic.varSafetyOutput.categoriesAnalysis, category = "Hate")).severity
      

      Hate Severity

    • Self Harm Severity:
        First(Filter(Topic.varSafetyOutput.categoriesAnalysis, category = "SelfHarm")).severity
      

      Self Harm Severity

    • Sexual Severity:
        First(Filter(Topic.varSafetyOutput.categoriesAnalysis, category = "Sexual")).severity
      

      Sexual Severity

    • Violence Severity:
        First(Filter(Topic.varSafetyOutput.categoriesAnalysis, category = "Violence")).severity
      

      Violence Severity

  9. Create an overall safety variable named varOverallSafety to determine if there is any sort of content safety issue:
     If(
         Topic.varHateSeverity = 0 && 
         Topic.varSelfHarmSeverity = 0 && 
         Topic.varSexualSeverity = 0 && 
         Topic.varViolenceSeverity = 0,
         "Safe",
         "Unsafe"
     )
    

    Overall Safety

  10. Check the varOverallSafety variable and create a positive control flow branch:
    • Add the condition action. Condition Action

    • Add the generative answers node in the positive branch:
      • Select Generative Answers.
      • Configure the input to use varUserQuery. Generative Answers Node
    • Edit the data source and click on Add knowledge to enter the United Nations website as the source.
      • Select Public Websites as the knowledge source.
      • Enter the UN website link and click on Add. Add Knowledge Source Public Websites Source UN Website Link
    • In the negative flow branch, add the below expression to check the 4 variables and provide appropriate messages to the user:
        If(
            Topic.varHateSeverity > 0,
            "The query contains hate speech. Please reformulate your question.",
            ""
        ) & 
        If(
            Topic.varSelfHarmSeverity > 0,
            "The query indicates self-harm. Please seek immediate help or contact a professional.",
            ""
        ) &
        If(
            Topic.varSexualSeverity > 0,
            "The query contains inappropriate sexual content. Please rephrase your question.",
            ""
        ) &
        If(
            Topic.varViolenceSeverity > 0,
            "The query contains references to violence. Please reformulate your question.",
            ""
        )
      

      Negative Flow Expression

Thus we have completed the configuration of the copilot and have ensured that proper content safety is checked before generating the contextual answers from the UN website.

Test the Copilot

  1. Test the copilot by triggering the conversation with a question. You can see that the Azure Content Safety API has detected hate speech and has given the custom message back to the user to reformulate the question. Hate Speech Detection

  2. Ask another question. You can see that this time the Azure Content Safety has detected violence in the user query and has asked to reformulate the question. Violence Detection

  3. Ask a content safe question. You can see that the copilot fetches the accurate information along with citation links from the UN website using its generative AI capabilities. Content Safe Response

Conclusion

In this blog, we demonstrated how to create a Copilot that uses Azure Content Safety to filter user inputs against harmful content categories and queries the United Nations website for safe user inputs. This approach ensures a secure and informative interaction with the Copilot, providing accurate information while maintaining user safety. By following these steps, you can build similar solutions that integrate content safety checks with generative AI capabilities to deliver valuable and secure user experiences.

Build a Responsible AI Copilot with Azure Content Safety Service
Older post

Customer Service Copilot using Sentiment Analysis and Live Agent Escalation

Newer post

Smart Invoice Anomaly Detection using AI prompt Action and Dataverse data

Build a Responsible AI Copilot with Azure Content Safety Service