Search code examples
botframework

Content Moderator in Virtual assistant template


I installed the newly launched virtual assistant template (C#) of the bot framework, downloaded it from here https://github.com/microsoft/botframework-solutions/tree/master/templates/Virtual-Assistant-Template/csharp and deployed it using deployment scripts and the default setup works well.

According to the documentation at https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-virtual-assistant-template?view=azure-bot-service-4.0#content-moderator, content moderator is optional but enabled. It says "A middleware component is provided that screen texts and surfaces through a TextModeratorResult on the TurnState object." I see cognitive service for Content-moderator has been created and the secret key appears in the appsettings but startup does not instantate the object neither does the botservice.

But, I do not see any evidence of it in the code. Searching keys such as "TextModeratorResult", "TurnState", "contentModerator" does not return anything.

Is this something we need to instantiate? Are there any guidelines?


Solution

  • You should only have to add a single line to get this working. However, it looks like this hasn't been tested in awhile because there's a few steps to get this working. I'll submit a PR to fix thisPR, but for now:

    1. Ensure you have "contentModerator": "key" set in appsettings.json. This should have happened automatically during deployment.
    2. Remove the Microsoft.Azure.CognitiveServices.ContentModerator NuGet package from your project. It's been added to Microsoft.Bot.Builder.Solutions and having it in both caused a "Could Not Load Type..." error for me.
    3. In <yourProject>/Adapters/DefaultAdapter.cs (or DefaultWebsocketAdapter.cs if you're using Websockets), add (near the rest of the middleware pipeline):
    Use(new ContentModeratorMiddleware(settings.ContentModerator.Key, "https://<yourCMRegion>"));
    

    Note: Your endpoint region MUST be formatted like I have it, with the https:// or it will not work. I'll rollI rolled a fix for this into my PR

    1. Now, you can access the moderated data in `TurnContext.TurnState.TextModeratorResult.

    enter image description here