forked from microsoft/teams-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenAIModerator.cs
135 lines (116 loc) · 4.69 KB
/
OpenAIModerator.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
using Microsoft.Teams.AI.AI.Planners;
using Microsoft.Teams.AI.Exceptions;
using Microsoft.Extensions.Logging;
using Microsoft.Teams.AI.State;
using Microsoft.Bot.Builder;
using Microsoft.Teams.AI.AI.OpenAI;
namespace Microsoft.Teams.AI.AI.Moderator
{
/// <summary>
/// An moderator that uses OpenAI's moderation API.
/// </summary>
/// <typeparam name="TState">The turn state class.</typeparam>
public class OpenAIModerator<TState> : IModerator<TState> where TState : TurnState
{
private readonly OpenAIModeratorOptions _options;
private readonly OpenAIClient _client;
/// <summary>
/// Constructs an instance of the moderator.
/// </summary>
/// <param name="options">Options to configure the moderator</param>
/// <param name="loggerFactory">The logger factory instance</param>
/// <param name="httpClient">HTTP client.</param>
public OpenAIModerator(OpenAIModeratorOptions options, ILoggerFactory? loggerFactory = null, HttpClient? httpClient = null)
{
_options = options;
OpenAIClientOptions clientOptions = new(_options.ApiKey)
{
Organization = _options.Organization,
};
_client = new OpenAIClient(clientOptions, loggerFactory, httpClient);
}
/// <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 _HandleTextModerationAsync(input, true, cancellationToken);
}
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 _HandleTextModerationAsync(output, false, cancellationToken);
return newPlan ?? plan;
}
}
break;
}
default:
break;
}
return plan;
}
private async Task<Plan?> _HandleTextModerationAsync(string text, bool isModelInput, CancellationToken cancellationToken = default)
{
try
{
ModerationResponse response = await _client.ExecuteTextModerationAsync(text, _options.Model, cancellationToken);
ModerationResult? result = response.Results.Count > 0 ? response.Results[0] : null;
if (result != null)
{
if (result.Flagged)
{
string actionName = isModelInput ? AIConstants.FlaggedInputActionName : AIConstants.FlaggedOutputActionName;
// Flagged
return new Plan()
{
Commands = new List<IPredictedCommand>
{
new PredictedDoCommand(actionName, new Dictionary<string, object?>
{
{ "Result", result }
})
}
};
}
}
return null;
}
catch (HttpOperationException e)
{
// Http error
if (e.StatusCode != null && (int)e.StatusCode == 429)
{
return new Plan()
{
Commands = new List<IPredictedCommand>
{
new PredictedDoCommand(AIConstants.HttpErrorActionName)
}
};
}
throw;
}
}
}
}