langchain.memory.vectorstore_token_buffer_memory
.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: Optional[str] = NoneΒΆ
- param llm: BaseLanguageModel [Required]ΒΆ
- param max_token_limit: int = 2000ΒΆ
- param memory_key: str = 'history'ΒΆ
- param output_key: Optional[str] = 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.