Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toCookbookLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãORetrievalChainsMemoryChat MessagesMemory typesMemory in LLMChainMemory in the Multi-Input ChainMemory in AgentMessage Memory in Agent backed by a databaseCustomizing Conversational MemoryCustom MemoryMultiple Memory classesAgentsCallbacksModulesSecurityGuidesMoreModulesMemoryMemory in LLMChainOn this pageMemory in LLMChainThis notebook goes over how to use the Memory class with an LLMChain. We will add  the ConversationBufferMemory class, although this can be any memory class.from langchain.chains import LLMChainfrom langchain.llms import OpenAIfrom langchain.memory import ConversationBufferMemoryfrom langchain.prompts import PromptTemplateThe most important step is setting up the prompt correctly. In the below prompt, we have two input keys: one for the actual input, another for the input from the Memory class. Importantly, we make sure the keys in the PromptTemplate and the ConversationBufferMemory match up (chat_history).template = """You are a chatbot having a conversation with a human.{chat_history}Human: {human_input}Chatbot:"""prompt = PromptTemplate(    input_variables=["chat_history", "human_input"], template=template)memory = ConversationBufferMemory(memory_key="chat_history")llm = OpenAI()llm_chain = LLMChain(    llm=llm,    prompt=prompt,    verbose=True,    memory=memory,)llm_chain.predict(human_input="Hi there my friend")            > Entering new LLMChain chain...    Prompt after formatting:    You are a chatbot having a conversation with a human.            Human: Hi there my friend    Chatbot:        > Finished chain.    ' Hi there! How can I help you today?'llm_chain.predict(human_input="Not too bad - how are you?")            > Entering new LLMChain chain...
This notebook goes over how to use the Memory class with an LLMChain.
This notebook goes over how to use the Memory class with an LLMChain. ->: Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toCookbookLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãORetrievalChainsMemoryChat MessagesMemory typesMemory in LLMChainMemory in the Multi-Input ChainMemory in AgentMessage Memory in Agent backed by a databaseCustomizing Conversational MemoryCustom MemoryMultiple Memory classesAgentsCallbacksModulesSecurityGuidesMoreModulesMemoryMemory in LLMChainOn this pageMemory in LLMChainThis notebook goes over how to use the Memory class with an LLMChain. We will add  the ConversationBufferMemory class, although this can be any memory class.from langchain.chains import LLMChainfrom langchain.llms import OpenAIfrom langchain.memory import ConversationBufferMemoryfrom langchain.prompts import PromptTemplateThe most important step is setting up the prompt correctly. In the below prompt, we have two input keys: one for the actual input, another for the input from the Memory class. Importantly, we make sure the keys in the PromptTemplate and the ConversationBufferMemory match up (chat_history).template = """You are a chatbot having a conversation with a human.{chat_history}Human: {human_input}Chatbot:"""prompt = PromptTemplate(    input_variables=["chat_history", "human_input"], template=template)memory = ConversationBufferMemory(memory_key="chat_history")llm = OpenAI()llm_chain = LLMChain(    llm=llm,    prompt=prompt,    verbose=True,    memory=memory,)llm_chain.predict(human_input="Hi there my friend")            > Entering new LLMChain chain...    Prompt after formatting:    You are a chatbot having a conversation with a human.            Human: Hi there my friend    Chatbot:        > Finished chain.    ' Hi there! How can I help you today?'llm_chain.predict(human_input="Not too bad - how are you?")            > Entering new LLMChain chain...