ConversationVectorStoreTokenBufferMemory#

class langchain.memory.vectorstore_token_buffer_memory.ConversationVectorStoreTokenBufferMemory[source]#

Bases: ConversationTokenBufferMemory

Conversation chat memory with token limit and vectordb backing.

load_memory_variables() will return a dict with the key β€œhistory”. It contains background information retrieved from the vector store plus recent lines of the current conversation.

To help the LLM understand the part of the conversation stored in the vectorstore, each interaction is timestamped and the current date and time is also provided in the history. A side effect of this is that the LLM will have access to the current date and time.

Initialization arguments:

This class accepts all the initialization arguments of ConversationTokenBufferMemory, such as llm. In addition, it accepts the following additional arguments

retriever: (required) A VectorStoreRetriever object to use

as the vector backing store

split_chunk_size: (optional, 1000) Token chunk split size

for long messages generated by the AI

previous_history_template: (optional) Template used to format

the contents of the prompt history

Example using ChromaDB:

from langchain.memory.token_buffer_vectorstore_memory import (
        ConversationVectorStoreTokenBufferMemory
)
from langchain_chroma import Chroma
from langchain_community.embeddings import HuggingFaceInstructEmbeddings
from langchain_openai import OpenAI

embedder = HuggingFaceInstructEmbeddings(
                query_instruction="Represent the query for retrieval: "
)
chroma = Chroma(collection_name="demo",
                embedding_function=embedder,
                collection_metadata={"hnsw:space": "cosine"},
                )

retriever = chroma.as_retriever(
        search_type="similarity_score_threshold",
        search_kwargs={
            'k': 5,
            'score_threshold': 0.75,
        },
)

conversation_memory = ConversationVectorStoreTokenBufferMemory(
        return_messages=True,
        llm=OpenAI(),
        retriever=retriever,
        max_token_limit = 1000,
)

conversation_memory.save_context({"Human": "Hi there"},
                                  {"AI": "Nice to meet you!"}
)
conversation_memory.save_context({"Human": "Nice day isn't it?"},
                                  {"AI": "I love Wednesdays."}
)
conversation_memory.load_memory_variables({"input": "What time is it?"})
param ai_prefix: str = 'AI'#
param chat_memory: BaseChatMessageHistory [Optional]#
param human_prefix: str = 'Human'#
param input_key: str | None = None#
param llm: BaseLanguageModel [Required]#
param max_token_limit: int = 2000#
param memory_key: str = 'history'#
param output_key: str | None = None#
param previous_history_template: str = '\nCurrent date and time: {current_time}.\n\nPotentially relevant timestamped excerpts of previous conversations (you \ndo not need to use these if irrelevant):\n{previous_history}\n\n'#
param retriever: VectorStoreRetriever [Required]#
param return_messages: bool = False#
param split_chunk_size: int = 1000#
async aclear() β†’ None#

Clear memory contents.

Return type:

None

async aload_memory_variables(inputs: dict[str, Any]) β†’ dict[str, Any]#

Async return key-value pairs given the text input to the chain.

Parameters:

inputs (dict[str, Any]) – The inputs to the chain.

Returns:

A dictionary of key-value pairs.

Return type:

dict[str, Any]

async asave_context(inputs: Dict[str, Any], outputs: Dict[str, str]) β†’ None#

Save context from this conversation to buffer.

Parameters:
  • inputs (Dict[str, Any])

  • outputs (Dict[str, str])

Return type:

None

clear() β†’ None#

Clear memory contents.

Return type:

None

load_memory_variables(inputs: Dict[str, Any]) β†’ Dict[str, Any][source]#

Return history and memory buffer.

Parameters:

inputs (Dict[str, Any])

Return type:

Dict[str, Any]

save_context(inputs: Dict[str, Any], outputs: Dict[str, str]) β†’ None[source]#

Save context from this conversation to buffer. Pruned.

Parameters:
  • inputs (Dict[str, Any])

  • outputs (Dict[str, str])

Return type:

None

save_remainder() β†’ None[source]#

Save the remainder of the conversation buffer to the vector store.

This is useful if you have made the vectorstore persistent, in which case this can be called before the end of the session to store the remainder of the conversation.

Return type:

None

property buffer: Any#

String buffer of memory.

property buffer_as_messages: List[BaseMessage]#

Exposes the buffer as a list of messages in case return_messages is True.

property buffer_as_str: str#

Exposes the buffer as a string in case return_messages is False.

property memory_retriever: VectorStoreRetrieverMemory#

Return a memory retriever from the passed retriever object.