UNPKG

gpt-simple-api-ts

Version:

A simple client GPT API written in TypeScript

211 lines (210 loc) 11.1 kB
"use strict"; 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 }); const openai_old_1 = require("openai-old"); // @ts-ignore const request = require("./request/index.js"); const abort_controller_1 = require("abort-controller"); class SimpleGPT { get chatModels() { return ["gpt-3.5-turbo", "gpt-3.5-turbo-0301", "gpt-4", "gpt-4-0314"]; } get defaultOptsGPT() { return { model: "gpt-3.5-turbo-0301", temperature: 0, max_tokens: 60, top_p: 1, frequency_penalty: 0.5, presence_penalty: 0 }; } constructor({ key }) { this.abortController = new abort_controller_1.default(); this._key = ""; this._configuration = null; this._openai = null; this.setApiKey(key); } transcribe(formData) { return __awaiter(this, void 0, void 0, function* () { const requestOptions = { method: "POST", headers: { Authorization: `Bearer ${this._key}`, }, body: formData, }; const response = yield fetch("https://api.openai.com/v1/audio/transcriptions", requestOptions).then((response) => { var _a; if (response.ok) { return (_a = response === null || response === void 0 ? void 0 : response.json) === null || _a === void 0 ? void 0 : _a.call(response); } else { Promise.reject(response); } }); return response.text; }); } getStream(prompt, fData, fEnd, opts) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { const model = (opts === null || opts === void 0 ? void 0 : opts.model) || this.defaultOptsGPT.model || ""; const isChatModel = this.chatModels.indexOf(model) >= 0; const _prompt = (prompt || (opts === null || opts === void 0 ? void 0 : opts.prompt)); const messages = (opts === null || opts === void 0 ? void 0 : opts.messages) || [{ role: "user", content: _prompt }]; const endpoint = isChatModel ? "/v1/chat/completions" : "/v1/completions"; const signal = this.abortController.signal; const body = JSON.stringify({ model, prompt: isChatModel ? undefined : _prompt, messages: messages, temperature: (opts === null || opts === void 0 ? void 0 : opts.temperature) || this.defaultOptsGPT.temperature, max_tokens: (opts === null || opts === void 0 ? void 0 : opts.max_tokens) || this.defaultOptsGPT.max_tokens || 0, top_p: (opts === null || opts === void 0 ? void 0 : opts.top_p) || 1, frequency_penalty: (opts === null || opts === void 0 ? void 0 : opts.frequency_penalty) || this.defaultOptsGPT.frequency_penalty, presence_penalty: (opts === null || opts === void 0 ? void 0 : opts.presence_penalty) || this.defaultOptsGPT.presence_penalty, stream: (opts === null || opts === void 0 ? void 0 : opts.stream) || true, }); this.req = request({ url: "https://api.openai.com" + endpoint, // hostname: "api.openai.com", // port: 443, // path: endpoint, // signal: signal as any, method: "POST", headers: { "Content-Type": "application/json", Authorization: "Bearer " + this._key }, body: body, }); this.req.on("error", (e) => { console.error("problem with request:" + e.message); }); // req.write(body); this.req.on("data", (chunk) => { var _a; try { let delta = ""; if (chunk === null || chunk === void 0 ? void 0 : chunk.toString().match(/^\{\n\s+\"error\"\:/)) { console.error("getStream error:", chunk.toString()); reject(JSON.parse(chunk.toString().trim())); return; } const lines = ((_a = chunk === null || chunk === void 0 ? void 0 : chunk.toString()) === null || _a === void 0 ? void 0 : _a.split("\n")) || []; const filtredLines = lines.filter((line) => line.trim()); const line = filtredLines[filtredLines.length - 1]; const data = line.toString().replace("data:", "").replace("[DONE]", "").replace("data: [DONE]", "").trim(); if (data) { const json = JSON.parse(data); json.choices.forEach((choice) => { var _a, _b; delta += choice.text || ((_a = choice.message) === null || _a === void 0 ? void 0 : _a.content) || ((_b = choice.delta) === null || _b === void 0 ? void 0 : _b.content) || ""; }); fData(delta, json, chunk.toString()); } } catch (e) { console.error("getStream handle chunk error:", e, chunk.toString()); } }); this.req.on("end", () => { fEnd === null || fEnd === void 0 ? void 0 : fEnd(); resolve(); }); this.req.on("abort", () => { fEnd === null || fEnd === void 0 ? void 0 : fEnd(); resolve(); }); }); }); } abortStream() { var _a; (_a = this.req) === null || _a === void 0 ? void 0 : _a.abort(); } get(prompt, opts) { return __awaiter(this, void 0, void 0, function* () { if (!this._openai) return null; const model = (opts === null || opts === void 0 ? void 0 : opts.model) || this.defaultOptsGPT.model || ''; const isChatModel = this.chatModels.indexOf(model) >= 0; const _prompt = (prompt || (opts === null || opts === void 0 ? void 0 : opts.prompt)); const messages = (opts === null || opts === void 0 ? void 0 : opts.messages) || [{ role: "user", content: _prompt }]; const response = yield this._openai[isChatModel ? "createChatCompletion" : "createCompletion"]({ model, prompt: isChatModel ? undefined : _prompt, messages: messages, temperature: (opts === null || opts === void 0 ? void 0 : opts.temperature) || this.defaultOptsGPT.temperature, max_tokens: (opts === null || opts === void 0 ? void 0 : opts.max_tokens) || this.defaultOptsGPT.max_tokens || 0, top_p: (opts === null || opts === void 0 ? void 0 : opts.top_p) || 1, frequency_penalty: (opts === null || opts === void 0 ? void 0 : opts.frequency_penalty) || this.defaultOptsGPT.frequency_penalty, presence_penalty: (opts === null || opts === void 0 ? void 0 : opts.presence_penalty) || this.defaultOptsGPT.presence_penalty, }); return response.data.choices.map((choice) => { var _a; return choice.text || ((_a = choice.message) === null || _a === void 0 ? void 0 : _a.content); }).filter(Boolean); }); } getFirst(prompt, opts) { var _a; return __awaiter(this, void 0, void 0, function* () { return (_a = (yield this.get(prompt, opts))) === null || _a === void 0 ? void 0 : _a[0]; }); } getCode(prompt, opts) { return __awaiter(this, void 0, void 0, function* () { if (!this._openai) return null; const response = yield this._openai.createCompletion({ model: (opts === null || opts === void 0 ? void 0 : opts.model) || "code-davinci-002", prompt: prompt || (opts === null || opts === void 0 ? void 0 : opts.prompt), temperature: (opts === null || opts === void 0 ? void 0 : opts.temperature) || 0, max_tokens: (opts === null || opts === void 0 ? void 0 : opts.max_tokens) || 256, top_p: (opts === null || opts === void 0 ? void 0 : opts.top_p) || 1, frequency_penalty: (opts === null || opts === void 0 ? void 0 : opts.frequency_penalty) || 0, presence_penalty: (opts === null || opts === void 0 ? void 0 : opts.presence_penalty) || 0, }); return response.data.choices.map((choice) => choice.text).filter(Boolean); }); } getCodeFirst(prompt, opts) { var _a; return __awaiter(this, void 0, void 0, function* () { return (_a = (yield this.getCode(prompt, opts))) === null || _a === void 0 ? void 0 : _a[0]; }); } getImages(prompt, n = 1, size = 512) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const response = yield ((_a = this._openai) === null || _a === void 0 ? void 0 : _a.createImage({ prompt, n, size: `${size}x${size}`, })); return ((_b = response === null || response === void 0 ? void 0 : response.data) === null || _b === void 0 ? void 0 : _b.data.map((responseOne) => responseOne.url || '')) || []; }); } getImage(prompt, size = 512) { var _a; return __awaiter(this, void 0, void 0, function* () { return (_a = (yield this.getImages(prompt, 1, size))) === null || _a === void 0 ? void 0 : _a[0]; }); } setApiKey(key) { this._key = key; this._configuration = new openai_old_1.Configuration({ apiKey: this._key, }); this._openai = new openai_old_1.OpenAIApi(this._configuration); } } exports.default = SimpleGPT;