forked from microsoft/teams-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenAIModerator.ts
186 lines (172 loc) · 6.49 KB
/
OpenAIModerator.ts
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
/**
* @module teams-ai
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { Plan, PredictedDoCommand, PredictedSayCommand } from '../planners';
import { TurnState } from '../TurnState';
import { TurnContext } from 'botbuilder';
import {
OpenAIClient,
CreateModerationResponseResultsInner,
CreateModerationResponse,
OpenAIClientResponse
} from '../internals';
import { Moderator } from './Moderator';
import { AI } from '../AI';
/**
* Options for the OpenAI based moderator.
*/
export interface OpenAIModeratorOptions {
/**
* OpenAI API key
*/
apiKey: string;
/**
* Which parts of the conversation to moderate.
*/
moderate: 'input' | 'output' | 'both';
/**
* Optional. OpenAI organization.
*/
organization?: string;
/**
* Optional. OpenAI endpoint.
*/
endpoint?: string;
/**
* Optional. OpenAI model to use.
*/
model?: string;
/**
* Optional. Azure Content Safety API version.
*/
apiVersion?: string;
}
/**
* A moderator that uses OpenAI's moderation API to review prompts and plans for safety.
* @remarks
* This moderation can be configure to review the input from the user, output from the model, or both.
* @template TState Optional. Type of the applications turn state.
*/
export class OpenAIModerator<TState extends TurnState = TurnState> implements Moderator<TState> {
private readonly _options: OpenAIModeratorOptions;
private readonly _client: OpenAIClient;
/**
* Creates a new instance of the OpenAI based moderator.
* @param {OpenAIModeratorOptions} options Configuration options for the moderator.
*/
public constructor(options: OpenAIModeratorOptions) {
this._options = Object.assign({}, options);
this._client = this.createClient(this._options);
}
/**
* Reviews an incoming utterance for safety violations.
* @param {TurnContext} context - Context for the current turn of conversation.
* @param {TState} state - Application state for the current turn of conversation.
* @returns {Promise<Plan | undefined>} An undefined value to approve the prompt or a new plan to redirect to if not approved.
*/
public async reviewInput(context: TurnContext, state: TState): Promise<Plan | undefined> {
switch (this._options.moderate) {
case 'input':
case 'both': {
const input = state.temp.input ?? context.activity.text;
const result = await this.createModeration(input, this._options.model);
if (result) {
if (result.flagged) {
// Input flagged
return {
type: 'plan',
commands: [
{
type: 'DO',
action: AI.FlaggedInputActionName,
parameters: result
} as PredictedDoCommand
]
};
}
} else {
// Rate limited
return {
type: 'plan',
commands: [{ type: 'DO', action: AI.HttpErrorActionName, parameters: {} } as PredictedDoCommand]
};
}
break;
}
}
return undefined;
}
/**
* Reviews the SAY commands generated by the planner for safety violations.
* @param {TurnContext} context - Context for the current turn of conversation.
* @param {TState} state - Application state for the current turn of conversation.
* @param {Plan} plan - Plan generated by the planner.
* @returns {Promise<Plan>} The plan to execute. Either the current plan passed in for review or a new plan.
*/
public async reviewOutput(context: TurnContext, state: TState, plan: Plan): Promise<Plan> {
switch (this._options.moderate) {
case 'output':
case 'both':
for (let i = 0; i < plan.commands.length; i++) {
const cmd = plan.commands[i];
if (cmd.type == 'SAY') {
const output = (cmd as PredictedSayCommand).response;
const result = await this.createModeration(output.content || '', this._options.model);
if (result) {
if (result.flagged) {
// Output flagged
return {
type: 'plan',
commands: [
{
type: 'DO',
action: AI.FlaggedOutputActionName,
parameters: result
} as PredictedDoCommand
]
};
}
} else {
// Rate limited
return {
type: 'plan',
commands: [
{ type: 'DO', action: AI.HttpErrorActionName, parameters: {} } as PredictedDoCommand
]
};
}
}
}
break;
}
return plan;
}
public get options(): OpenAIModeratorOptions {
return this._options;
}
protected createClient(options: OpenAIModeratorOptions): OpenAIClient {
return new OpenAIClient({
apiKey: options.apiKey,
organization: options.organization,
endpoint: options.endpoint
});
}
protected async createModeration(
input: string,
model?: string
): Promise<CreateModerationResponseResultsInner | undefined> {
const response = (await this._client.createModeration({
input,
model
})) as OpenAIClientResponse<CreateModerationResponse>;
if (response.data?.results && Array.isArray(response.data.results) && response.data.results.length > 0) {
return response.data.results[0];
} else {
return undefined;
}
}
}