LangChain OpenTutorial
  • 🦜️🔗 The LangChain Open Tutorial for Everyone
  • 01-Basic
    • Getting Started on Windows
    • 02-Getting-Started-Mac
    • OpenAI API Key Generation and Testing Guide
    • LangSmith Tracking Setup
    • Using the OpenAI API (GPT-4o Multimodal)
    • Basic Example: Prompt+Model+OutputParser
    • LCEL Interface
    • Runnable
  • 02-Prompt
    • Prompt Template
    • Few-Shot Templates
    • LangChain Hub
    • Personal Prompts for LangChain
    • Prompt Caching
  • 03-OutputParser
    • PydanticOutputParser
    • PydanticOutputParser
    • CommaSeparatedListOutputParser
    • Structured Output Parser
    • JsonOutputParser
    • PandasDataFrameOutputParser
    • DatetimeOutputParser
    • EnumOutputParser
    • Output Fixing Parser
  • 04-Model
    • Using Various LLM Models
    • Chat Models
    • Caching
    • Caching VLLM
    • Model Serialization
    • Check Token Usage
    • Google Generative AI
    • Huggingface Endpoints
    • HuggingFace Local
    • HuggingFace Pipeline
    • ChatOllama
    • GPT4ALL
    • Video Q&A LLM (Gemini)
  • 05-Memory
    • ConversationBufferMemory
    • ConversationBufferWindowMemory
    • ConversationTokenBufferMemory
    • ConversationEntityMemory
    • ConversationKGMemory
    • ConversationSummaryMemory
    • VectorStoreRetrieverMemory
    • LCEL (Remembering Conversation History): Adding Memory
    • Memory Using SQLite
    • Conversation With History
  • 06-DocumentLoader
    • Document & Document Loader
    • PDF Loader
    • WebBaseLoader
    • CSV Loader
    • Excel File Loading in LangChain
    • Microsoft Word(doc, docx) With Langchain
    • Microsoft PowerPoint
    • TXT Loader
    • JSON
    • Arxiv Loader
    • UpstageDocumentParseLoader
    • LlamaParse
    • HWP (Hangeul) Loader
  • 07-TextSplitter
    • Character Text Splitter
    • 02. RecursiveCharacterTextSplitter
    • Text Splitting Methods in NLP
    • TokenTextSplitter
    • SemanticChunker
    • Split code with Langchain
    • MarkdownHeaderTextSplitter
    • HTMLHeaderTextSplitter
    • RecursiveJsonSplitter
  • 08-Embedding
    • OpenAI Embeddings
    • CacheBackedEmbeddings
    • HuggingFace Embeddings
    • Upstage
    • Ollama Embeddings With Langchain
    • LlamaCpp Embeddings With Langchain
    • GPT4ALL
    • Multimodal Embeddings With Langchain
  • 09-VectorStore
    • Vector Stores
    • Chroma
    • Faiss
    • Pinecone
    • Qdrant
    • Elasticsearch
    • MongoDB Atlas
    • PGVector
    • Neo4j
    • Weaviate
    • Faiss
    • {VectorStore Name}
  • 10-Retriever
    • VectorStore-backed Retriever
    • Contextual Compression Retriever
    • Ensemble Retriever
    • Long Context Reorder
    • Parent Document Retriever
    • MultiQueryRetriever
    • MultiVectorRetriever
    • Self-querying
    • TimeWeightedVectorStoreRetriever
    • TimeWeightedVectorStoreRetriever
    • Kiwi BM25 Retriever
    • Ensemble Retriever with Convex Combination (CC)
  • 11-Reranker
    • Cross Encoder Reranker
    • JinaReranker
    • FlashRank Reranker
  • 12-RAG
    • Understanding the basic structure of RAG
    • RAG Basic WebBaseLoader
    • Exploring RAG in LangChain
    • RAPTOR: Recursive Abstractive Processing for Tree-Organized Retrieval
    • Conversation-With-History
    • Translation
    • Multi Modal RAG
  • 13-LangChain-Expression-Language
    • RunnablePassthrough
    • Inspect Runnables
    • RunnableLambda
    • Routing
    • Runnable Parallel
    • Configure-Runtime-Chain-Components
    • Creating Runnable objects with chain decorator
    • RunnableWithMessageHistory
    • Generator
    • Binding
    • Fallbacks
    • RunnableRetry
    • WithListeners
    • How to stream runnables
  • 14-Chains
    • Summarization
    • SQL
    • Structured Output Chain
    • StructuredDataChat
  • 15-Agent
    • Tools
    • Bind Tools
    • Tool Calling Agent
    • Tool Calling Agent with More LLM Models
    • Iteration-human-in-the-loop
    • Agentic RAG
    • CSV/Excel Analysis Agent
    • Agent-with-Toolkits-File-Management
    • Make Report Using RAG, Web searching, Image generation Agent
    • TwoAgentDebateWithTools
    • React Agent
  • 16-Evaluations
    • Generate synthetic test dataset (with RAGAS)
    • Evaluation using RAGAS
    • HF-Upload
    • LangSmith-Dataset
    • LLM-as-Judge
    • Embedding-based Evaluator(embedding_distance)
    • LangSmith Custom LLM Evaluation
    • Heuristic Evaluation
    • Compare experiment evaluations
    • Summary Evaluators
    • Groundedness Evaluation
    • Pairwise Evaluation
    • LangSmith Repeat Evaluation
    • LangSmith Online Evaluation
    • LangFuse Online Evaluation
  • 17-LangGraph
    • 01-Core-Features
      • Understanding Common Python Syntax Used in LangGraph
      • Title
      • Building a Basic Chatbot with LangGraph
      • Building an Agent with LangGraph
      • Agent with Memory
      • LangGraph Streaming Outputs
      • Human-in-the-loop
      • LangGraph Manual State Update
      • Asking Humans for Help: Customizing State in LangGraph
      • DeleteMessages
      • DeleteMessages
      • LangGraph ToolNode
      • LangGraph ToolNode
      • Branch Creation for Parallel Node Execution
      • Conversation Summaries with LangGraph
      • Conversation Summaries with LangGraph
      • LangGrpah Subgraph
      • How to transform the input and output of a subgraph
      • LangGraph Streaming Mode
      • Errors
      • A Long-Term Memory Agent
    • 02-Structures
      • LangGraph-Building-Graphs
      • Naive RAG
      • Add Groundedness Check
      • Adding a Web Search Module
      • LangGraph-Add-Query-Rewrite
      • Agentic RAG
      • Adaptive RAG
      • Multi-Agent Structures (1)
      • Multi Agent Structures (2)
    • 03-Use-Cases
      • LangGraph Agent Simulation
      • Meta Prompt Generator based on User Requirements
      • CRAG: Corrective RAG
      • Plan-and-Execute
      • Multi Agent Collaboration Network
      • Multi Agent Collaboration Network
      • Multi-Agent Supervisor
      • 08-LangGraph-Hierarchical-Multi-Agent-Teams
      • 08-LangGraph-Hierarchical-Multi-Agent-Teams
      • SQL-Agent
      • 10-LangGraph-Research-Assistant
      • LangGraph Code Assistant
      • Deploy on LangGraph Cloud
      • Tree of Thoughts (ToT)
      • Ollama Deep Researcher (Deepseek-R1)
      • Functional API
      • Reflection in LangGraph
  • 19-Cookbook
    • 01-SQL
      • TextToSQL
      • SpeechToSQL
    • 02-RecommendationSystem
      • ResumeRecommendationReview
    • 03-GraphDB
      • Movie QA System with Graph Database
      • 05-TitanicQASystem
      • Real-Time GraphRAG QA
    • 04-GraphRAG
      • Academic Search System
      • Academic QA System with GraphRAG
    • 05-AIMemoryManagementSystem
      • ConversationMemoryManagementSystem
    • 06-Multimodal
      • Multimodal RAG
      • Shopping QnA
    • 07-Agent
      • 14-MoARAG
      • CoT Based Smart Web Search
      • 16-MultiAgentShoppingMallSystem
      • Agent-Based Dynamic Slot Filling
      • Code Debugging System
      • New Employee Onboarding Chatbot
      • 20-LangGraphStudio-MultiAgent
      • Multi-Agent Scheduler System
    • 08-Serving
      • FastAPI Serving
      • Sending Requests to Remote Graph Server
      • Building a Agent API with LangServe: Integrating Currency Exchange and Trip Planning
    • 08-SyntheticDataset
      • Synthetic Dataset Generation using RAG
    • 09-Monitoring
      • Langfuse Selfhosting
