Long-term Memory for LangChain Apps
Building an Agent using LangChain and Zep Open Source
You are viewing the Zep Open Source v0.x documentation. This version is no longer supported, and documentation is provided as a reference only.
The current documentation for Zep Community Edition is available here.
Langchain Python and LangchainJS ship with a ZepMemory
class.
Managing Chat History Memory
Zep’s ZepMemory
class can be used to provide long-term memory for your Langchain chat apps or agents. Zep will store the entire historical message stream, automatically summarize messages, enrich them with token counts, timestamps, metadata and more.
You can also provide your bot or agent with access to relevant messages in long-term storage by using Zep’s built-in vector search.
Note: Installing Zep
A Zep server install is required. You will also need to have the Zep Python SDK or Zep typescript SDK installed in order to use the Langchain
ZepMemory
class.See the Zep Quick Start Guide for details.
Using Zep as a LangChain Memory Store
Using Zep with the LangChain’s Python LangChain Expression Language (LCEL)
The ZepChatMessageHistory
class is used to provide long-term memory to LangChain apps built using the LangChain Expression Language (LCEL).
First we’ll create a ChatPromptTemplate
that includes a placeholder for the message history. Then we’ll create a ChatOpenAI
model and chain it to the prompt.
Now we’ll instantiate a ZepChatMessageHistory
, provide it with a unique session ID, and the Zep server URL and API key (if required). We’ll also create a RunnableWithMessageHistory
that will provide the message history to the chain. We’ll specify the input and history message keys. The input messages key is the key that the chain will use to access the input messages. The history messages key is the key that the chain will use to access the message history. Ensure that these match your prompt template.
Let’s invoke the chain again, this time with a different question. We’ll also print the message history to see what’s been stored.
The prior message context was used to answer the question. Zep automatically generates summaries of the message history. These will be added to the message history sent to LAngChain and can be used to provide long-term context to the chain.
Using LangChain’s ZepMemory class
Note:
ZepMemory
is used with the legacy LangChain Python APIThe following examples illustrate using Zep as a Langchain memory store. In Python, the
ZepMemory
class is used when building LangChain app’s with the legacy API. See above for details on building apps with the LangChain Expression Language (LCEL).
Python
Using Zep as your Langchain app’s long-term memory simple: initialize the ZepMemory
with your Zep instance URL, API key, and your user’s session identifier.
TypeScript
Using Zep as your Langchain app’s long-term memory simple: initialize the ZepMemory with your Zep instance URL and your user’s session identifier (see Zep Concepts) and then utilize it as the chat memory for a Langchain Conversation.
Once you’ve created the memory
, use it in your chain or with your agent.
Python
Inspecting the zep_chat_history
, we see that a summary was automatically created, and that the history has been enriched with token counts, UUIDs, and timestamps.
TypeScript
Inspecting the ZepMemory
, we see that a summary was automatically created, and that the history has been enriched with token counts, UUIDs, and timestamps.
Search Zep’s message history from your Langchain app
Zep supports both vector search using a Langchain Retriever
as well as via an instance of ZepMemory
.
Using a ZepMemory instance
If you don’t need to provide a Retriever
to your chain or agent, you can search the long-term message history for a session directly from an instance of ZepMemory
.