{"version":3,"file":"buffer_memory.cjs","names":["BaseChatMemory"],"sources":["../../src/memory/buffer_memory.ts"],"sourcesContent":["import { InputValues, MemoryVariables } from \"@langchain/core/memory\";\nimport { getBufferString } from \"@langchain/core/messages\";\nimport { BaseChatMemory, BaseChatMemoryInput } from \"./chat_memory.js\";\n\n/**\n * Interface for the input parameters of the `BufferMemory` class.\n */\nexport interface BufferMemoryInput extends BaseChatMemoryInput {\n  humanPrefix?: string;\n  aiPrefix?: string;\n  memoryKey?: string;\n}\n\n/**\n * The `BufferMemory` class is a type of memory component used for storing\n * and managing previous chat messages. It is a wrapper around\n * `ChatMessageHistory` that extracts the messages into an input variable.\n * This class is particularly useful in applications like chatbots where\n * it is essential to remember previous interactions. Note: The memory\n * instance represents the history of a single conversation. Therefore, it\n * is not recommended to share the same history or memory instance between\n * two different chains. If you deploy your LangChain app on a serverless\n * environment, do not store memory instances in a variable, as your\n * hosting provider may reset it by the next time the function is called.\n * @example\n * ```typescript\n * // Initialize the memory to store chat history and set up the language model with a specific temperature.\n * const memory = new BufferMemory({ memoryKey: \"chat_history\" });\n * const model = new ChatOpenAI({ model: \"gpt-4o-mini\", temperature: 0.9 });\n *\n * // Create a prompt template for a friendly conversation between a human and an AI.\n * const prompt =\n *   PromptTemplate.fromTemplate(`The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n *\n * Current conversation:\n * {chat_history}\n * Human: {input}\n * AI:`);\n *\n * // Set up the chain with the language model, prompt, and memory.\n * const chain = new LLMChain({ llm: model, prompt, memory });\n *\n * // Example usage of the chain to continue the conversation.\n * // The `call` method sends the input to the model and returns the AI's response.\n * const res = await chain.call({ input: \"Hi! I'm Jim.\" });\n * console.log({ res });\n *\n * ```\n */\nexport class BufferMemory extends BaseChatMemory implements BufferMemoryInput {\n  humanPrefix = \"Human\";\n\n  aiPrefix = \"AI\";\n\n  memoryKey = \"history\";\n\n  constructor(fields?: BufferMemoryInput) {\n    super({\n      chatHistory: fields?.chatHistory,\n      returnMessages: fields?.returnMessages ?? false,\n      inputKey: fields?.inputKey,\n      outputKey: fields?.outputKey,\n    });\n    this.humanPrefix = fields?.humanPrefix ?? this.humanPrefix;\n    this.aiPrefix = fields?.aiPrefix ?? this.aiPrefix;\n    this.memoryKey = fields?.memoryKey ?? this.memoryKey;\n  }\n\n  get memoryKeys() {\n    return [this.memoryKey];\n  }\n\n  /**\n   * Loads the memory variables. It takes an `InputValues` object as a\n   * parameter and returns a `Promise` that resolves with a\n   * `MemoryVariables` object.\n   * @param _values `InputValues` object.\n   * @returns A `Promise` that resolves with a `MemoryVariables` object.\n   */\n  async loadMemoryVariables(_values: InputValues): Promise<MemoryVariables> {\n    const messages = await this.chatHistory.getMessages();\n    if (this.returnMessages) {\n      const result = {\n        [this.memoryKey]: messages,\n      };\n      return result;\n    }\n    const result = {\n      [this.memoryKey]: getBufferString(\n        messages,\n        this.humanPrefix,\n        this.aiPrefix\n      ),\n    };\n    return result;\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiDA,IAAa,eAAb,cAAkCA,2BAAAA,eAA4C;CAC5E,cAAc;CAEd,WAAW;CAEX,YAAY;CAEZ,YAAY,QAA4B;AACtC,QAAM;GACJ,aAAa,QAAQ;GACrB,gBAAgB,QAAQ,kBAAkB;GAC1C,UAAU,QAAQ;GAClB,WAAW,QAAQ;GACpB,CAAC;AACF,OAAK,cAAc,QAAQ,eAAe,KAAK;AAC/C,OAAK,WAAW,QAAQ,YAAY,KAAK;AACzC,OAAK,YAAY,QAAQ,aAAa,KAAK;;CAG7C,IAAI,aAAa;AACf,SAAO,CAAC,KAAK,UAAU;;;;;;;;;CAUzB,MAAM,oBAAoB,SAAgD;EACxE,MAAM,WAAW,MAAM,KAAK,YAAY,aAAa;AACrD,MAAI,KAAK,eAIP,QAHe,GACZ,KAAK,YAAY,UACnB;AAUH,SAPe,GACZ,KAAK,aAAA,GAAA,yBAAA,iBACJ,UACA,KAAK,aACL,KAAK,SACN,EACF"}