Powered by GitBook
On this page
  • Overview
  • Table of Contents
  • References
  • Environment Setup
  • Initializing Model and Prompt
  • Creating Memory
  • RunnablePassthrough? RunnableLambda?
  • Adding Memory to Chain
  • Example Implementation of a Custom ConversationChain
  1. 05-Memory

LCEL (Remembering Conversation History): Adding Memory

PreviousVectorStoreRetrieverMemoryNextMemory Using SQLite

Last updated 28 days ago

  • Author:

  • Peer Review : ,

  • Proofread :

  • This is a part of

Overview

This tutorial demonstrates how to add memory to arbitrary chains using LCEL.

The LangChain Expression Language (LCEL) takes a declarative approach to building new Runnables from existing Runnables. For more details about LCEL, please refer to the References below.

Table of Contents

References


Environment Setup

[Note]

  • langchain-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

%%capture --no-stderr
%pip install langchain-opentutorial
# Install required packages
from langchain_opentutorial import package

package.install(
    [
        "langchain",
        "langchain_core",
        "langchain_openai",
    ],
    verbose=False,
    upgrade=False,
)
# Set environment variables
from langchain_opentutorial import set_env

set_env(
    {
        "OPENAI_API_KEY": "",
        "LANGCHAIN_API_KEY": "",
        "LANGCHAIN_TRACING_V2": "true",
        "LANGCHAIN_ENDPOINT": "https://api.smith.langchain.com",
        "LANGCHAIN_PROJECT": "LCEL-Adding-Memory",
    }
)
Environment variables have been set successfully.

