@microsoft/botbuilder-m365
Version:
M365 extensions for Microsoft BotBuilder, Alpha release.
91 lines • 4.07 kB
JavaScript
;
/**
* @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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenAIClient = void 0;
const axios_1 = __importDefault(require("axios"));
class OpenAIClient {
constructor(options) {
this.DefaultEndpoint = 'https://api.openai.com';
this.UserAgent = 'Microsoft Teams Conversational AI SDK';
this.options = options;
// Cleanup and validate endpoint
if (options.endpoint) {
options.endpoint = options.endpoint.trim();
if (options.endpoint.endsWith('/')) {
options.endpoint = options.endpoint.substring(0, options.endpoint.length - 1);
}
if (!options.endpoint.toLowerCase().startsWith('https://')) {
throw new Error(`OpenAIClient initialized with an invalid endpoint of '${options.endpoint}'. The endpoint must be a valid HTTPS url.`);
}
}
// Validate API key
if (!options.apiKey) {
throw new Error(`OpenAIClient initialized without an 'apiKey'.`);
}
// Create client and set headers
this._httpClient = axios_1.default.create({
validateStatus: (status) => status < 400 || status == 429
});
}
createCompletion(request) {
var _a;
const url = `${(_a = this.options.endpoint) !== null && _a !== void 0 ? _a : this.DefaultEndpoint}/v1/completions`;
return this.post(url, request);
}
createChatCompletion(request) {
var _a;
const url = `${(_a = this.options.endpoint) !== null && _a !== void 0 ? _a : this.DefaultEndpoint}/v1/chat/completions`;
return this.post(url, request);
}
createEmbedding(request) {
var _a;
const url = `${(_a = this.options.endpoint) !== null && _a !== void 0 ? _a : this.DefaultEndpoint}/v1/embeddings`;
return this.post(url, request);
}
createModeration(request) {
var _a;
const url = `${(_a = this.options.endpoint) !== null && _a !== void 0 ? _a : this.DefaultEndpoint}/v1/moderations`;
return this.post(url, request);
}
addRequestHeaders(headers, options) {
headers['Authorization'] = `Bearer ${options.apiKey}`;
if (options.organization) {
headers['OpenAI-Organization'] = options.organization;
}
}
post(url, body) {
return __awaiter(this, void 0, void 0, function* () {
// Initialize request headers
const requestHeaders = {
'Content-Type': 'application/json',
'User-Agent': this.UserAgent
};
this.addRequestHeaders(requestHeaders, this.options);
// Send request
const { status, statusText, data, headers } = yield this._httpClient.post(url, body, {
headers: requestHeaders
});
return { status, statusText, data, headers: headers };
});
}
}
exports.OpenAIClient = OpenAIClient;
//# sourceMappingURL=OpenAIClient.js.map