@microsoft/botbuilder-m365
Version:
M365 extensions for Microsoft BotBuilder, Alpha release.
129 lines • 5.67 kB
JavaScript
;
/* eslint-disable security/detect-object-injection */
/**
* @module botbuilder-m365
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenAIModerator = void 0;
const OpenAIClients_1 = require("./OpenAIClients");
const AI_1 = require("./AI");
class OpenAIModerator {
constructor(options) {
this._options = Object.assign({}, options);
this._client = this.createClient(this._options);
}
reviewPrompt(context, state, prompt, options) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
switch (this._options.moderate) {
case 'input':
case 'both': {
const input = (_b = (_a = state === null || state === void 0 ? void 0 : state.temp) === null || _a === void 0 ? void 0 : _a.value.input) !== null && _b !== void 0 ? _b : context.activity.text;
const result = yield this.createModeration(input, this._options.model);
if (result) {
if (result.flagged) {
// Input flagged
return {
type: 'plan',
commands: [
{
type: 'DO',
action: AI_1.AI.FlaggedInputActionName,
entities: result
}
]
};
}
}
else {
// Rate limited
return {
type: 'plan',
commands: [{ type: 'DO', action: AI_1.AI.RateLimitedActionName, entities: {} }]
};
}
break;
}
}
return undefined;
});
}
reviewPlan(context, state, plan) {
return __awaiter(this, void 0, void 0, function* () {
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.response;
const result = yield this.createModeration(output, this._options.model);
if (result) {
if (result.flagged) {
// Output flagged
return {
type: 'plan',
commands: [
{
type: 'DO',
action: AI_1.AI.FlaggedOutputActionName,
entities: result
}
]
};
}
}
else {
// Rate limited
return {
type: 'plan',
commands: [
{ type: 'DO', action: AI_1.AI.RateLimitedActionName, entities: {} }
]
};
}
}
}
break;
}
return plan;
});
}
get options() {
return this._options;
}
createClient(options) {
return new OpenAIClients_1.OpenAIClient({
apiKey: options.apiKey,
organization: options.organization,
endpoint: options.endpoint
});
}
createModeration(input, model) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const response = yield this._client.createModeration({ input, model });
if (((_a = response.data) === null || _a === void 0 ? void 0 : _a.results) && Array.isArray(response.data.results) && response.data.results.length > 0) {
return response.data.results[0];
}
else {
return undefined;
}
});
}
}
exports.OpenAIModerator = OpenAIModerator;
//# sourceMappingURL=OpenAIModerator.js.map