We'll use an LLMChain, and show working with both an LLM and a ChatModel.Using an LLM‚Äãfrom langchain.llms import OpenAIfrom langchain.prompts import PromptTemplatefrom langchain.chains import LLMChainfrom langchain.memory import ConversationBufferMemoryllm = OpenAI(temperature=0)# Notice that "chat_history" is present in the prompt templatetemplate = """You are a nice chatbot having a conversation with a human.Previous conversation:{chat_history}New human question: {question}Response:"""prompt = PromptTemplate.from_template(template)# Notice that we need to align the `memory_key`memory = ConversationBufferMemory(memory_key="chat_history")conversation = LLMChain(    llm=llm,    prompt=prompt,    verbose=True,    memory=memory)# Notice that we just pass in the `question` variables - `chat_history` gets populated by memoryconversation({"question": "hi"})Using a ChatModel‚Äãfrom langchain.chat_models import ChatOpenAIfrom langchain.prompts import (    ChatPromptTemplate,    MessagesPlaceholder,    SystemMessagePromptTemplate,    HumanMessagePromptTemplate,)from langchain.chains import LLMChainfrom langchain.memory import ConversationBufferMemoryllm = ChatOpenAI()prompt = ChatPromptTemplate(    messages=[        SystemMessagePromptTemplate.from_template(            "You are a nice chatbot having a conversation with a human."        ),        # The `variable_name` here is what must align with memory        MessagesPlaceholder(variable_name="chat_history"),        HumanMessagePromptTemplate.from_template("{question}")    ])# Notice that we `return_messages=True` to fit into the MessagesPlaceholder# Notice that `"chat_history"` aligns with the MessagesPlaceholder name.memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)conversation = LLMChain(    llm=llm,    prompt=prompt,    verbose=True,    memory=memory)# Notice that we just pass in the `question` variables - `chat_history` gets populated by memoryconversation({"question": "hi"})Next
Most LLM applications have a conversational interface. An essential component of a conversation is being able to refer to information introduced earlier in the conversation.
Most LLM applications have a conversational interface. An essential component of a conversation is being able to refer to information introduced earlier in the conversation. ->: We'll use an LLMChain, and show working with both an LLM and a ChatModel.Using an LLM‚Äãfrom langchain.llms import OpenAIfrom langchain.prompts import PromptTemplatefrom langchain.chains import LLMChainfrom langchain.memory import ConversationBufferMemoryllm = OpenAI(temperature=0)# Notice that "chat_history" is present in the prompt templatetemplate = """You are a nice chatbot having a conversation with a human.Previous conversation:{chat_history}New human question: {question}Response:"""prompt = PromptTemplate.from_template(template)# Notice that we need to align the `memory_key`memory = ConversationBufferMemory(memory_key="chat_history")conversation = LLMChain(    llm=llm,    prompt=prompt,    verbose=True,    memory=memory)# Notice that we just pass in the `question` variables - `chat_history` gets populated by memoryconversation({"question": "hi"})Using a ChatModel‚Äãfrom langchain.chat_models import ChatOpenAIfrom langchain.prompts import (    ChatPromptTemplate,    MessagesPlaceholder,    SystemMessagePromptTemplate,    HumanMessagePromptTemplate,)from langchain.chains import LLMChainfrom langchain.memory import ConversationBufferMemoryllm = ChatOpenAI()prompt = ChatPromptTemplate(    messages=[        SystemMessagePromptTemplate.from_template(            "You are a nice chatbot having a conversation with a human."        ),        # The `variable_name` here is what must align with memory        MessagesPlaceholder(variable_name="chat_history"),        HumanMessagePromptTemplate.from_template("{question}")    ])# Notice that we `return_messages=True` to fit into the MessagesPlaceholder# Notice that `"chat_history"` aligns with the MessagesPlaceholder name.memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)conversation = LLMChain(    llm=llm,    prompt=prompt,    verbose=True,    memory=memory)# Notice that we just pass in the `question` variables - `chat_history` gets populated by memoryconversation({"question": "hi"})Next