UNPKG

nicechat

Version:

An extensible AI chat framework for OpenAi's models

126 lines (125 loc) 5.86 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()); }); }; var __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.chatOpenai = void 0; const chalk_1 = __importDefault(require("chalk")); const openai_1 = __importDefault(require("openai")); const nicechat_1 = require("./nicechat"); function chatOpenai(apiKey, model, systemMsg, isDebug, plugins) { var _a, e_1, _b, _c; var _d, _e, _f, _g; return __awaiter(this, void 0, void 0, function* () { const openai = new openai_1.default({ apiKey }); const functions = [...plugins.values()].map((x) => x.meta); console.log("[" + chalk_1.default.blueBright(systemMsg) + "]"); const messages = [ system(systemMsg), ]; // initial use input const input = yield (0, nicechat_1.readLine)(); messages.push(user(input)); while (true) { // starting stream const stream = yield openai.chat.completions.create({ model, messages, functions, stream: true, }); let msg = ""; let fcall = { name: "", arguments: "", }; try { for (var _h = true, stream_1 = (e_1 = void 0, __asyncValues(stream)), stream_1_1; stream_1_1 = yield stream_1.next(), _a = stream_1_1.done, !_a; _h = true) { _c = stream_1_1.value; _h = false; const part = _c; // collect regular message const p = ((_e = (_d = part.choices[0]) === null || _d === void 0 ? void 0 : _d.delta) === null || _e === void 0 ? void 0 : _e.content) || ""; if (p) { process.stdout.write(chalk_1.default.greenBright(p)); msg += p; } // collect functino call const pfn = (_g = (_f = part.choices[0]) === null || _f === void 0 ? void 0 : _f.delta) === null || _g === void 0 ? void 0 : _g.function_call; if (pfn) { if (pfn.name) { fcall.name += pfn.name; } if (pfn.arguments) { fcall.arguments += pfn.arguments; } } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_h && !_a && (_b = stream_1.return)) yield _b.call(stream_1); } finally { if (e_1) throw e_1.error; } } stream.controller.abort(); if (fcall.name) { // function call messages.push(assistantFn(fcall)); const plugin = plugins.get(fcall.name); if (!plugin) { throw new Error("Unregistered function: " + fcall.name); } const toolkit = { log: (0, nicechat_1.logger)(`[${plugin.meta.name}]`), debug: isDebug ? (0, nicechat_1.logger)(`[${plugin.meta.name}]`) : () => { }, }; console.log(`[${chalk_1.default.blueBright(plugin.meta.name)}]: ${chalk_1.default.yellowBright(fcall.arguments.replace(/\n/g, " ").replace(/\s+/g, " "))}`); const fnResult = yield plugin.execute(fcall.arguments, { toolkit }); console.log(); messages.push(fnResultResp(fcall.name, fnResult)); } else { messages.push(assistant(msg)); // ask user for next input console.log("\n"); const input = yield (0, nicechat_1.readLine)(); messages.push(user(input)); } } }); } exports.chatOpenai = chatOpenai; // helpers function system(content) { return { role: "system", content }; } function user(content) { return { role: "user", content }; } function assistant(content) { return { role: "assistant", content }; } function assistantFn(function_call) { return { role: "assistant", content: null, function_call }; } function fnResultResp(name, content) { return { role: "function", name, content }; }