UNPKG

hugbot

Version:

Chatbot maker for HuggingFace Inference API and other AI API providers and backends.

99 lines 4.7 kB
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _PromptConstructor_instances, _a, _PromptConstructor_buildConversation, _PromptConstructor_addBosTag, _PromptConstructor_addUserEntry, _PromptConstructor_addAIentry, _PromptConstructor_addSysPrompt, _PromptConstructor_addAIOpening, _PromptConstructor_addUserInstruction; /** * AI Prompt formatter. Supports most of the common models with standard tag formats. * Does NOT support Mistral, use MistralPromptConstructor instead. * @param tags Use tags param to specify the prompt tag format. * @example * ```json * tags: { * bos: "", * system: "<|im_start|>system\n", * user: "<|im_start|>user\n", * bot: "<|im_start|>assistant\n", * closing: "<|im_end|>\n", * } * ``` * @method getPromptTemplate - Takes the MemoryDump object and returns formatted AI prompt. * @example * ```json * memoryDump: { * conversation: [ * { * role: "user", * input: "hi", * }, * { * role: "ai", * input: "Hello, how can I help you?" * }, * { * role: "user", * input: "Tell me something interesting" * } * ], * systemPrompt: "You are a helpful AI assistant." * responseAffirmation: "", * userInstruction: "" * } * ``` * * ===> * * ```string * <|im_start|>system * You are a helpful AI assistant.<|im_end|> * <|im_start|>user * hi<|im_end|> * <|im_start|>assistant * Hello, how can I help you?<|im_end> * <|im_start|>user * Tell me something interesting<|im_end|> * <|im_start>assistant * ``` */ export class PromptConstructor { constructor(tags = { bos: "", system: "<|im_start|>system\n", user: "<|im_start|>user\n", bot: "<|im_start|>assistant\n", closing: "<|im_end|>\n", }) { _PromptConstructor_instances.add(this); this.tags = tags; } getPromptTemplate(memoryDump) { const conv = JSON.parse(JSON.stringify(memoryDump.conversation)); conv[conv.length - 1].input = __classPrivateFieldGet(_a, _a, "m", _PromptConstructor_addUserInstruction).call(_a, memoryDump); return __classPrivateFieldGet(this, _PromptConstructor_instances, "m", _PromptConstructor_addBosTag).call(this) + __classPrivateFieldGet(this, _PromptConstructor_instances, "m", _PromptConstructor_addSysPrompt).call(this, memoryDump.systemPrompt) + __classPrivateFieldGet(this, _PromptConstructor_instances, "m", _PromptConstructor_buildConversation).call(this, conv) + __classPrivateFieldGet(this, _PromptConstructor_instances, "m", _PromptConstructor_addAIOpening).call(this, memoryDump.responseAffirmation); } } _a = PromptConstructor, _PromptConstructor_instances = new WeakSet(), _PromptConstructor_buildConversation = function _PromptConstructor_buildConversation(conv) { return conv.map((x) => { return x.role === 'user' ? __classPrivateFieldGet(this, _PromptConstructor_instances, "m", _PromptConstructor_addUserEntry).call(this, x.input) : __classPrivateFieldGet(this, _PromptConstructor_instances, "m", _PromptConstructor_addAIentry).call(this, x.input); }).join(''); }, _PromptConstructor_addBosTag = function _PromptConstructor_addBosTag() { return this.tags.bos; }, _PromptConstructor_addUserEntry = function _PromptConstructor_addUserEntry(input) { return `${this.tags.user}${input}${this.tags.closing}`; }, _PromptConstructor_addAIentry = function _PromptConstructor_addAIentry(input) { return `${this.tags.bot}${input}${this.tags.closing}`; }, _PromptConstructor_addSysPrompt = function _PromptConstructor_addSysPrompt(sysPrompt) { return sysPrompt ? `${this.tags.system}${sysPrompt}${this.tags.closing}` : ""; }, _PromptConstructor_addAIOpening = function _PromptConstructor_addAIOpening(responseAffirmation) { return `${this.tags.bot}${responseAffirmation}`; }, _PromptConstructor_addUserInstruction = function _PromptConstructor_addUserInstruction(memoryDump) { const input = memoryDump.conversation[memoryDump.conversation.length - 1].input; return memoryDump.userInstruction ? `${input} ${memoryDump.userInstruction}` : `${input}`; }; //# sourceMappingURL=PromptConstructor.js.map