UNPKG

hugbot

Version:

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

68 lines 4.96 kB
"use strict"; 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 __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var _ShortTermMemory_instances, _ShortTermMemory_memory, _ShortTermMemory_popLeftIfMemoryOverflow, _ShortTermMemory_isMemoryOverflow, _ShortTermMemory_totalTokens_get, _ShortTermMemory_conversationTokens_get, _ShortTermMemory_averageBotReplyTokens_get; Object.defineProperty(exports, "__esModule", { value: true }); exports.ShortTermMemory = void 0; const MistralTokenizer_1 = require("../Tokenizers/MistralTokenizer"); /** * Conversation memory buffer with token counter for managing limited context window lengths. * @method {push} - Adds entries to memory buffer. * @method {dump} - Retrieves current memory state including system prompt and response instructions. * @property {contextWindow} - Buffer length can be adjusted with contextWindow property. * @property {systemPrompt} {responseAffirmation} {userInstruction} - Stores current system prompt and other AI prompt template elements. * @property {tokenizer} - Tokenizer function used to calculate ammount of tokens across all memory entries. */ class ShortTermMemory { constructor(params) { _ShortTermMemory_instances.add(this); _ShortTermMemory_memory.set(this, []); this.tokenizer = MistralTokenizer_1.mistralTokenizer; this.contextWindow = 4096; this.systemPrompt = "You are a helpful AI assistant."; this.responseAffirmation = ""; this.userInstruction = ""; if (params) Object.entries(params).forEach(([key, value]) => Object.assign(this, { [key]: value })); } push(entry) { __classPrivateFieldSet(this, _ShortTermMemory_memory, __classPrivateFieldGet(this, _ShortTermMemory_memory, "f").concat(entry), "f"); __classPrivateFieldGet(this, _ShortTermMemory_instances, "m", _ShortTermMemory_popLeftIfMemoryOverflow).call(this); } get dump() { return { conversation: __classPrivateFieldGet(this, _ShortTermMemory_memory, "f").slice(), systemPrompt: this.systemPrompt, responseAffirmation: this.responseAffirmation, userInstruction: this.userInstruction }; } } exports.ShortTermMemory = ShortTermMemory; _ShortTermMemory_memory = new WeakMap(), _ShortTermMemory_instances = new WeakSet(), _ShortTermMemory_popLeftIfMemoryOverflow = function _ShortTermMemory_popLeftIfMemoryOverflow() { while (__classPrivateFieldGet(this, _ShortTermMemory_instances, "m", _ShortTermMemory_isMemoryOverflow).call(this)) __classPrivateFieldSet(this, _ShortTermMemory_memory, __classPrivateFieldGet(this, _ShortTermMemory_memory, "f").slice(1), "f"); }, _ShortTermMemory_isMemoryOverflow = function _ShortTermMemory_isMemoryOverflow() { return __classPrivateFieldGet(this, _ShortTermMemory_instances, "a", _ShortTermMemory_totalTokens_get) > this.contextWindow - __classPrivateFieldGet(this, _ShortTermMemory_instances, "a", _ShortTermMemory_averageBotReplyTokens_get); }, _ShortTermMemory_totalTokens_get = function _ShortTermMemory_totalTokens_get() { return __classPrivateFieldGet(this, _ShortTermMemory_instances, "a", _ShortTermMemory_conversationTokens_get) + this.tokenizer(this.systemPrompt) + this.tokenizer(this.responseAffirmation) + this.tokenizer(this.userInstruction); }, _ShortTermMemory_conversationTokens_get = function _ShortTermMemory_conversationTokens_get() { return __classPrivateFieldGet(this, _ShortTermMemory_memory, "f").reduce((acc, x) => acc + this.tokenizer(x.input), 0); }, _ShortTermMemory_averageBotReplyTokens_get = function _ShortTermMemory_averageBotReplyTokens_get() { const aiReplies = __classPrivateFieldGet(this, _ShortTermMemory_memory, "f").filter(x => x.role === "ai"); const avg = aiReplies.reduce((acc, x) => acc + this.tokenizer(x.input), 0); return Math.ceil(avg / aiReplies.length); }; //# sourceMappingURL=ShortTermMemory.js.map