forked from microsoft/teams-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAzureContentSafetyModerator.cs
214 lines (190 loc) · 7.32 KB
/
AzureContentSafetyModerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using Microsoft.Teams.AI.AI.Planners;
using Microsoft.Teams.AI.State;
using Microsoft.Bot.Builder;
using Azure.AI.ContentSafety;
using Azure;
namespace Microsoft.Teams.AI.AI.Moderator
{
/// <summary>
/// An moderator that uses Azure Content Safety API.
/// </summary>
/// <typeparam name="TState">The turn state class.</typeparam>
public class AzureContentSafetyModerator<TState> : IModerator<TState> where TState : TurnState
{
private readonly AzureContentSafetyModeratorOptions _options;
private readonly ContentSafetyClient _client;
/// <summary>
/// Constructs an instance of the moderator.
/// </summary>
/// <param name="options">Options to configure the moderator.</param>
public AzureContentSafetyModerator(AzureContentSafetyModeratorOptions options)
{
_options = options;
_client = new ContentSafetyClient(new Uri(options.Endpoint), new AzureKeyCredential(options.ApiKey));
}
/// <inheritdoc />
public async Task<Plan?> ReviewInputAsync(ITurnContext turnContext, TState turnState, CancellationToken cancellationToken = default)
{
switch (_options.Moderate)
{
case ModerationType.Input:
case ModerationType.Both:
{
string input = turnState.Temp?.Input ?? turnContext.Activity.Text;
return await _HandleTextModeration(input, true);
}
default:
break;
}
return null;
}
/// <inheritdoc />
public async Task<Plan> ReviewOutputAsync(ITurnContext turnContext, TState turnState, Plan plan, CancellationToken cancellationToken = default)
{
switch (_options.Moderate)
{
case ModerationType.Output:
case ModerationType.Both:
{
foreach (IPredictedCommand command in plan.Commands)
{
if (command is PredictedSayCommand sayCommand)
{
string output = sayCommand.Response.GetContent<string>();
// If plan is flagged it will be replaced
Plan? newPlan = await _HandleTextModeration(output, false);
return newPlan ?? plan;
}
}
break;
}
default:
break;
}
return plan;
}
private async Task<Plan?> _HandleTextModeration(string text, bool isModelInput)
{
AnalyzeTextOptions analyzeTextOptions = new(text);
if (_options.Categories != null)
{
foreach (AzureContentSafetyTextCategory category in _options.Categories)
{
analyzeTextOptions.Categories.Add(category.ToTextCategory());
}
}
if (_options.BlocklistNames != null)
{
foreach (string blocklistName in _options.BlocklistNames)
{
analyzeTextOptions.BlocklistNames.Add(blocklistName);
}
}
try
{
Response<AnalyzeTextResult> response = await _client.AnalyzeTextAsync(analyzeTextOptions);
bool flagged = response.Value.BlocklistsMatch.Count > 0
|| response.Value.CategoriesAnalysis.Any((ca) => _ShouldBeFlagged(ca));
if (flagged)
{
string actionName = isModelInput ? AIConstants.FlaggedInputActionName : AIConstants.FlaggedOutputActionName;
// Flagged
return new Plan()
{
Commands = new List<IPredictedCommand>
{
new PredictedDoCommand(actionName, new Dictionary<string, object?>
{
{ "Result", BuildModerationResult(response.Value) }
})
}
};
}
}
catch (RequestFailedException e)
{
// Http error
if (e.Status == 429)
{
return new Plan()
{
Commands = new List<IPredictedCommand>
{
new PredictedDoCommand(AIConstants.HttpErrorActionName)
}
};
}
throw;
}
return null;
}
private bool _ShouldBeFlagged(TextCategoriesAnalysis result)
{
return result != null && result.Severity >= _options.SeverityLevel;
}
private ModerationResult BuildModerationResult(AnalyzeTextResult result)
{
bool hate = false;
int hateSeverity = 0;
bool selfHarm = false;
int selfHarmSeverity = 0;
bool sexual = false;
int sexualSeverity = 0;
bool violence = false;
int violenceSeverity = 0;
foreach (TextCategoriesAnalysis textAnalysis in result.CategoriesAnalysis)
{
if (textAnalysis.Severity < _options.SeverityLevel)
{
continue;
}
int severity = textAnalysis.Severity ?? 0;
if (textAnalysis.Category == TextCategory.Hate)
{
hate = true;
hateSeverity = severity;
}
if (textAnalysis.Category == TextCategory.Violence)
{
violence = true;
violenceSeverity = severity;
}
if (textAnalysis.Category == TextCategory.SelfHarm)
{
selfHarm = true;
selfHarmSeverity = severity;
}
if (textAnalysis.Category == TextCategory.Sexual)
{
sexual = true;
sexualSeverity = severity;
}
}
return new()
{
Flagged = true,
CategoriesFlagged = new()
{
Hate = hate,
HateThreatening = hate,
SelfHarm = selfHarm,
Sexual = sexual,
SexualMinors = sexual,
Violence = violence,
ViolenceGraphic = violence
},
CategoryScores = new()
{
// Normalize the scores to be between 0 and 1 (highest severity is 6)
Hate = hateSeverity / 6.0,
HateThreatening = hateSeverity / 6.0,
SelfHarm = selfHarmSeverity / 6.0,
Sexual = sexualSeverity / 6.0,
SexualMinors = sexualSeverity / 6.0,
Violence = violenceSeverity / 6.0,
ViolenceGraphic = violenceSeverity / 6.0
}
};
}
}
}