หน่วยความจำและบริบทการสนทนาใน LangChain
เรียนรู้ว่าหน่วยความจำของ LangChain ติดตามประวัติการสนทนาอย่างไร เพื่อให้สายงานและแชตบอตสนทนาหลายรอบได้อย่างต่อเนื่องสอดคล้อง
หน่วยความจำและบริบทการสนทนาใน LangChain เป็นบทเรียน LangChain / RAG / Vector DBs ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน LangChain / RAG / Vector DBs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส LangChain / RAG / Vector DBs มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
LLMs Are Stateless
A language model has no memory between calls. Each request is independent — it only knows what you put in the current prompt. To build a chatbot that remembers, you must feed prior turns back in yourself.
What LangChain Memory Does
LangChain memory automates this: it stores the conversation and injects relevant history into the prompt on each new turn. Your chain stays simple while the model appears to remember.
Conversation Buffer Memory
The simplest memory keeps the full transcript and prepends it to every prompt. Great for short chats, but it grows with every turn.
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
memory.save_context({'input': 'Hi, I am Sam'}, {'output': 'Hello Sam!'})
print(memory.load_memory_variables({}))The Context Window Limit
Every model has a finite context window (a token budget). A growing buffer eventually overflows it, causing errors or truncation. Managing history size is the central challenge of memory.
turns = 50
tokens_per_turn = 200
total = turns * tokens_per_turn
print('history tokens:', total)Window Memory
ConversationBufferWindowMemory keeps only the last k turns. It bounds token usage at the cost of forgetting older context — a simple, effective trade-off for many chatbots.
from langchain.memory import ConversationBufferWindowMemory
memory = ConversationBufferWindowMemory(k=3)
# only the most recent 3 exchanges are keptSummary Memory
ConversationSummaryMemory uses the LLM to compress old turns into a running summary. You keep the gist of a long conversation in far fewer tokens, sacrificing exact wording for breadth.
Summary Buffer: Best of Both
ConversationSummaryBufferMemory keeps recent turns verbatim and summarizes everything older. Recent context stays precise while distant context is condensed — a popular default for production chatbots.
Wiring Memory into a Chain
You attach memory to a conversational chain. On each call, the chain loads history, builds the prompt, calls the model, and saves the new turn back to memory automatically.
from langchain.chains import ConversationChain
chain = ConversationChain(llm=llm, memory=memory)
chain.predict(input='What is my name?')Memory Variables and Prompts
Memory exposes its content as a variable (often history or chat_history) that your prompt template references. The placeholder is where the stored conversation gets injected.
template = 'Conversation so far:\n{history}\nHuman: {input}\nAI:'Persisting Memory
In-process memory vanishes when the app restarts. For real users, back memory with a store — Redis, a database, or a chat-message-history backend keyed by session id — so conversations survive across requests and servers.
Choosing a Memory Type
Match memory to need: buffer for short chats, window when you only care about recent turns, summary for long sessions on a budget, and summary-buffer for the common case. Always persist memory for multi-user apps.
Quick Check
Test your understanding of LangChain memory.
Recap
You learned how LangChain gives chatbots memory:
- LLMs are stateless; memory re-injects history each turn
- Buffer, window, summary, and summary-buffer trade detail against tokens
- Memory exposes a history variable that the prompt template uses
- Persist memory per session for multi-user, multi-server apps
คำถามที่พบบ่อย
บทเรียน “หน่วยความจำและบริบทการสนทนาใน LangChain” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “หน่วยความจำและบริบทการสนทนาใน LangChain” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส LangChain / RAG / Vector DBs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส LangChain / RAG / Vector DBs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “หน่วยความจำและบริบทการสนทนาใน LangChain”
เรียนรู้ว่าหน่วยความจำของ LangChain ติดตามประวัติการสนทนาอย่างไร เพื่อให้สายงานและแชตบอตสนทนาหลายรอบได้อย่างต่อเนื่องสอดคล้อง คุณปฏิบัติ LangChain / RAG / Vector DBs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน LangChain / RAG / Vector DBs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน LangChain / RAG / Vector DBs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “หน่วยความจำและบริบทการสนทนาใน LangChain” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน LangChain / RAG / Vector DBs นี้ได้ไหม
ได้ บทเรียน LangChain / RAG / Vector DBs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การตั้งค่าสภาพแวดล้อม LangChain
- พรอมป์ LLM และสายงานพื้นฐาน
- ตัวแยกวิเคราะห์ผลลัพธ์และการเรียกกลับ
- หน่วยความจำและบริบทการสนทนาใน LangChain