1. An LLM Has No Memory
DefinitionAn LLM is stateless. Every call is new, and the model does not remember earlier calls.
2. Memory Is a List You Resend
The model forgets, so you keep the memory. Store every message in a list and send the whole list on every call. The reply goes into the list too.
Added to the list, ready for the next turn.
3. What Is a Context Window?
DefinitionA context window is the maximum number of tokens a model can read in one call.
The list grows every turn, so one day it no longer fits.
Chatbot with historychat_history.py · 35 linesShow code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | from dotenv import load_dotenv load_dotenv() from langchain_core.messages import AIMessage,SystemMessage,HumanMessage from langchain.chat_models import init_chat_model from rich import print model = init_chat_model("mistralai:mistral-small-2603", temperature=0.7) chat_history = [ SystemMessage(content="You are a helpful coding tutor. Keep answers brief.") ] print(SystemMessage(content="You are a helpful coding tutor. Keep answers brief.")) print("welcome to chatbot type exit to quit") while True: user_input = input("You : ask any think: ") if user_input == "exit": break chat_history.append(HumanMessage(content=user_input)) response = model.invoke(chat_history) chat_history.append(AIMessage(content=response.content)) print(f"Bot: {response.content}") print(chat_history) |
4. MessagesPlaceholder
DefinitionA MessagesPlaceholder is a slot in a
ChatPromptTemplatethat you fill with a list of messages.
A normal blank like {state} holds one piece of text. History is a list, so it needs a slot of its own. There are two ways to write it. Pick a tab.
from langchain_core.prompts import ChatPromptTemplatefrom langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder template = ChatPromptTemplate.from_messages([ ("system", "You are a helpful assistant."), ("placeholder", "{chat_history}"), MessagesPlaceholder("chat_history"), ("human", "{question}"), ])
MessagesPlaceholder. The result is the same.Now fill the slot with the history.
The first word of each tuple is a fixed keyword, not a name you choose. "system", "human" and "ai" are the roles from the lesson Writing Messages. "placeholder" is one more keyword, and it is not a role. It is the short form of MessagesPlaceholder.
Chatbot with MessagesPlaceholderchat_history_placeholder.py · 41 linesShow code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | from dotenv import load_dotenv load_dotenv() from langchain_core.prompts import ChatPromptTemplate from langchain_core.messages import HumanMessage, AIMessage from langchain.chat_models import init_chat_model model = init_chat_model("mistralai:mistral-small-2603", temperature=0.7) # 1. Define Template with placeholder in the middle template = ChatPromptTemplate.from_messages([ ("system", "You are a helpful and concise AI tutor."), ("placeholder", "{chat_history}"), # Dynamic message list slot ("human", "{question}") # Current user question ]) # 2. History Storage chat_history = [] print("welcome to chatbot - Type 'exit' to quit") while True: user_query = input("You: ") if user_query.lower() == "exit": break # Step A: Format template with history and current question formatted_prompt = template.invoke({ "chat_history": chat_history, "question": user_query }) # Step B: Call the model response = model.invoke(formatted_prompt) print("AI:", response.content) print() # Step C: Update history for the next iteration chat_history.append(HumanMessage(content=user_query)) chat_history.append(AIMessage(content=response.content)) |