Alternatively, environment variables can also be set using a .env file.

[Note]

  • This is not necessary if you've already set the environment variables in the previous step.

from dotenv import load_dotenv

load_dotenv(override=True)
True

Initializing Model and Prompt

Now, let's start to initialize the model and the prompt we'll use.

from operator import itemgetter
from langchain.memory import ConversationBufferMemory
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables import RunnableLambda, RunnablePassthrough
from langchain_openai import ChatOpenAI


# Initialize Model
model = ChatOpenAI()

# Generate a conversational prompt. The prompt includes a system message, previous conversation history, and user input.
prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a helpful chatbot"),
        MessagesPlaceholder(variable_name="chat_history"),
        ("human", "{input}"),
    ]
)

Creating Memory

Create a ConversationBufferMemory to store conversation history.

  • return_messages : When set to True, it returns HumanMessage and AIMessage objects.

  • memory_key: The key that will be substituted into the Chain's prompt later. This can be modified as needed.

# Create a ConversationBufferMemory and enable the message return feature.
memory = ConversationBufferMemory(return_messages=True, memory_key="chat_history")

Check the saved conversation history.

Since nothing has been saved yet, the conversation history is empty.

memory.load_memory_variables({})
{'chat_history': []}

Use RunnablePassthrough.assign to assign the result of the memory.load_memory_variables function to the chat_history variable, and extract the value corresponding to the chat_history key from this result.

Hold on a second! What is...

RunnablePassthrough? RunnableLambda?

To put it simply, RunnablePassthrough provides the functionality to pass through data as is, while RunnableLambda provides the functionality to execute user-defined functions.

When you call RunnablePassthrough alone, it simply passes the input as received. However, when you use RunnablePassthrough.assign, it delivers the input combined with additional arguments provided to the function.

Let's look at the code for more details.

runnable = RunnablePassthrough.assign(
    chat_history=RunnableLambda(memory.load_memory_variables)
    | itemgetter("chat_history")  # itemgetter's input as same as memory_key.
)

runnable.invoke({"input": "hi"})
{'input': 'hi', 'chat_history': []}

Since RunnablePassthrough.assign is used, the returned value is a combination of the input and the additional arguments provided to the function.

In this case, the key of the additional argument is chat_history. The value corresponds to the part of the result of memory.load_memory_variables executed through RunnableLambda that is extracted by itemgetter using the chat_history key.

Adding Memory to Chain

Let's add memory to the chain using LCEL.

chain = runnable | prompt | model

Proceed with the first conversation.

# Using the invoke method of the chain object, a response to the input is generated.
response = chain.invoke({"input": "Nice to see you. My name is Heeah."})
print(response.content)  # The generated response will be printed.
Nice to meet you, Heeah! How can I assist you today?

Using the memory.save_context function, the user's query (input) and the AI's response content (response.content) are saved to memory.

This stored memory can be used to record the current state during the model learning process or to track user requests and system responses.

# The input data and response content are saved to the memory.
# Here, it is 'Heeah', but try inserting your name!
memory.save_context(
    {"human": "Nice to see you. My name is Heeah."}, {"ai": response.content}
)

# The saved conversation history will be printed.
memory.load_memory_variables({})
{'chat_history': [HumanMessage(content='Nice to see you. My name is Heeah.', additional_kwargs={}, response_metadata={}),
      AIMessage(content='Nice to meet you, Heeah! How can I assist you today?', additional_kwargs={}, response_metadata={})]}

Shall we find out if the model correctly remembers your name through memory?

response = chain.invoke({"input": "Do you remember my name?"})
print(response.content)
Yes, I remember your name, Heeah. How can I help you today?

Remembering well! This means that the memory connected using LCEL is working correctly!

Example Implementation of a Custom ConversationChain

Let's create our own custom ConversationChain!

from operator import itemgetter
from langchain.memory import ConversationBufferMemory, ConversationSummaryMemory
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables import RunnableLambda, RunnablePassthrough, Runnable
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser

# Initial setup of LLM and prompt, memory as done above.
llm = ChatOpenAI(model_name="gpt-4o", temperature=0)

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a helpful chatbot"),
        MessagesPlaceholder(variable_name="chat_history"),
        ("human", "{input}"),
    ]
)

