Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toCookbookPrompt + LLMRAGMultiple chainsQuerying a SQL DBAgentsCode writingAdding memoryAdding moderationUsing toolsLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãORetrievalChainsMemoryAgentsCallbacksModulesSecurityGuidesMoreLangChain Expression LanguageCookbookAdding memoryAdding memoryThis shows how to add memory to an arbitrary chain. Right now, you can use the memory classes but need to hook it up manuallyfrom operator import itemgetterfrom langchain.chat_models import ChatOpenAIfrom langchain.memory import ConversationBufferMemoryfrom langchain.schema.runnable import RunnablePassthrough, RunnableLambdafrom langchain.prompts import ChatPromptTemplate, MessagesPlaceholdermodel = ChatOpenAI()prompt = ChatPromptTemplate.from_messages([    ("system", "You are a helpful chatbot"),    MessagesPlaceholder(variable_name="history"),    ("human", "{input}")])memory = ConversationBufferMemory(return_messages=True)memory.load_memory_variables({})    {'history': []}chain = RunnablePassthrough.assign(    memory=RunnableLambda(memory.load_memory_variables) | itemgetter("history")) | prompt | modelinputs = {"input": "hi im bob"}response = chain.invoke(inputs)response    AIMessage(content='Hello Bob! How can I assist you today?', additional_kwargs={}, example=False)memory.save_context(inputs, {"output": response.content})memory.load_memory_variables({})    {'history': [HumanMessage(content='hi im bob', additional_kwargs={}, example=False),      AIMessage(content='Hello Bob! How can I assist you today?', additional_kwargs={}, example=False)]}inputs = {"input": "whats my name"}response = chain.invoke(inputs)response    AIMessage(content='Your name is Bob.', additional_kwargs={}, example=False)PreviousCode writingNextAdding
This shows how to add memory to an arbitrary chain. Right now, you can use the memory classes but need to hook it up manually
This shows how to add memory to an arbitrary chain. Right now, you can use the memory classes but need to hook it up manually ->: Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toCookbookPrompt + LLMRAGMultiple chainsQuerying a SQL DBAgentsCode writingAdding memoryAdding moderationUsing toolsLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãORetrievalChainsMemoryAgentsCallbacksModulesSecurityGuidesMoreLangChain Expression LanguageCookbookAdding memoryAdding memoryThis shows how to add memory to an arbitrary chain. Right now, you can use the memory classes but need to hook it up manuallyfrom operator import itemgetterfrom langchain.chat_models import ChatOpenAIfrom langchain.memory import ConversationBufferMemoryfrom langchain.schema.runnable import RunnablePassthrough, RunnableLambdafrom langchain.prompts import ChatPromptTemplate, MessagesPlaceholdermodel = ChatOpenAI()prompt = ChatPromptTemplate.from_messages([    ("system", "You are a helpful chatbot"),    MessagesPlaceholder(variable_name="history"),    ("human", "{input}")])memory = ConversationBufferMemory(return_messages=True)memory.load_memory_variables({})    {'history': []}chain = RunnablePassthrough.assign(    memory=RunnableLambda(memory.load_memory_variables) | itemgetter("history")) | prompt | modelinputs = {"input": "hi im bob"}response = chain.invoke(inputs)response    AIMessage(content='Hello Bob! How can I assist you today?', additional_kwargs={}, example=False)memory.save_context(inputs, {"output": response.content})memory.load_memory_variables({})    {'history': [HumanMessage(content='hi im bob', additional_kwargs={}, example=False),      AIMessage(content='Hello Bob! How can I assist you today?', additional_kwargs={}, example=False)]}inputs = {"input": "whats my name"}response = chain.invoke(inputs)response    AIMessage(content='Your name is Bob.', additional_kwargs={}, example=False)PreviousCode writingNextAdding