Class VectorStoreRetrieverMemory

Class for managing long-term memory in Large Language Model (LLM) applications. It provides a way to persist and retrieve relevant documents from a vector store database, which can be useful for maintaining conversation history or other types of memory in an LLM application.

Example

const vectorStore = new MemoryVectorStore(new OpenAIEmbeddings());
const memory = new VectorStoreRetrieverMemory({
vectorStoreRetriever: vectorStore.asRetriever(1),
memoryKey: "history",
});

// Saving context to memory
await memory.saveContext(
{ input: "My favorite food is pizza" },
{ output: "thats good to know" },
);
await memory.saveContext(
{ input: "My favorite sport is soccer" },
{ output: "..." },
);
await memory.saveContext({ input: "I don't the Celtics" }, { output: "ok" });

// Loading memory variables
console.log(
await memory.loadMemoryVariables({ prompt: "what sport should i watch?" }),
);

const model = new ChatAnthropic({ temperature: 0.9 });
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.

Relevant pieces of previous conversation:
{history}

(You do not need to use these pieces of information if not relevant)

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

// Making calls to the chain with different inputs
const res1 = await chain.call({ input: "Hi, my name is Perry, what's up?" });
console.log({ res1 });

const res2 = await chain.call({ input: "what's my favorite sport?" });
console.log({ res2 });

const res3 = await chain.call({ input: "what's my name?" });
console.log({ res3 });

Hierarchy

Implements

Constructors

Properties

memoryKey: string
returnDocs: boolean
vectorStoreRetriever: VectorStoreRetriever<VectorStore>
inputKey?: string

Accessors

  • get memoryKeys(): string[]
  • Returns string[]

Methods

  • Method to load memory variables. It uses the vectorStoreRetriever to get relevant documents based on the query obtained from the input values.

    Parameters

    Returns Promise<MemoryVariables>

    A Promise that resolves to a MemoryVariables object.

  • Method to save context. It constructs a document from the input and output values (excluding the memory key) and adds it to the vector store database using the vectorStoreRetriever.

    Parameters

    Returns Promise<void>

    A Promise that resolves to void.

Generated using TypeDoc