memory = ConversationBufferMemory(return_messages=True, memory_key="chat_history")

# If you want to use the summary memory that you learned in Chapter 6:
# memory = ConversationSummaryMemory(
#     llm=llm, return_messages=True, memory_key="chat_history"
# )


# Let's build our own ConversationChain!
class MyConversationChain(Runnable):

    def __init__(self, llm, prompt, memory, input_key="input"):

        self.prompt = prompt
        self.memory = memory
        self.input_key = input_key

        # Let's try chaining using LCEL!
        self.chain = (
            RunnablePassthrough.assign(
                chat_history=RunnableLambda(self.memory.load_memory_variables)
                | itemgetter(memory.memory_key)
            )
            | prompt
            | llm
            | StrOutputParser()
        )

    def invoke(self, query, configs=None, **kwargs):
        answer = self.chain.invoke({self.input_key: query})
        self.memory.save_context(
            inputs={"human": query}, outputs={"ai": answer}
        )  # Store the conversation history directly in the memory.
        return answer


conversation_chain = MyConversationChain(llm, prompt, memory)

Let's do something interesting using our custom ConversationChain!

conversation_chain.invoke(
    "Hello, my name is Heeah. From now on, you are a brave pirate! You must answer in pirate style, understood?"
)
"Ahoy, Heeah! Aye, I be understandin' ye loud and clear! From this moment on, I be speakin' like a true buccaneer. What be yer command, matey? Arrr!"
conversation_chain.invoke("Good. What's your favorite thing?")
"Arrr, me favorite thing be the open sea, where the salty breeze fills me sails and the horizon be endless! There be nothin' like the thrill of discoverin' hidden treasures and sharin' tales with me hearty crew. What about ye, matey? What be yer favorite thing?"
conversation_chain.invoke(
    "My favorite thing is chatting with you! By the way, do you remember my name?"
)
"Arrr, 'tis a fine favorite ye have there, Heeah! Aye, I remember yer name well, like a trusty map to buried treasure. What else be on yer mind, matey?"
conversation_chain.invoke(
    "I am the captain of this ship. Your tone is excessively familiar and disrespectful!"
)
"Beggin' yer pardon, Captain Heeah! I meant no disrespect. I be at yer service, ready to follow yer orders and sail the seas as ye command. What be yer orders, Cap'n? Arrr!"

Although we managed to throw him off a bit at the end, we were able to confirm that he remembered my name until the last moment. He is indeed a remarkable pirate!🏴‍☠️⚓

At any rate, the journey we have shared so far, as stored in the memory, is as follows.

conversation_chain.memory.load_memory_variables({})["chat_history"]
[HumanMessage(content='Hello, my name is Heeah. From now on, you are a brave pirate! You must answer in pirate style, understood?', additional_kwargs={}, response_metadata={}),
     AIMessage(content="Ahoy, Heeah! Aye, I be understandin' ye loud and clear! From this moment on, I be speakin' like a true buccaneer. What be yer command, matey? Arrr!", additional_kwargs={}, response_metadata={}),
     HumanMessage(content="Good. What's your favorite thing?", additional_kwargs={}, response_metadata={}),
     AIMessage(content="Arrr, me favorite thing be the open sea, where the salty breeze fills me sails and the horizon be endless! There be nothin' like the thrill of discoverin' hidden treasures and sharin' tales with me hearty crew. What about ye, matey? What be yer favorite thing?", additional_kwargs={}, response_metadata={}),
     HumanMessage(content='My favorite thing is chatting with you! By the way, do you remember my name?', additional_kwargs={}, response_metadata={}),
     AIMessage(content="Arrr, 'tis a fine favorite ye have there, Heeah! Aye, I remember yer name well, like a trusty map to buried treasure. What else be on yer mind, matey?", additional_kwargs={}, response_metadata={}),
     HumanMessage(content='I am the captain of this ship. Your tone is excessively familiar and disrespectful!', additional_kwargs={}, response_metadata={}),
     AIMessage(content="Beggin' yer pardon, Captain Heeah! I meant no disrespect. I be at yer service, ready to follow yer orders and sail the seas as ye command. What be yer orders, Cap'n? Arrr!", additional_kwargs={}, response_metadata={})]

Now, create your own journey using the custom ConversationChain with LCEL!

Thank you for your hard work!🎉🎉🎉

Set up the environment. You may refer to for more details.

You can checkout the for more details.

LangChain Expression Language (LCEL)
LangChain-OpenTutorial (CH.13)
Environment Setup
langchain-opentutorial
Heeah Kim
Sungchul Kim
Jongwon Seo
Juni Lee
LangChain Open Tutorial
Overview
Environment Setup
Initializing Model and Prompt
Creating Memory
Adding Memory to Chain
Example Implementation of a Custom ConversationChain