UNPKG

polyfact

Version:

<h1 align="center">PolyFact</h1>

306 lines (305 loc) 14.9 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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 __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Chat = exports.createChat = void 0; var axios_1 = __importStar(require("axios")); var t = __importStar(require("polyfact-io-ts")); var generate_1 = require("../generate"); var clientOpts_1 = require("../clientOpts"); var memory_1 = require("../memory"); var error_1 = require("../helpers/error"); var dataloader_1 = require("../dataloader"); var Message = t.type({ id: t.string, chat_id: t.string, is_user_message: t.boolean, content: t.string, created_at: t.string, }); function createChat(systemPrompt, systemPromptId, options) { var _a, _b; if (options === void 0) { options = {}; } return __awaiter(this, void 0, void 0, function () { var _c, token, endpoint, body, response, e_1; return __generator(this, function (_d) { switch (_d.label) { case 0: _d.trys.push([0, 3, , 4]); return [4 /*yield*/, (0, clientOpts_1.defaultOptions)(options)]; case 1: _c = _d.sent(), token = _c.token, endpoint = _c.endpoint; body = __assign(__assign({}, (systemPromptId ? { system_prompt_id: systemPromptId } : {})), (systemPrompt && !systemPromptId ? { system_prompt: systemPrompt } : {})); return [4 /*yield*/, axios_1.default.post("".concat(endpoint, "/chats"), body, { headers: { "X-Access-Token": token, }, })]; case 2: response = _d.sent(); return [2 /*return*/, (_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.id]; case 3: e_1 = _d.sent(); if (e_1 instanceof axios_1.AxiosError) { throw new error_1.ApiError((_b = e_1 === null || e_1 === void 0 ? void 0 : e_1.response) === null || _b === void 0 ? void 0 : _b.data); } throw e_1; case 4: return [2 /*return*/]; } }); }); } exports.createChat = createChat; var Chat = /** @class */ (function () { function Chat(options, clientOptions) { if (options === void 0) { options = {}; } if (clientOptions === void 0) { clientOptions = {}; } this.options = options; this.clientOptions = (0, clientOpts_1.defaultOptions)(clientOptions); this.chatId = createChat(options.systemPrompt, options.systemPromptId, this.clientOptions); this.options.provider = options.provider || ""; if (options.autoMemory) { this.autoMemory = this.clientOptions.then(function (co) { return new memory_1.Memory(co); }); } } Chat.prototype.dataLoader = function (data, onProgress) { return __awaiter(this, void 0, void 0, function () { var memory, _a, error_2; return __generator(this, function (_b) { switch (_b.label) { case 0: _b.trys.push([0, 3, , 4]); onProgress("START_LOADING"); return [4 /*yield*/, (0, dataloader_1.loaderToMemory)(data, this.clientOptions)]; case 1: memory = _b.sent(); onProgress("GET_MEMORY_ID"); _a = this; return [4 /*yield*/, memory.memoryId]; case 2: _a.memoryId = _b.sent(); onProgress("FULLY_LOADED"); return [3 /*break*/, 4]; case 3: error_2 = _b.sent(); onProgress("LOAD_ERROR"); console.error("Error loading data into memory:", error_2); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }; Chat.prototype.sendMessage = function (message, options) { var _this = this; if (options === void 0) { options = {}; } var stopped = false; var resultStream = new generate_1.Generation({ stop: function () { stopped = true; resultStream.push(null); }, clientOptions: this.clientOptions, }); var aiMessage = ""; (function () { return __awaiter(_this, void 0, void 0, function () { var chatId, genOptions, _a, result, _b, _c; var _this = this; return __generator(this, function (_d) { switch (_d.label) { case 0: return [4 /*yield*/, this.chatId]; case 1: chatId = _d.sent(); genOptions = options; if (!(this.autoMemory && !genOptions.memory && !genOptions.memoryId)) return [3 /*break*/, 3]; _a = genOptions; return [4 /*yield*/, this.autoMemory]; case 2: _a.memory = _d.sent(); _d.label = 3; case 3: if (this.options.systemPromptId) { genOptions.systemPromptId = this.options.systemPromptId; } if (this.memoryId) { genOptions.memoryId = this.memoryId; } if (stopped) { return [2 /*return*/]; } _b = generate_1.generate; _c = [message, __assign(__assign(__assign({}, this.options), options), { web: false, chatId: chatId })]; return [4 /*yield*/, this.clientOptions]; case 4: result = _b.apply(void 0, _c.concat([_d.sent()])).pipeInto(resultStream); result.on("data", function (d) { aiMessage = aiMessage.concat(d); }); result.on("end", function () { (function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.autoMemory) return [3 /*break*/, 3]; return [4 /*yield*/, this.autoMemory]; case 1: (_a.sent()).add("Human: ".concat(message)); return [4 /*yield*/, this.autoMemory]; case 2: (_a.sent()).add("AI: ".concat(aiMessage)); _a.label = 3; case 3: return [2 /*return*/]; } }); }); })(); }); return [2 /*return*/]; } }); }); })(); return resultStream; }; Chat.prototype.getMessages = function () { var _a, _b; return __awaiter(this, void 0, void 0, function () { var response, _c, _d, _e, _f, _g, _h, _j, e_2; var _k, _l; return __generator(this, function (_m) { switch (_m.label) { case 0: _m.trys.push([0, 5, , 6]); _d = (_c = axios_1.default).get; _f = "".concat; return [4 /*yield*/, this.clientOptions]; case 1: _g = (_e = _f.apply("", [(_m.sent()).endpoint, "/chat/"])).concat; return [4 /*yield*/, this.chatId]; case 2: _h = [_g.apply(_e, [_m.sent(), "/history"])]; _k = {}; _l = {}; _j = "X-Access-Token"; return [4 /*yield*/, this.clientOptions]; case 3: return [4 /*yield*/, _d.apply(_c, _h.concat([(_k.headers = (_l[_j] = (_m.sent()).token, _l), _k)]))]; case 4: response = _m.sent(); return [2 /*return*/, (_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.filter(function (message) { return Message.is(message); })]; case 5: e_2 = _m.sent(); if (e_2 instanceof axios_1.AxiosError) { throw new error_1.ApiError((_b = e_2 === null || e_2 === void 0 ? void 0 : e_2.response) === null || _b === void 0 ? void 0 : _b.data); } throw e_2; case 6: return [2 /*return*/]; } }); }); }; return Chat; }()); exports.Chat = Chat; function client(clientOptions) { if (clientOptions === void 0) { clientOptions = {}; } return { createChat: function (systemPrompt, systemPromptId) { return createChat(systemPrompt, systemPromptId, clientOptions); }, Chat: /** @class */ (function (_super) { __extends(C, _super); function C(options) { if (options === void 0) { options = {}; } return _super.call(this, options, clientOptions) || this; } return C; }(Chat)), }; } exports.default = client;