MessageHistory Example
The Zep Memory, VectorStore, and Retriever classes found in the LangChain project are deprecated. LangGraph is the recommended approach to using Zep with the LangChain ecosystem.
LangChain offers a ZepChatMessageHistory
class compatible with LangChain Expression Language (LCEL).
This guide will walk you through creating a MessageHistory chain using Zep’s conversation history.
Make sure you have the following environment variables specified when running these examples:
ZEP_API_KEY
- API key to your zep project
OPENAI_API_KEY
- Open AI api key which the chain will require to generate the answer
You will need to have a collection in place to initialize vector store in this example
If you want to create a collection from a web article, you can run the python ingest script Try modifying the script to ingest the article of your choice.
Alternatively, you can create a collection by running either Document example in python sdk repository or Document example in typescript sdk repository.
You will need to have a session_id
in place to invoke the final chain in this example
You can create a session by running either Memory example
in python sdk repository or Memory example in typescript sdk repository.
Initialize ZepClient with necessary imports
Python
TypeScript
Python
TypeScript
Set up an answer synthesis template and prompt.
MessagesPlaceholder
- We’re using the variable name chat_history
here.
This will incorporate the chat history into the prompt.
It’s important that this variable name aligns with the history_messages_key
in the RunnableWithMessageHistory
chain for seamless integration.
question
must match input_messages_key
in `RunnableWithMessageHistory“ chain. Compose the final chain
Python
TypeScript
Here’s a quick overview of what’s happening:
- We use
RunnableWithMessageHistory
to incorporate Zep’s Chat History into our chain. - This class requires a
session_id
as a parameter when you activate the chain. - To manually invoke this chain, provide the
session_id
as a parameter and thequestion
as an input to the chain.
Python
TypeScript
First, we initialize ZepChatMessageHistory
with the following parameters:
session_id
- This uniquely identifies the conversation within Zep.zep_client
- The instance of the Zep client.memory_type
set toperpetual
. If not specified, Message Window Buffer Memory will be used by default. We recommend configuring your application to use Perpetual Memory.
Interested in learning more? Explore How Zep Memory Works.
Next, we construct a chain that operates after retrieving the chat history:
inputs
will extract the user’s question and chat history from the context.answer_prompt
will incorporate chat history into the prompt.ChatOpenAI
will generate a response.StrOutputParser
will parse the response.
Running the Chain with LangServe
This chain can also be executed as part of our LangServe sample project. To do this, you’ll need to:
For this you will need to:
Clone our Python SDK
There is a README file in the langchain-langserve
directory will guide you through the setup process.
Go to http://localhost:8000/message_history/playground
to use LangServe playground for this chain.