Class that provides a concrete implementation of the conversation memory. It includes methods for loading memory variables, saving context, and clearing the memory.

Example

const memory = new ConversationSummaryMemory({
memoryKey: "chat_history",
llm: new ChatOpenAI({ modelName: "gpt-3.5-turbo", temperature: 0 }),
});

const model = new ChatOpenAI();
const prompt =
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.

Current conversation:
{chat_history}
Human: {input}
AI:`);
const chain = new LLMChain({ llm: model, prompt, memory });

const res1 = await chain.call({ input: "Hi! I'm Jim." });
console.log({ res1, memory: await memory.loadMemoryVariables({}) });

const res2 = await chain.call({ input: "What's my name?" });
console.log({ res2, memory: await memory.loadMemoryVariables({}) });

Hierarchy (view full)

Constructors

Properties

aiPrefix: string = "AI"
buffer: string = ""
chatHistory: BaseChatMessageHistory
humanPrefix: string = "Human"
llm: BaseLanguageModelInterface<any, BaseLanguageModelCallOptions>
memoryKey: string = "history"
prompt: BasePromptTemplate<any, BasePromptValueInterface, any> = SUMMARY_PROMPT
returnMessages: boolean = false
summaryChatMessageClass: (new (content) => BaseMessage) = SystemMessage

Type declaration

    • new (content): BaseMessage
    • Parameters

      • content: string

      Returns BaseMessage

inputKey?: string
outputKey?: string

Accessors

Methods

  • Loads the memory variables for the conversation memory.

    Parameters

    • _: InputValues

    Returns Promise<MemoryVariables>

    A promise that resolves to an object containing the memory variables.

  • Predicts a new summary for the conversation given the existing messages and summary.

    Parameters

    • messages: BaseMessage[]

      Existing messages in the conversation.

    • existingSummary: string

      Current summary of the conversation.

    Returns Promise<string>

    A promise that resolves to a new summary string.

  • Saves the context of the conversation memory.

    Parameters

    • inputValues: InputValues

      Input values for the conversation.

    • outputValues: OutputValues

      Output values from the conversation.

    Returns Promise<void>

    A promise that resolves when the context has been saved.

Generated using TypeDoc