UNPKG

@gentrace/openai

Version:

Gentrace OpenAI v4 plugin for Node.JS

220 lines (216 loc) 7.84 kB
'use strict'; var core = require('@gentrace/core'); var OpenAI = require('openai'); var openai = require('../openai.js'); var chat = require('openai/resources/beta/chat/chat'); var __awaiter = (undefined && undefined.__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 __rest = (undefined && undefined.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; class SimpleOpenAI extends openai.OpenAIPipelineHandler { constructor(options) { var _a; const oaiOptions = __rest(options, ["gentraceApiKey", "gentraceBasePath", "gentraceLogger"]); if (options.gentraceBasePath) { try { const url = new URL(options.gentraceBasePath); if (url.pathname.startsWith("/api/v1")) { } else { throw new Error('Gentrace base path must end in "/api/v1".'); } } catch (err) { throw new Error(`Invalid Gentrace base path: ${err.message}`); } } let gentraceConfig = null; if (options.gentraceApiKey) { gentraceConfig = new core.Configuration({ apiKey: (_a = options.gentraceApiKey) !== null && _a !== void 0 ? _a : core.GENTRACE_API_KEY, basePath: options.gentraceBasePath, logger: options.gentraceLogger, }); } else if (!core.globalGentraceConfig) { throw new Error("Gentrace API key not provided. Please provide it in the init() call."); } else { gentraceConfig = core.globalGentraceConfig; } super(Object.assign(Object.assign({}, oaiOptions), { gentraceConfig })); // @ts-ignore this.completions = new SimpleGentraceCompletions(Object.assign(Object.assign({ // @ts-ignore client: this }, options), { gentraceConfig })); // @ts-ignore this.chat = new SimpleGentraceChat(Object.assign(Object.assign({ // @ts-ignore client: this }, options), { gentraceConfig })); // @ts-ignore this.beta = new SimpleGentraceBeta(Object.assign(Object.assign({ // @ts-ignore client: this }, options), { gentraceConfig })); // @ts-ignore this.moderations = new SimpleGentraceModerations(Object.assign(Object.assign({ // @ts-ignore client: this }, options), { gentraceConfig })); this.embeddings = new SimpleGentraceEmbeddings(Object.assign(Object.assign({ // @ts-ignore client: this }, options), { gentraceConfig })); } getConfig() { return this.config; } setPipelineRun(pipelineRun) { this.pipelineRun = pipelineRun; } } class SimpleGentraceEmbeddings extends openai.GentraceEmbeddings { constructor({ client, pipelineRun, gentraceConfig, }) { super({ client, gentraceConfig, pipelineRun, }); } // @ts-ignore create(body, options) { const _super = Object.create(null, { createInner: { get: () => super.createInner } }); return __awaiter(this, void 0, void 0, function* () { return _super.createInner.call(this, body, options); }); } } class SimpleGentraceModerations extends openai.GentraceModerations { constructor({ client, pipelineRun, gentraceConfig, }) { super({ client, gentraceConfig, pipelineRun, }); } // @ts-ignore create(body, options) { const _super = Object.create(null, { createInner: { get: () => super.createInner } }); return __awaiter(this, void 0, void 0, function* () { return _super.createInner.call(this, body, options); }); } } class SimpleGentraceCompletions extends openai.GentraceCompletions { constructor({ client, pipelineRun, gentraceConfig, }) { super({ client, pipelineRun, gentraceConfig, }); } // @ts-ignore create(body, requestOptions) { const _super = Object.create(null, { createInner: { get: () => super.createInner } }); return __awaiter(this, void 0, void 0, function* () { return _super.createInner.call(this, body, requestOptions); }); } } class SimpleGentraceBeta extends OpenAI.Beta { constructor({ client, pipelineRun, gentraceConfig, }) { super(client); // @ts-ignore this.chat = new SimpleGentraceBetaChat({ // @ts-ignore client, pipelineRun, gentraceConfig, }); } } class SimpleGentraceBetaChat extends chat.Chat { constructor({ client, pipelineRun, gentraceConfig, }) { super(client); // @ts-ignore this.completions = new SimpleGentraceBetaChatCompletions({ // @ts-ignore client, pipelineRun, gentraceConfig, }); } } class SimpleGentraceBetaChatCompletions extends openai.GentraceBetaChatCompletions { constructor({ client, pipelineRun, gentraceConfig, }) { super({ client, pipelineRun, gentraceConfig, }); } // @ts-ignore parse(body, options) { const _super = Object.create(null, { parseInner: { get: () => super.parseInner } }); return __awaiter(this, void 0, void 0, function* () { return _super.parseInner.call(this, body, options); }); } } class SimpleGentraceChat extends OpenAI.Chat { constructor({ client, pipelineRun, gentraceConfig, }) { super(client); // @ts-ignore this.completions = new SimpleGentraceChatCompletions({ // @ts-ignore client, pipelineRun, gentraceConfig, }); } } class SimpleGentraceChatCompletions extends openai.GentraceChatCompletions { constructor({ client, pipelineRun, gentraceConfig, }) { super({ client, pipelineRun, gentraceConfig, }); } // @ts-ignore create(body, requestOptions) { const _super = Object.create(null, { createInner: { get: () => super.createInner } }); return __awaiter(this, void 0, void 0, function* () { return _super.createInner.call(this, body, requestOptions); }); } } exports.SimpleGentraceBeta = SimpleGentraceBeta; exports.SimpleGentraceBetaChat = SimpleGentraceBetaChat; exports.SimpleGentraceChat = SimpleGentraceChat; exports.SimpleGentraceCompletions = SimpleGentraceCompletions; exports.SimpleOpenAI = SimpleOpenAI; //# sourceMappingURL=simple.js.map