{"version":3,"file":"summary.cjs","names":["BaseChatMemory","SUMMARY_PROMPT","SystemMessage","LLMChain"],"sources":["../../src/memory/summary.ts"],"sourcesContent":["import type { BaseLanguageModelInterface } from \"@langchain/core/language_models/base\";\nimport {\n  BaseMessage,\n  SystemMessage,\n  getBufferString,\n} from \"@langchain/core/messages\";\nimport { BasePromptTemplate } from \"@langchain/core/prompts\";\nimport {\n  InputValues,\n  MemoryVariables,\n  OutputValues,\n} from \"@langchain/core/memory\";\nimport { LLMChain } from \"../chains/llm_chain.js\";\nimport { SUMMARY_PROMPT } from \"./prompt.js\";\nimport { BaseChatMemory, BaseChatMemoryInput } from \"./chat_memory.js\";\n\n/**\n * Interface for the input parameters of the ConversationSummaryMemory\n * class.\n */\nexport interface ConversationSummaryMemoryInput extends BaseConversationSummaryMemoryInput {}\n\n/**\n * Interface for the input parameters of the BaseConversationSummaryMemory\n * class.\n */\nexport interface BaseConversationSummaryMemoryInput extends BaseChatMemoryInput {\n  llm: BaseLanguageModelInterface;\n  memoryKey?: string;\n  humanPrefix?: string;\n  aiPrefix?: string;\n  prompt?: BasePromptTemplate;\n  summaryChatMessageClass?: new (content: string) => BaseMessage;\n}\n\n/**\n * Abstract class that provides a structure for storing and managing the\n * memory of a conversation. It includes methods for predicting a new\n * summary for the conversation given the existing messages and summary.\n */\nexport abstract class BaseConversationSummaryMemory extends BaseChatMemory {\n  memoryKey = \"history\";\n\n  humanPrefix = \"Human\";\n\n  aiPrefix = \"AI\";\n\n  llm: BaseLanguageModelInterface;\n\n  prompt: BasePromptTemplate = SUMMARY_PROMPT;\n\n  summaryChatMessageClass: new (content: string) => BaseMessage = SystemMessage;\n\n  constructor(fields: BaseConversationSummaryMemoryInput) {\n    const {\n      returnMessages,\n      inputKey,\n      outputKey,\n      chatHistory,\n      humanPrefix,\n      aiPrefix,\n      llm,\n      prompt,\n      summaryChatMessageClass,\n    } = fields;\n\n    super({ returnMessages, inputKey, outputKey, chatHistory });\n\n    this.memoryKey = fields?.memoryKey ?? this.memoryKey;\n    this.humanPrefix = humanPrefix ?? this.humanPrefix;\n    this.aiPrefix = aiPrefix ?? this.aiPrefix;\n    this.llm = llm;\n    this.prompt = prompt ?? this.prompt;\n    this.summaryChatMessageClass =\n      summaryChatMessageClass ?? this.summaryChatMessageClass;\n  }\n\n  /**\n   * Predicts a new summary for the conversation given the existing messages\n   * and summary.\n   * @param messages Existing messages in the conversation.\n   * @param existingSummary Current summary of the conversation.\n   * @returns A promise that resolves to a new summary string.\n   */\n  async predictNewSummary(\n    messages: BaseMessage[],\n    existingSummary: string\n  ): Promise<string> {\n    const newLines = getBufferString(messages, this.humanPrefix, this.aiPrefix);\n    const chain = new LLMChain({ llm: this.llm, prompt: this.prompt });\n    return await chain.predict({\n      summary: existingSummary,\n      new_lines: newLines,\n    });\n  }\n}\n\n/**\n * Class that provides a concrete implementation of the conversation\n * memory. It includes methods for loading memory variables, saving\n * context, and clearing the memory.\n * @example\n * ```typescript\n * const memory = new ConversationSummaryMemory({\n *   memoryKey: \"chat_history\",\n *   llm: new ChatOpenAI({ model: \"gpt-3.5-turbo\", temperature: 0 }),\n * });\n *\n * const model = new ChatOpenAI({ model: \"gpt-4o-mini\" });\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 * const chain = new LLMChain({ llm: model, prompt, memory });\n *\n * const res1 = await chain.call({ input: \"Hi! I'm Jim.\" });\n * console.log({ res1, memory: await memory.loadMemoryVariables({}) });\n *\n * const res2 = await chain.call({ input: \"What's my name?\" });\n * console.log({ res2, memory: await memory.loadMemoryVariables({}) });\n *\n * ```\n */\nexport class ConversationSummaryMemory extends BaseConversationSummaryMemory {\n  buffer = \"\";\n\n  constructor(fields: ConversationSummaryMemoryInput) {\n    super(fields);\n  }\n\n  get memoryKeys() {\n    return [this.memoryKey];\n  }\n\n  /**\n   * Loads the memory variables for the conversation memory.\n   * @returns A promise that resolves to an object containing the memory variables.\n   */\n  async loadMemoryVariables(_: InputValues): Promise<MemoryVariables> {\n    if (this.returnMessages) {\n      const result = {\n        [this.memoryKey]: [new this.summaryChatMessageClass(this.buffer)],\n      };\n      return result;\n    }\n    const result = { [this.memoryKey]: this.buffer };\n    return result;\n  }\n\n  /**\n   * Saves the context of the conversation memory.\n   * @param inputValues Input values for the conversation.\n   * @param outputValues Output values from the conversation.\n   * @returns A promise that resolves when the context has been saved.\n   */\n  async saveContext(\n    inputValues: InputValues,\n    outputValues: OutputValues\n  ): Promise<void> {\n    await super.saveContext(inputValues, outputValues);\n    const messages = await this.chatHistory.getMessages();\n    this.buffer = await this.predictNewSummary(messages.slice(-2), this.buffer);\n  }\n\n  /**\n   * Clears the conversation memory.\n   * @returns A promise that resolves when the memory has been cleared.\n   */\n  async clear() {\n    await super.clear();\n    this.buffer = \"\";\n  }\n}\n"],"mappings":";;;;;;;;;;;AAwCA,IAAsB,gCAAtB,cAA4DA,2BAAAA,eAAe;CACzE,YAAY;CAEZ,cAAc;CAEd,WAAW;CAEX;CAEA,SAA6BC,eAAAA;CAE7B,0BAAgEC,yBAAAA;CAEhE,YAAY,QAA4C;EACtD,MAAM,EACJ,gBACA,UACA,WACA,aACA,aACA,UACA,KACA,QACA,4BACE;AAEJ,QAAM;GAAE;GAAgB;GAAU;GAAW;GAAa,CAAC;AAE3D,OAAK,YAAY,QAAQ,aAAa,KAAK;AAC3C,OAAK,cAAc,eAAe,KAAK;AACvC,OAAK,WAAW,YAAY,KAAK;AACjC,OAAK,MAAM;AACX,OAAK,SAAS,UAAU,KAAK;AAC7B,OAAK,0BACH,2BAA2B,KAAK;;;;;;;;;CAUpC,MAAM,kBACJ,UACA,iBACiB;EACjB,MAAM,YAAA,GAAA,yBAAA,iBAA2B,UAAU,KAAK,aAAa,KAAK,SAAS;AAE3E,SAAO,MADO,IAAIC,kBAAAA,SAAS;GAAE,KAAK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